Realization of cosine similarity of php-data analysis
<?php/** * Data Analysis engine * The elements of the analysis vector must be consistent with the elements of the base vector, taking the maximum number, and the analysis vector is filled with less than 0 elements. * Find the cosine of the analysis vector and the reference vector * @author [email protected] *//** * Get the modulus of the vector * @param unknown_type $array The n-dimensional vectors of the datum points of the incoming analysis data. |eg:array (1 , 1,1,1,1); */function Getmarkmod ($arrParam) {$strModDouble = 0; foreach ($arrParam as $val) {$strModDouble + = $val * $val;} $strMod = sqrt ($strModDouble); Whether you need to retain several return $strMod after the decimal point;} /** * Gets the number of elements of the benchmark * @param unknown_type $arrParam * @return numbers */function getmarklenth ($arrParam) {$intLenth = count ($ar Rparam); return $intLenth;} /** * Indexing an incoming array, the index of the datum point must be k, and the vector index of the angle must be ' j '. * @param unknown_type $arrParam * @param unknown_type $index * @ruturn $arrBack */function handindex ($arrParam, $index = ' K ') {foreach ($arrParam as $key = + $val) {$in = $index. $key; $arrBack [$in] = $val; } return $arrBack;} /** * * @param unknown_type $arrMark benchmark vector Array (index processed) * @param unknown_type $arrAnaly Analysis vector Array (index processed) |array (' J0 ' =>1, ' J1 ' =>2 ....) * @param unknown_type $strMarkMod Benchmark vector * @param unknown_type $The length of the Intlenth vector is */function getcosine ($arrMark, $arrAnaly, $strMarkMod, $intLenth) {$strVector = 0; $strCosine = 0; for ($i = 0; $i < $intLenth; $i + +) {$strMarkVal = $arrMark [' K '. $i]; $strAnalyVal = $arrAnaly [' j '. $i]; $strVector + = $strMarkVal * $strAnalyVal;} $arrA Nalymod = Getmarkmod ($arrAnaly); The modulus $strFenzi of analytic vectors = $strVector; $strFenMu = $arrAnalyMod * $STRMARKMOD; $strCosine = $strFenzi/$strFenMu; if (0!== (int) $strFenMu) {$strCosine = $strFenzi/$strFenMu; } return $strCosine;}? >