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的更多相关文章

  1. [机器学习系列] k-近邻算法(K–nearest neighbors)

    C++ with Machine Learning -K–nearest neighbors 我本想写C++与人工智能,但是转念一想,人工智能范围太大了,我根本介绍不完也没能力介绍完,所以还是取了他的 ...

  2. K Nearest Neighbor 算法

    文章出处:http://coolshell.cn/articles/8052.html K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KN ...

  3. K NEAREST NEIGHBOR 算法(knn)

    K Nearest Neighbor算法又叫KNN算法,这个算法是机器学习里面一个比较经典的算法, 总体来说KNN算法是相对比较容易理解的算法.其中的K表示最接近自己的K个数据样本.KNN算法和K-M ...

  4. 快速近似最近邻搜索库 FLANN - Fast Library for Approximate Nearest Neighbors

    What is FLANN? FLANN is a library for performing fast approximate nearest neighbor searches in high ...

  5. 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 ...

  6. K近邻(K Nearest Neighbor-KNN)原理讲解及实现

    算法原理 K最近邻(k-Nearest Neighbor)算法是比较简单的机器学习算法.它采用测量不同特征值之间的距离方法进行分类.它的思想很简单:如果一个样本在特征空间中的k个最近邻(最相似)的样本 ...

  7. K nearest neighbor cs229

    vectorized code 带来的好处. import numpy as np from sklearn.datasets import fetch_mldata import time impo ...

  8. K-Means和K Nearest Neighbor

    来自酷壳: http://coolshell.cn/articles/7779.html http://coolshell.cn/articles/8052.html

  9. Approximate Nearest Neighbors.接近最近邻搜索

    (一):次优最近邻:http://en.wikipedia.org/wiki/Nearest_neighbor_search 有少量修改:如有疑问,请看链接原文.....1.Survey:Neares ...

随机推荐

  1. java中TCP两个例子大写服务器和文件上传

    大写服务器的实例: package com.core.net; import java.io.BufferedReader; import java.io.BufferedWriter; import ...

  2. Eclipse Tomcat 配置问题

    问题描述: Tomcat能启动,ecplise的Console无报错,但是打开http://localhost:8080/报404错误 端口是8080没错,地址也没错,重启了tomcat还是报错,但是 ...

  3. NOIP2012提高组

    D1T1.Vigenère密码 模拟 #include<iostream> #include<cstdio> using namespace std; int main() { ...

  4. 杭电oj2064、2067、2068、2073、2076-2078、2080、2083-2085

    2064  汉诺塔III #include<stdio.h> int main(){ int n,i; _int64 s[]; while(~scanf("%d",&a ...

  5. c++ 实现 http 上传和下载

    代码下载地址:   http://download.csdn.net/detail/mtour/8243527 最近写了个程序需要用到http通讯,由于flash空间比较小,没有考虑 libcurl库 ...

  6. WCF 小程序案例以及序列化的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;u ...

  7. AC日记——Valued Keys codeforces 801B

    801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...

  8. 恢复安装过树莓派相关操作系统的TF卡容量

    原文地址:传送门 前言玩树莓派的都知道,当我们向TF卡写入系统后,在Windows下能识别的只有几百M的容量了,这主要是由于在装Linux系统的时候给TF卡分了Windows无法识别的分区,下面我用图 ...

  9. Codeforces Round #445 D. Restoration of string【字符串】

    D. Restoration of string time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  10. superviosrd进程管理

    supervisor进程管理器---------------------------1. 安装依赖yum install python-setuptools-devel 2. 安装supervisor ...