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. C# 窗口全屏、置顶、获取焦点

    很简单的几行代码 this.FormBorderStyle = FormBorderStyle.None; //设置窗体为无边框样式 this.WindowState = FormWindowStat ...

  2. 找vector最大最小《转载》

    定义了vector类型的数据,要找到其中的最大最小值,其实在C++中的algorithm头文件下就有直接的函数可以使用: #include <vector> #include <al ...

  3. lowcodeEngine设计器源码简析(创建流程,主要对象), 怎么生成一个左侧面板

    lowcodeEngine 怎么生成一个左侧面板 初始化流程 如何生成一个左侧面板 初始化插件时往skeleton.leftArea新增按钮,新增按钮时往skeleton.leftFloatArea新 ...

  4. 基于DFA算法实现的敏感词过滤

    本文转自浅析敏感词过滤算法(C++),自己也在其基础上根据自己的情况做了一点修改. https://blog.csdn.net/u012755940/article/details/51689401? ...

  5. CSDN上书签迁移

    title: CSDN上书签迁移 date: 2020-11-01 16:34:30 img: /photos/2020.6.03_15/2020_06_11_cover.jpg summary: C ...

  6. 关于sqlyang 连接远程服务器 MySQL "1251-client does not support authentication..."的处理办法

    原因是在mysql8之前的版本中加密规则为mysql_native_password而在mysql8以后的加密规则为caching_sha2_password. 做如下修改 ALTER USER 'r ...

  7. shell之flock

    1.flock 最大的用途就是实现对 crontab 任务的串行化:为了防止crontab 任务出现多实例的情况,导致系统内存被耗尽. 在 crontab 任务中,有可能出现某个任务的执行时间超过了 ...

  8. 无需联网,一键永久激活所有Windows/Office

    对于 Windows 激活工具,大家可能了解不多,熟悉的比如小马激活工具,因为激活工具从来都是病毒高发区,各种工具混在一起,一不小心,电脑就中招了. 今天介绍一款不一样的. R@1n ReBirth ...

  9. TIDB-DM数据迁移第一部(安装部署)

    官方连接: https://docs.pingcap.com/zh/tidb/stable/dm-overview 架构: 1.安装DM download https://tiup-mirrors.p ...

  10. 使用python启动appium(虚拟器)

    1.先安装各种库 https://www.cnblogs.com/zhanglingling00/p/14169462.html pip install Appium-Flutter-Finder p ...