NN算法的核心是,欧式距离(Euclid),在分类的数据中,找到与目标数据欧式距离最近的点,把目标点分类到其类,算法很简单,下面是C#代码的实现:

namespace LocationService.Math
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class NN
{
public List<Tuple<string, double[]>> FingerPrintsTable;
public NN()
{
FingerPrintsTable = new List<Tuple<string, double[]>>();
} public void AddFingerPrint(string key,double[] attributes)
{
var finger = Tuple.Create(key,attributes);
AddFingerPrint(finger);
}
public void AddFingerPrint(Tuple<string,double[]> fingerprint)
{
FingerPrintsTable.Add(fingerprint);
} public void AddFingerPrint(IEnumerable<Tuple<string, double[]>> fingers)
{
FingerPrintsTable.AddRange(fingers);
}
public void RemoveFingerPrint(string key)
{
FingerPrintsTable.RemoveAll(fi => fi.Item1 == key);
} public void RemoveFingerPrint(Tuple<string, double[]> fingerprint)
{
FingerPrintsTable.Remove(fingerprint);
} public double EuclideanDistance(double[] x1, double[] x2,int scale=1)
{
double sum=0;
for (int i = 0; i < x1.Length; i++)
{
sum += Math.Pow(x1[i] - x2[i], 2);
}
sum = Math.Sqrt(sum) / x1.Length*scale;
return sum;
} public List<Tuple<string, double>> FingerEuclideanList(Tuple<string, double[]> target)
{
List<Tuple<string, double>> list = new List<Tuple<string, double>>(); foreach (var finger in FingerPrintsTable)
{
list.Add(Tuple.Create(finger.Item1, EuclideanDistance(finger.Item2, target.Item2)));
}
return list;
} /// <summary>
/// Apply the Euclidean distance
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
public Tuple<string,double> ApplyEuclideanFilter(Tuple<string, double[]> target)
{
var list = FingerEuclideanList(target);
list.Sort((x, y) => (int)((x.Item2 - y.Item2) * 100));
foreach (var item in list)
{
this.log("[Label:{0} Distance:{1}]", item.Item1, item.Item2);
}
return list[0];
} }
}

用其他的案例,分类影片类型:

分类使用

NN nN = new NN();
nN.AddFingerPrint("爱情",new double[] {3,104});
nN.AddFingerPrint("爱情", new double[] {2,100 });
nN.AddFingerPrint("爱情", new double[] {1,81});
nN.AddFingerPrint("动作", new double[] {101,10 });
nN.AddFingerPrint("动作", new double[] {99,5});
nN.AddFingerPrint("动作", new double[] {98,2}); Tuple<string, double[]> target = Tuple.Create("未知",new double[] {81,80});
Tuple<string, double> result=null;
this.MeasureTime(()=> {
result = nN.ApplyEuclideanFilter(target); },time=> {
this.log("===================================");
this.log("分类类型:{0} 欧式距离为:{1}", result.Item1, result.Item2);
this.log("total time:{0}ms", time);
this.log("===================================");
});

分类结果如下:

C# NN算法实现的更多相关文章

  1. CNN:人工智能之神经网络算法进阶优化,六种不同优化算法实现手写数字识别逐步提高,应用案例自动驾驶之捕捉并识别周围车牌号—Jason niu

    import mnist_loader from network3 import Network from network3 import ConvPoolLayer, FullyConnectedL ...

  2. 从NLP任务中文本向量的降维问题,引出LSH(Locality Sensitive Hash 局部敏感哈希)算法及其思想的讨论

    1. 引言 - 近似近邻搜索被提出所在的时代背景和挑战 0x1:从NN(Neighbor Search)说起 ANN的前身技术是NN(Neighbor Search),简单地说,最近邻检索就是根据数据 ...

  3. Python基础+Pythonweb+Python扩展+Python选修四大专题 超强麦子学院Python35G视频教程

    [保持在百度网盘中的, 可以在观看,嘿嘿 内容有点多,要想下载, 回复后就可以查看下载地址,资源收集不易,请好好珍惜] 下载地址:http://www.fu83.cc/ 感觉文章好,可以小手一抖 -- ...

  4. 手势估计- Hand Pose Estimation

    http://blog.csdn.net/myarrow/article/details/51933651 1. 目前进展 1.1 相关资料      1)HANDS CVPR 2016      2 ...

  5. Deep learning:四十(龙星计划2013深度学习课程小总结)

    头脑一热,坐几十个小时的硬座北上去天津大学去听了门4天的深度学习课程,课程预先的计划内容见:http://cs.tju.edu.cn/web/courseIntro.html.上课老师为微软研究院的大 ...

  6. Scala - Spark Lambda“goesto“ => 分析

    /// 定义一个函数AddNoise,参数分别为rdd,Fraction.其中rdd为(BreezeDenseMatrix, BreezeDenseMatrix)元组构成的RDD.Fraction为一 ...

  7. SK-Learn 全家福

    SK-Learn API 全家福 最近SK-Learn用的比较多, 以后也会经常用,将Sk-Learn 所有内容整理了一下,整理思路,并可以备查. (高清图片可以用鼠标右键在单独窗口打开,或者保存到本 ...

  8. CS231n 2017 学习笔记01——KNN(K-Nearest Neighbors)

    本博客内容来自 Stanford University CS231N 2017 Lecture 2 - Image Classification 课程官网:http://cs231n.stanford ...

  9. 《Andrew Ng深度学习》笔记1

    深度学习概论 1.什么是神经网络? 2.用神经网络来监督学习 3.为什么神经网络会火起来? 1.什么是神经网络? 深度学习指的是训练神经网络.通俗的话,就是通过对数据的分析与计算发现自变量与因变量的映 ...

  10. javaSE基础(三)

    泛型类:像ArrayList这样的特殊类,他们允许通过类型参数来指明使用的数据类型. 报装类:一种用于将基本类型的数据"封装"成对象的类. 装箱:将 基本类型的数据自动转换为对应类 ...

随机推荐

  1. wireshark作业

    1Wireshark基本操作: 1.启动wireshark,正确选择混杂模式,访问任意网站: 2.设置过滤器呈现本地和该网站服务器之间的交互报文: 3.保存抓包结果文件.cap: 4.在作业纸上记录下 ...

  2. HJ92 在字符串中找出连续最长的数字串

    描述 输入一个字符串,返回其最长的数字子串,以及其长度.若有多个最长的数字子串,则将它们全部输出(按原字符串的相对位置) 本题含有多组样例输入. 输入描述: 输入一个字符串. 输出描述: 输出字符串中 ...

  3. 新装Eclipse运行Java程序报错Exception in thread "main" java.lang.UnsupportedClassVersionError

    错误现象:   Exception in thread "main" java.lang.UnsupportedClassVersionError: views/LoginFram ...

  4. Generative Adversarial Network - Python实现

    算法特征 ①. discriminator区别真假; ②. generator以假乱真 算法推导 Part Ⅰ: 熵之相关定义 entropy之定义如下, \[\begin{equation*} H( ...

  5. springboot gradle 集成流程引擎activiti

    buildscript { repositories { maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } } dep ...

  6. uniapp for显示数据改变时,绑定的list值同时改变

    <template> <view class="container"> <uni-table> <uni-tr v-for="( ...

  7. openwrt 配置 单网卡多IP

    config interface 'wan0' option ifname 'eth1' option proto 'static' option nat '1' option mtu '1500' ...

  8. 虚拟机中Linux分区扩容

    打开Virtualbox所在的安装目录,执行以下命令,命令中的虚拟有磁盘路径改成自己的: 调整容量前,先关闭虚拟机.接着,打开CMD,进入VirtualBox的安装目录,执行VBoxManage li ...

  9. 使用Kali破解无线密码

    1.查看网卡信息,是否有wlanX网卡ifconfig 2.启动网卡监听模式 airmon-ng start wlan0 检查下是否处于监听模型:ifconfig查看一下,如果网卡名加上了mon后则成 ...

  10. Excel下载乱码

    1.前端:一定不可以以ajax的请求方式,不然会弹出乱码. 要使用<a href="../Ajax/AjaxPrint.ashx?action=PrintClick&Tid=& ...