Example of the PHP data analysis engine calculates the cosine similarity

Source: Internet
Author: User
Tags modulus
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;}? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.