Cosine similarity implementation—php data analysis PHP implementation
<?php /** * 数据分析引擎 * 分析向量的元素 必须和基准向量的元素一致,取最大个数,分析向量不足元素以0填补。 * 求出分析向量与基准向量的余弦值 * @php爱好者 */ /** * 获得向量的模 * @param unknown_type $array 传入分析数据的基准点的N维向量。|eg:array(1,1,1,1,1); */ function getMarkMod($arrParam){ $strModDouble = 0; foreach($arrParam as $val){ $strModDouble += $val * $val; } $strMod = sqrt($strModDouble); //是否需要保留小数点后几位 return $strMod; } /** * 获取标杆的元素个数 * @param unknown_type $arrParam * @return number */ function getMarkLenth($arrParam){ $intLenth = count($arrParam); return $intLenth; } /** * 对传入数组进行索引分配,基准点的索引必须为k,求夹角的向量索引必须为 '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标杆向量数组(索引被处理过) * @param unknown_type $arrAnaly 分析向量数组 (索引被处理过) |array('j0'=>1,'j1'=>2....) * @param unknown_type $strMarkMod标杆向量的模 * @param unknown_type $intLenth 向量的长度 */ 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; } $arrAnalyMod = getMarkMod($arrAnaly); //求分析向量的模 $strFenzi = $strVector; $strFenMu = $arrAnalyMod * $strMarkMod; $strCosine = $strFenzi / $strFenMu; if(0 !== (int)$strFenMu){ $strCosine = $strFenzi / $strFenMu; } return $strCosine; } ?>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
How to fix KB5055612 fails to install in Windows 10?
4 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Nordhold: Fusion System, Explained
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
Java Tutorial
1672
14


CakePHP Tutorial
1428
52


Laravel Tutorial
1332
25


PHP Tutorial
1277
29


C# Tutorial
1257
24

