[Algorithms] Classify Mystery Items with the K-Nearest Neighbors Algorithm in JavaScript
The k-nearest neighbors algorithm is used for classification of unknown items and involves calculating the distance of the unknown item's neighbors. We'll use Euclidean distance to determine the closest neighbor and make our best guess on what the mystery fruit is.
Just a Euclidean distance, that's all...
function determineFruit({ size, redness }) {
const fruit = [
{ name: "grape", size: 1, redness: 0 },
{ name: "orange", size: 2, redness: 1 },
{ name: "grapefruit", size: 3, redness: 2 }
];
const { name } = fruit.reduce(
(prev, cur) => {
let curCalc = calcDistance([[size, cur.size], [redness, cur.redness]]);
console.log(curCalc);
return prev.dist < curCalc ? prev : { name: cur.name, dist: curCalc };
},
{
name: fruit[0].name,
dist: calcDistance([[size, fruit[0].size], [redness, fruit[0].redness]])
}
);
return `This is most likely a ${name}`;
}
function calcDistance(data) {
return Math.sqrt(
data.reduce((acc, curr) => console.log(curr) && acc + Math.pow(curr[0] - curr[1], 2), 0)
);
}
console.log(determineFruit({ size: 2, redness: 2 }));
[Algorithms] Classify Mystery Items with the K-Nearest Neighbors Algorithm in JavaScript的更多相关文章
- [机器学习系列] k-近邻算法(K–nearest neighbors)
C++ with Machine Learning -K–nearest neighbors 我本想写C++与人工智能,但是转念一想,人工智能范围太大了,我根本介绍不完也没能力介绍完,所以还是取了他的 ...
- K Nearest Neighbor 算法
文章出处:http://coolshell.cn/articles/8052.html K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KN ...
- K NEAREST NEIGHBOR 算法(knn)
K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KNN算法是相对比较容易理解的算法.其中的K表示最接近自己的K个数据样本.KNN算法和K-M ...
- 快速近似最近邻搜索库 FLANN - Fast Library for Approximate Nearest Neighbors
What is FLANN? FLANN is a library for performing fast approximate nearest neighbor searches in high ...
- What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?
QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...
- K近邻(K Nearest Neighbor-KNN)原理讲解及实现
算法原理 K最近邻(k-Nearest Neighbor)算法是比较简单的机器学习算法.它采用测量不同特征值之间的距离方法进行分类.它的思想很简单:如果一个样本在特征空间中的k个最近邻(最相似)的样本 ...
- K nearest neighbor cs229
vectorized code 带来的好处. import numpy as np from sklearn.datasets import fetch_mldata import time impo ...
- K-Means和K Nearest Neighbor
来自酷壳: http://coolshell.cn/articles/7779.html http://coolshell.cn/articles/8052.html
- Approximate Nearest Neighbors.接近最近邻搜索
(一):次优最近邻:http://en.wikipedia.org/wiki/Nearest_neighbor_search 有少量修改:如有疑问,请看链接原文.....1.Survey:Neares ...
随机推荐
- Python之多线程:Threading模块
1.Threading模块提供的类 Thread,Lock,Rlock,Condition,Semaphore,Event,Timer,local 2.threading模块提供的常用的方法 (1)t ...
- mysql5.6免安装配置(菜鸟版)
mysql5.6免安装配置 MySQL5.6.13安装步骤(Windows7 32位) 1. 下载MySQL Community Server 5.6.13 2. 解压MySQL压缩包 将以下载的My ...
- 解决PKIX path building failed
起因 上周在生产环境部署时,把安全证书加到k8s-ingress中时发现报该错误 解决 找网上解决方案,因为这种问题相对比较少见,也没百度,直接谷歌,找到解决方案如下:https://stackove ...
- .ini配置读取
ini文件的格式 格式 INI文件由节.键.值组成. 节 [section] 参数(键=值) name=value 注解 注解使用分号表示(;).在分号后面的文字,直到该行结尾都全部为注解. ...
- 集合类---List
一.ArrayList详解 1.继承关系 public class ArrayList<E> extends AbstractList<E> implements List&l ...
- 为什么js引入页面后不起作用?
为什么js引入页面后不起作用? 例如常见的报错:Uncaught ReferenceError: $ is not defined. 可能出现这种情况的原因如下: 原因一: 引入js的位置不对,应在使 ...
- PHP 时间获取本周 本月 本季度用法
<?php $week_begin = mktime(0, 0, 0,date("m"),date("d")-date("w&qu ...
- (1) python--numpy
废话不多说,直接上代码 import numpy as np # 如何创建一个数组 arr = np.array([1, 2, 3, 4]) print(arr) # [1 2 3 4] # 查看数组 ...
- Matlab,C++存取二进制
1,Matlab存储二进制 load Wall.dat %读取数据,数组名为Wall fid=fopen('Wall','wb'); %打开一个文件,二进制写入 fwrite(fid,Wall','f ...
- JquerySession使用
添加数据 $.session.set('key', 'value') 删除数据 $.session.remove('key'); 获取数据 $.session.get('key'); 清除数据 $.s ...