#
一个最基本的例子#样本数据的封装feature = [[170,70,42],[166,56,39],[188,90,44],[165,88,40],[170,66,40],[176,80,42],[166,55,37],[155,50,38]]target = ['男','女','男','男','女','男','女','女']from sklearn.neighbors import KNeighborsClassifierknn = KNeighborsClassifier(n_neighbors=3) #k 值knn.fit(feature,target) #试knn.score(feature,target) #打分#分类knn.predict([[167,66,38]]) #调用#其他特征数据(判断男女)# 心率# 血压# 体温
#
导包,机器学习的算法KNN、数据蓝蝴蝶import sklearn.datasets as datasetsimport numpy as npiris = datasets.load_iris() #鸢尾花#提取样本数据feature = iris['data'] # 特征target = iris['target'] # 目标#将样本数据进行随机打乱np.random.seed(1)np.random.shuffle(feature)np.random.seed(1)np.random.shuffle(target)#获取训练样本数据和测试样本数据#提取训练的特征and目标数据x_train = feature[0:140]y_train = target[0:140]#提取测试的特征and目标数据x_test = feature[140:]y_test = target[140:]#实例化模型对象&训练模型knn = KNeighborsClassifier(n_neighbors=11)knn.fit(x_train,y_train)knn.score(x_test,y_test) #分数print('模型的分类结果:',knn.predict(x_test))print('真实的分类结果:',y_test)knn.predict([[8.7, 1.5, 5.8, 0.8]]) #调用函数#