1. 使用KNN进行电影类型预测:
给定训练样本集合如下:
求解:testData={"老友记": [29, 10, 2, "?片"]}。
解题步骤:
1.计算一个新样本与数据集中所有数据的距离
2.按照距离大小进行递增排序
3.选取距离最小的k个样本
4.确定前k个样本所在类别出现的频率,并输出出现频率最高的类别
import numpy as np
def createDataset():
'''
创建训练集,特征值分别为搞笑镜头、拥抱镜头、打斗镜头的数量
'''
learning_dataset = {"宝贝当家": [45, 2, 9, "喜剧片"],
"美人鱼": [21, 17, 5, "喜剧片"],
"澳门风云3": [54, 9, 11, "喜剧片"],
"功夫熊猫3": [39, 0, 31, "喜剧片"],
"谍影重重": [5, 2, 57, "动作片"],
"叶问3": [3, 2, 65, "动作片"],
"伦敦陷落": [2, 3, 55, "动作片"],
"我的特工爷爷": [6, 4, 21, "动作片"],
"奔爱": [7, 46, 4, "爱情片"],
"夜孔雀": [9, 39, 8, "爱情片"],
"代理情人": [9, 38, 2, "爱情片"],
"新步步惊心": [8, 34, 17, "爱情片"]}
return learning_dataset
def kNN(learning_dataset,dataPoint,k):
'''
kNN算法,返回k个邻居的类别和得到的测试数据的类别
'''
# s1:计算一个新样本与数据集中所有数据的距离
disList=[]
for key,v in learning_dataset.items():
#对距离进行平方和开根号
d=np.linalg.norm(np.array(v[:3])-np.array(dataPoint))
#round四舍五入保留两位小数,并添加到集合中
disList.append([key,round(d,2)])
# s2:按照距离大小进行递增排序
disList.sort(key=lambda dis: dis[1]) # 常规排序方法,熟悉key的作用
# s3:选取距离最小的k个样本
disList=disList[:k]
# s4:确定前k个样本所在类别出现的频率,并输出出现频率最高的类别
labels = {"喜剧片":0,"动作片":0,"爱情片":0}
#从k个中进行统计哪个类别标签最多
for s in disList:
#取出对应标签
label = learning_dataset[s[0]]
labels[label[len(label)-1]] += 1
labels =sorted(labels.items(),key=lambda asd: asd[1],reverse=True)
return labels,labels[0][0]
if __name__ == '__main__':
learning_dataset=createDataset()
testData={"老友记": [29, 10, 2, "?片"]}
dataPoint=list(testData.values())[0][0:3]
k=6
labels,result=kNN(learning_dataset,dataPoint,k)
print(labels,result,sep='\n')
结果为喜剧片!
2. 编写代码,实现对iris数据集的KNN算法分类及预测
要求:
(1)数据集划分为测试集占20%;
(2)n_neighbors=5;
(3)评价模型的准确率;
(4)使用模型预测未知种类的鸢尾花。
(待预测数据:X1=[[1.5 , 3 , 5.8 , 2.2], [6.2 , 2.9 , 4.3 , 1.3]])
iris数据集有150组,每组4个数据。
第一步:引入所需库
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
第二步:划分测试集占20%
x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.2, random_state=0)
test_size为0-1的数代表占百分之几
random_state为零随机数确定,每次结果都相同
第三步:n_neighbors=5
KNeighborsClassifier(n_neighbors=5)
第四步:评价模型的准确率
KNN.fit(x_train, y_train)
# 训练集准确率
train_score = KNN.score(x_train, y_train)
# 测试集准确率
test_score = KNN.score(x_test, y_test)
第五步:使用模型预测未知种类的鸢尾花
#待预测数据:X1=[[1.5 , 3 , 5.8 , 2.2], [6.2 , 2.9 , 4.3 , 1.3]]
X1 = np.array([[1.5, 3, 5.8, 2.2], [6.2, 2.9, 4.3, 1.3]])
# 进行预测
prediction = KNN.predict(X1)
# 种类名称
k = iris.get("target_names")[prediction]
完整代码:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
if __name__ == '__main__':
iris = load_iris()
data = iris.get("data")
target = iris.get("target")
x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.2, random_state=0)
KNN = KNeighborsClassifier(n_neighbors=5)
KNN.fit(x_train, y_train)
train_score = KNN.score(x_train, y_train)
test_score = KNN.score(x_test, y_test)
print("模型的准确率:", test_score)
X1 = np.array([[1.5, 3, 5.8, 2.2], [6.2, 2.9, 4.3, 1.3]])
prediction = KNN.predict(X1)
k = iris.get("target_names")[prediction]
print("第一朵花的种类为:", k[0])
print("第二朵花的种类为:", k[1])
结果: