knn

KNN基础代码

守給你的承諾、 提交于 2019-12-02 14:27:27
库 sklearn 库下的工具: datasets,model_selection,neighbors K近邻代码思路: 有个数据集----对数据分割----调用KNN算法 iris = datasets.load_iris() 导入数据集 数据集权重:开源数据集,重要数据集之一 数据集特点:还有3个类别,所以可分类 数据集描述链接:https://archive.ics.uci.edu/ml/datasets/Iris/ x=iris.data y=iris.target x: 数据特征,y: 标签 或 分 y这里3个值。所以适合分类问题(0,1,2) iris: 有150个已知数据,所以 len ( x ) , len ( y ) 都是150 print(x,y) x_train , x_test , y_train , y_test = train_test_split ( x , y , random_state = 2003 ) 分割数据集 x 是 已知数据 ,一共 150 个, 分成训练集 112个 和 测试集 38个 y 同理 目的:训练集 训练模型,测试集 验证模型,否则不知道模型好坏 clf = KNeighborsClassifier ( n_neighbors = 3 ) k近邻算法 ,邻居为3 clf.fit ( x_train , y_train )

Use Euclidean distance in SURF

ぐ巨炮叔叔 提交于 2019-12-02 13:56:23
问题 In my code I'm filtering the good images based on the nearest neigbour distance ratio, as follows: for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) { if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int)matches[i].size()<=2 && (int)matches[i].size()>0)) { good_matches.push_back(matches[i][0]); } } Since I'm filtering the good images based on the nearest neighbor distance ratio, do I need to still do Euclidean distance calculation? And I want to know when I

Q: KNN in R — strange behavior

落花浮王杯 提交于 2019-12-02 13:06:44
Does anyone know why the following KNN R code gives different predictions for different seeds? This is strange as K<-5, and thus the majority is well defined. In addition, the floating numbers are not that small to fall under a precision of data problem. (remark: I know the test is weirdly different from the training. This is only a synthetic example created to demonstrate the strange KNN behavior) library(class) train <- rbind( c(0.0626015, 0.0530052, 0.0530052, 0.0496676, 0.0530052, 0.0626015), c(0.0565861, 0.0569546, 0.0569546, 0.0511377, 0.0569546, 0.0565861), c(0.0538332, 0.057786, 0

KNN算法

时光总嘲笑我的痴心妄想 提交于 2019-12-02 13:00:20
1、概述: 也称为K最近邻算法,原理为搜索最近的k个已知类别样本,用于未知类别样本的预测。 对于分布不均匀的几个样本结果可能会受k取值的影响,通常情况下k值一般取奇数 2、衡量相似性指标方式 : 欧式距离、曼哈顿距离、cos余弦值、杰卡德相似系数等等 3、过程: 确定k 确定样本间相似度的度量指标,形成簇 根据各簇下类别最多的分类作为样本预测点 4、避免k值设定出现过拟合(K值过小)和欠拟合(K值选择过大)现象 对于K值设定过大的情况,可以更改设定权重为距离的倒数。另外一种常用的方式为 多重交叉验证 ,k取不同的值,在每个k值下执行m重交叉验证,最后选定平均误差最小的k值。 5、余弦相似度 杰卡德相似系数(常用于用户推荐算法)值越大相似性越大 以上距离法构建样本时,一是需注意变量的数值化,若某个变量为离散型字符串,需要数值化处理(0,1,2...)。二是防止受数值变量的量纲影响,量纲可能影响距离,必要时需要进行转化,缩小归一化处理。 6、模型运行搜索方法 模型建立好以后常见的几种搜寻方法 暴力搜寻法(未知样本和已知样本的全表扫描)适合小样本数据,for循环迭代2次 KD树搜寻法 球树搜寻法 暴力法搜索对于大样本数据集存在内存消耗大,运行速度慢等问题。 案例1(暴力搜寻法): iris数据集预测,将样本分为2/3的训练解,和1/3的测试集,将测试集置于训练集中训练

Where can I find practical example of KNN in java using weka

▼魔方 西西 提交于 2019-12-02 11:53:38
问题 I have been searching for a practical example of KNN implementation using weka, but all I find is too general for me to understand the data that it needs to be able to work (or maybe how to make the objects that it needs to work) and also the results it shows, maybe someone that has worked with it before has a better example like with realistic things (products, movies, books, etc) and not the typical letters you see on algebra. So I can figure out how to implement it on my case (which is

find the k nearest neighbours of a point in 3d space with python numpy

我们两清 提交于 2019-12-02 09:53:20
I have a 3d point cloud of n points in the format np.array((n,3)). e.g This could be something like: P = [[x1,y1,z1],[x2,y2,z2],[x3,y3,z3],[x4,y4,z4],[x5,y5,z5],.....[xn,yn,zn]] I would like to be able to get the K-nearest neighbors of each point. so for example the k nearest neighbors of P1 might be P2,P3,P4,P5,P6 and the KNN of P2 might be P100,P150,P2 etc etc. how does one go about doing that in python? This can be solved neatly with scipy.spatial.distance.pdist . First, let's create an example array that stores points in 3D space: import numpy as np N = 10 # The number of points points =

Installed package, but getting an error that function can't be found R [duplicate]

若如初见. 提交于 2019-12-02 08:59:41
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Error: could not find function … in R I am trying to use knn function in R and have installed several packages to do so (eg. KNN, KNNgarden, iped). Using R-Studio, it comes across as thepackage is successfully installed (package ‘kknn’ successfully unpacked and MD5 sums checked), but when I try to use kknn (kknn(train, test, cl, k = 1, l = 0, prob = TRUE, use.all = TRUE)) I get the following error: Error: could

Where can I find practical example of KNN in java using weka

此生再无相见时 提交于 2019-12-02 07:05:57
I have been searching for a practical example of KNN implementation using weka, but all I find is too general for me to understand the data that it needs to be able to work (or maybe how to make the objects that it needs to work) and also the results it shows, maybe someone that has worked with it before has a better example like with realistic things (products, movies, books, etc) and not the typical letters you see on algebra. So I can figure out how to implement it on my case (which is recommend dishes to active user with KNN), would be highly appreciated, thanks. I was trying to understand

R's caret training errors when y is not a factor

家住魔仙堡 提交于 2019-12-02 04:52:33
问题 I am using R-studio and am using kaggle's forest cover data and keep getting an error when trying to use the knn3 function in caret. here is my code: library(caret) train <- read.csv("C:/data/forest_cover/train.csv", header=T) trainingRows <- createDataPartition(train$Cover_Type, p=0.8, list=F) head(trainingRows) train_train <- train[trainingRows,] train_test <- train[-trainingRows,] knnfit <- knn3(train_train[,-56], train_train$Cover_Type) This last line gives me this in the console: Error

k-近邻算法(KNN)

馋奶兔 提交于 2019-12-02 03:32:44
采用测量不同特征值之间的距离方法进行分类。 KNN 工作原理 1.假设有一个带有标签的样本数据集(训练样本集),其中包含每条数据与所属分类的对应关系。 2.输入没有标签的新数据后,将新数据的每个特征与样本集中数据对应的特征进行比较。 计算新数据与样本数据集中每条数据的距离。 对求得的所有距离进行排序(从小到大,越小表示越相似)。 取前 k (k 一般小于等于 20 )个样本数据对应的分类标签。 3.求 k 个数据中出现次数最多的分类标签作为新数据的分类。 KNN 开发流程 收集数据:任何方法 准备数据:距离计算所需要的数值,最好是结构化的数据格式 分析数据:任何方法 训练算法:此步骤不适用于 k-近邻算法 测试算法:计算错误率 使用算法:输入样本数据和结构化的输出结果,然后运行 k-近邻算法判断输入数据分类属于哪个分类,最后对计算出的分类执行后续处理 1 from numpy import * 2 import operator 3 4 5 def createDataSet(): 6 group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) 7 labels = ['A','A','B','B'] 8 return group,labels 9 10 def classify0(inX, dataSet, labels, k): 11