knn算法KNeighborsClassifier实现

import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier


# 加载数据
mov = pd.read_excel('电影分类数据.xlsx')
# print(mov)
train = mov.iloc[:, 1:6]

train.loc[train.loc[:, '电影类型'] == '喜剧片', '类别'] = 0
train.loc[train.loc[:, '电影类型'] == '动作片', '类别'] = 1
train.loc[train.loc[:, '电影类型'] == '爱情片', '类别'] = 2
# print(train)

test = np.array(mov.columns[-4:])
# print(test)

# 取前5个样本
k = 5
# 调用sklearn算法实现knn分类
# 构建
knn = KNeighborsClassifier(n_neighbors=k)

# 进行训练数据
knn.fit(train.iloc[:, 1:4].values, train.iloc[:, -1].values)

# 进行预测  # 训练使用二维数组,预测也应使用二维数组
y_predict = knn.predict([test[1:]])
print(y_predict)

你可能感兴趣的:(机器学习)