def main(): print 'Main Begin******************' group, labels = knn.createDataSet() print group, '\n', labels predict = [1, 0.9] label = knn.classify0(predict, group, labels, 3) print predict, ' lable is: ', label cp.predict() print 'Main End********************'
# -*- coding: UTF-8 -*- 或者 #coding=utf-8 ''' Created on 2016年8月20日 @author: xiaoyuan ''' import knn group,labels = knn.createDataSet() print knn.classify0([0,0], group, labels, 3)
import knn from numpy import * #生成数据集和类别标签 dataSet,labels = knn.createDataSet() #定义一个未知类别的数据 testX = array([5.9, 3.1, 5.1, 1.8]) k=3 #调用分类函数对未知数据分类 outputLabel = knn.kNNClassify(testX, dataSet, labels, 3) print("Your input is:", testX, " and classified to class:", outputLabel)
import knn group, labels = knn.createDataSet() print(group, labels) print(knn.classify0([0, 0], group, labels, 3))
# coding=utf-8 import knn from numpy import * import matplotlib.pyplot as plt import numpy as np if __name__ == "__main__" : # create the dataset dataSet, labels = knn.createDataSet() print dataSet # set the K value of KNN k = 3 # classify using kNN ## test1 data(1.2, 1.0) testX = array([1.2, 1.0]) outputLabel = knn.kNNClassify(testX, dataSet, labels, k) print "Your input is:", testX, "and classified to class: ", outputLabel ## test1 data(0.1, 0.3) testX = array([0.1, 0.3]) outputLabel = knn.kNNClassify(testX, dataSet, labels, k) print "Your input is:", testX, "and classified to class: ", outputLabel