[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 ...
随机推荐
- HDU 5752
Sqrt Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- TestNG测试执行顺序
1.preserve-order属性,之前一直认为preserve-order属性是控制配置方法的执行顺序的,其实不是,preserve-order主要是控制test下节点classes执行顺序的 例 ...
- WINDOWS2008 KMS 服务器安装及激活
搭建环境条件: windows server 2008 enterprise 安装光盘kms密钥kms服务安装步骤: 安装第一台windows server 2008 enterprise服务器用km ...
- python装饰器(整理版)
Python中函数有一个装饰器的概念,今天,看核心编程中的函数一章的时候接触到了这个概念,炸一看来,讲的说明真实不好明白.于是写下本篇以示说明,提供给迷糊者.希望能对一些人起到一定的帮助 装饰器的语法 ...
- C# 技巧收藏
IIS HTTP Error 500.21 - Internal Server Error 原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装 解决:开始- ...
- 10个简化Web开发者工作的HTML5开发工具
HTML5的到来,改变了设计和开发的工作,完全改变了以前的开发方式. HTML5进行本身就是一个很简单,很快捷的开发技术并且带给开发人员很多不同的工具和功能,使他们的工作变得更加Cool.它的功能非常 ...
- [HNOI2002] 公交车路线
题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另外一个公交站往往要换几次车,例如从公交站A到 ...
- 跳石头(NOIP2015) (二分查找)
原题传送门 好久没更了..昨天去学zkw线段树,被zxyer狠狠地D了一顿.. 来补坑.. 这是一道很奇特的题目. 根据题目可以看出这道题有二分题具有的性质.. 不懂二分性质的可以看我以前的博客 传送 ...
- (1)IIS
1.选择控制面板-程序和功能 2.点击左侧“打开或关闭Windows功能 3.internet信息服务下的文件夹全勾 4.安装完成后,选择管理工具 5.选择IIS管理器 6.显示如下
- 洛谷——P1258 小车问题
P1258 小车问题 题目描述 甲.乙两人同时从A地出发要尽快同时赶到B地.出发时A地有一辆小车,可是这辆小车除了驾驶员外只能带一人.已知甲.乙两人的步行速度一样,且小于车的速度.问:怎样利用小车才能 ...