如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别。
两个样本的距离可以通过如下公式计算,又叫欧式距离
还有曼哈顿距离、明科夫斯基距离(欧氏距离、曼哈顿距离都是明科夫斯基距离的一种特殊情况)
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
def knn_iris():
'''
knn预测鸢尾花种类
:return:
'''
# 1.获取数据
iris = load_iris()
# 2.数据集划分
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=6)
# 3.特征工程(标准化)
transfer = StandardScaler()
# x_train,x_test的标准差、平均值得一样
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4.KNN算法预估器(n_neighbors就是K)
estimator = KNeighborsClassifier(n_neighbors=3)
estimator.fit(x_train,y_train) # 完成训练
# 5.模型评估
# 5.1 方法1:直接对比真实值与预测值
y_predict = estimator.predict(x_test)
print('y_predict:\n', y_predict)
print('直接对比真实值与预测值:\n', y_test == y_predict)
# 5.1 方法2:计算准确率
score = estimator.score(x_test, y_test)
print('accuracy:\n', score)
return None
if __name__ == '__main__':
knn_iris()
通常情况下,有很多参数是需要手动指定的(如k-近邻算法中的K值),这种叫**超参数。**但是手动过程繁杂,所以需要对模型预设几种超参数组合。每组超参数都采用交叉验证来进行评估。最后选出最优参数组合建立模型。
鸢尾花数据集
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
def knn_iris():
'''
knn预测鸢尾花种类
:return:
'''
# 1.获取数据
iris = load_iris()
# 2.数据集划分
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=6)
# 3.特征工程(标准化)
transfer = StandardScaler()
# x_train,x_test的标准差、平均值得一样
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4.KNN算法预估器(n_neighbors就是K)
estimator = KNeighborsClassifier(n_neighbors=5)
estimator.fit(x_train, y_train) # 完成训练
# 5.模型评估
# 5.1 方法1:直接对比真实值与预测值
y_predict = estimator.predict(x_test)
print('y_predict:\n', y_predict)
print('直接对比真实值与预测值:\n', y_test == y_predict)
# 5.1 方法2:计算准确率
score = estimator.score(x_test, y_test)
print('accuracy:\n', score)
return None
def knn_iris_gridsearchC():
'''
knn预测鸢尾花种类(使用网格搜索优化超参数)
:return:
'''
# 1.获取数据
iris = load_iris()
# 2.数据集划分
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=6)
# 3.特征工程(标准化)
transfer = StandardScaler()
# x_train,x_test的标准差、平均值得一样
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4.KNN算法预估器
estimator = KNeighborsClassifier()
# 4.1 加入网格搜索与交叉验证
param_dict = {'n_neighbors': [1, 3, 5, 7, 9, 11]}
estimator = GridSearchCV(estimator, param_dict, cv=10)
# 4.2 训练
estimator.fit(x_train, y_train) # 完成训练
# 5.模型评估
# 5.1 方法1:直接对比真实值与预测值
y_predict = estimator.predict(x_test)
print('y_predict:\n', y_predict)
print('直接对比真实值与预测值:\n', y_test == y_predict)
# 5.2 方法2:计算准确率
score = estimator.score(x_test, y_test)
print('accuracy:\n', score)
# 5.3 参数
print('最佳参数:\n', estimator.best_params_)
print('最佳结果:\n', estimator.best_score_)
print('最佳估计器:\n', estimator.best_estimator_)
print('交叉验证结果:\n', estimator.cv_results_)
return None
if __name__ == '__main__':
knn_iris()
# knn_iris_gridsearchC()