This article mainly introduced the PHP data analysis engine calculates the cosine similarity algorithm, combined with the concrete instance form analysis PHP calculates the cosine similarity the operation procedure and the related realization skill, needs the friend to refer to the next
In this paper, the calculation of cosine similarity algorithm for PHP data analysis engine is described. Share to everyone for your reference, as follows:
About the cosine similarity of the relevant introduction can refer to Baidu Encyclopedia: Cosine similarity
<?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 datum vector * @author yu.guo@okhqb.com *//** * Get the modulus of the vector * @param unknown_type the n-dimensional vector $array the datum point 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 The modulus of the benchmark vector * @param unknown_type $intLenth The length of the vector*/function getcosine ($arrMark, $arrAnaly, $strMarkMod, $intLenth) {$strVector = 0; $strCosine = 0; for ($i = 0; $i < $in Tlenth; $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;}? >