[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 ...
随机推荐
- POJ1163 The Triangle
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44997 Accepted: 27174 Description 73 ...
- struts2中的文件上传和文件下载
单文件文件上传 1.
- HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)
M斐波那契数列 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- 微信 编码要UTF8
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...
- GPS经纬度的表示方法及换算
想要认识GPS中的经纬度,就必须先了解GPS,知道经纬度的来源: 1. GPS系统组成 GPS是 Gloabal Positioning System 的简称,意为全球定位系统,主要由地面的控制站.天 ...
- Access数据库 INSERT INTO 失败
一次操作Access数据库,插入一条数据,总是失败,如下: 通过赋值,一个字段一个字段的排查,最终确定是UserAge字段处有问题. 最初,UserAge字段是 %d 类型的,赋值20,可成功插入数据 ...
- [转]Google 的开源技术protobuf 简介与例子
本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下“Protocol Buffe ...
- msvc交叉编译:使用vcvarsall.bat设置命令行编译环境
一直以来我只知道vc设置命令行编译环境的批处理命令是%VS140COMNTOOLS%/Common7/Tools下的vsvars32.bat,(%VS140COMNTOOLS%为定义vs2015公共工 ...
- java keytool证书工具使用小结(转)
Keytool 是一个Java数据证书的管理工具 ,Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里,包含两种数据:密钥实体(K ...
- 【转载】SQL SERVER-Delete和Truncate的区别
转载博客:http://blog.csdn.net/dba_huangzj/article/details/7913068 背景 一般在删除表数据时候,通常会有执行两个SQL语句:delete和tru ...