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. linux 高并发系统限制 设置

    linux资源限制配置文件是/etc/security/limits.conf:限制用户进程的数量对于linux系统的稳定性非常重要. limits.conf文件限制着用户可以使用的最大文件数,最大线 ...

  2. java中的ConcurrentModificationException是什么异常?在哪些场景下会报该异常?

    在软件构造实验Lab2的ConcreteVerticesGraph里,需要我们编写remove()方法.移除一个点没有别的方法,只有遍历集合vertices(),找到该点并移除. 当时我没有写上红框中 ...

  3. HCIP-进阶实验03-网络流量路径控制

    HCIP-进阶实验03-网络流量路径控制 实验需求 某城域网网络环境部署规划如图所示,该网络通过OSPF协议进行部署设计,分为四个区域,分别为骨干区域0.普通区域1.2.3.其中普通区域1为特殊区域N ...

  4. C# Thread.Join()作用

    Thread.Join()在MSDN中的解释:Blocks the calling thread until a thread terminates 阻塞calling thread,直到当前join ...

  5. Spring 自定义注解 操作日志

    1.自定义注解 package com.jay.demo3.aop1.myannotation;       import java.lang.annotation.Documented;   imp ...

  6. Docker技术知识点总纲

    基本介绍的安装使用 1.Docker简要介绍 2.windows下Docker的安装 3.CentOS下Docker的安装 快速入门与常用操作 4.Docker引擎升级与存储驱动的升级 5.Docke ...

  7. Java 根据模板导出PDF

    目录 前言 思路一:直接导出pdf 使用itext模板导出pdf 思路二:先导出word再转成pdf 1)导出word 2)word转pdf 最终方案 docx4j spire.doc.free + ...

  8. uniapp 小程序自定义组件样式穿透问题

    1.正在开发时发现自定义组件间样式发生穿透问题 2.主需要引入下面代码可解决 export default { options: { //默认值 isolated(启动隔离) //apply-shar ...

  9. h5 json 生成excel

    引入库文件 创建下载按钮 Download Demo Excel 实现转换 var data = [ ["Joa Doe", "joa@doe.com"], [ ...

  10. Linux常用的操作指令01

    关键字: linux 查进程.杀进程.起进程1.查进程    ps命令查找与进程相关的PID号:    ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程序.   ...