knn

Find K nearest neighbors, starting from a distance matrix

断了今生、忘了曾经 提交于 2019-11-26 08:30:25
问题 I\'m looking for a well-optimized function that accepts an n X n distance matrix and returns an n X k matrix with the indices of the k nearest neighbors of the ith datapoint in the ith row. I find a gazillion different R packages that let you do KNN, but they all seem to include the distance computations along with the sorting algorithm within the same function. In particular, for most routines the main argument is the original data matrix, not a distance matrix. In my case, I\'m using a

Changing shape of single point in JFreeChart XYPLot

孤街醉人 提交于 2019-11-26 06:07:58
问题 I am using JFreeChart XYPLot for plotting a XYData set with different labels . I have created different XYSeries objects for different labels so that I can have different colors for different labels . Now I need to require the change the shapes of specific points(test data) in each XYDataSeries as below . In the above plotting , there are two different XYSeries with blue and red color . Out of these two I need to change the shapes of some points(test data) to X instead of circle . Is it

每周一道算法题013:电影推荐

孤人 提交于 2019-11-26 01:03:53
问题: A、B、C三位用户都喜欢看电影,他们给自己所喜欢的电影类型打了如下的分: A B C 喜剧片 3 4 2 动作片 4 3 5 生活片 4 5 1 恐怖片 1 1 3 爱情片 4 5 1 B用户喜欢的电影类型是应该推荐给A还是C? 思路: 用K最近邻(k-nearest neighbours,KNN)算法来解决 找到与B最近的点,如果是A就推荐给A,是C就推荐给C 解答: php: <?php $A = array(3, 4, 4, 1, 4); $B = array(4, 3, 5, 1, 5); $C = array(2, 5, 1, 3, 1); // K最近邻(k-nearest neighbours,KNN) function KNN($a, $b) { $lenA = count($a); $lenB = count($b); $len = min($lenA, $lenB); $sum = 0; for ($i = 0; $i < $len; $i++) { $sum += pow($a[$i] - $b[$i], 2); } return sqrt($sum); } echo KNN($B, $A); echo "\n"; echo KNN($B, $C); echo "\n"; 输出: 2 6.6332495807108 golang: package

Sklearn实现knn

只谈情不闲聊 提交于 2019-11-25 19:09:24
#----------------------------------Sklearn实现Knn------------------------------ # KNeighborsClassifier(n_neighbors=5, weights=’uniform’, # algorithm=’auto’, leaf_size=30, p=2, metric=’minkowski’, # metric_params=None, n_jobs=None, **kwargs)[source] #--------------------------------------实例一----------------------------------------------- from sklearn import datasets from sklearn.neighbors import KNeighborsClassifier from sklearn.cross_validation import train_test_split import numpy as np np.random.seed(0)#设置随机种子,设置后每次产生的随机数都一样 #加载数据集 iris=datasets.load_iris() #样本是150*4二维数据,代表150个样本,每个样本有4个特征 X