Realization of cosine similarity of php-data analysis

Source: Internet
Author: User
Tags benchmark modulus

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;}? >


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.