def kNN_Classifier(slope): group, labels = kNN.createDataset() distances = [] for i in range(len(group)): if group[i] > slope: point_dist = group[i] - slope else: point_dist = slope - group[i] distances.append((point_dist, labels[i])) distances.sort() one = 0 two = 0 for i in range(3): if distances[i][1] == 'one': one = one + 1 else: two = two + 1 if one > two: print "Line 1" else: print "Line 2"
# -*- coding:utf-8 -*- #@Time : 2021/2/19 11:51 #@Author: lyc #@File : kNNtest.py import kNN group, labels = kNN.createDataset() key = kNN.classify0([0, 1.0], group, labels, 3) print(key)
import os import kNN CURRENT_DIR = os.path.dirname(__file__) groups, labels = kNN.createDataset() print kNN.classify0([0,0,0],groups,labels,3) dataSetFile = os.path.join(CURRENT_DIR + '/datingTestSet.txt') datingDataMat,datingLabels = kNN.file2matrix(dataSetFile) print kNN.classify0([40920, 8.326976, 0.953952],groups,labels,3)
import kNN group,lables = kNN.createDataset() print(group) print(lables) a=kNN.classfy0([0,0],group,lables,3) print(a)#жпнд