第1关 使用sklearn中的kNN算法进行分类
from sklearn.neighbors import KNeighborsClassifier
def classification(train_feature, train_label, test_feature):
'''
使用KNeighborsClassifier对test_feature进行分类
:param train_feature: 训练集数据
:param train_label: 训练集标签
:param test_feature: 测试集数据
:return: 测试集预测结果
'''
#********* Begin *********#
clf = KNeighborsClassifier()
clf.fit(train_feature, train_label)
return clf.predict(test_feature)
#********* End *********#
第2关 使用sklearn中的kNN算法进行回归
from sklearn.neighbors import KNeighborsRegressor
def regression(train_feature, train_label, test_feature):
'''
使用KNeighborsRegressor对test_feature进行分类
:param train_feature: 训练集数据
:param train_label: 训练集标签
:param test_feature: 测试集数据
:return: 测试集预测结果
'''
#********* Begin *********#
clf=KNeighborsRegressor()
clf.fit(train_feature, train_label)
return clf.predict(test_feature)
#********* End *********#
本文转载自: https://blog.csdn.net/liiuyizeliuyize/article/details/129886103
版权归原作者 liuyizeliuyize 所有, 如有侵权,请联系我们删除。
版权归原作者 liuyizeliuyize 所有, 如有侵权,请联系我们删除。