机器学习算法分类
![](https://i-blog.csdnimg.cn/blog_migrate/013c394236e7d265dd94b17fe5412af4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/fb1b5e3d704d8aa9783216e9739f3389.png)
开发流程
![](https://i-blog.csdnimg.cn/blog_migrate/e70e6500b4b99e578124d73cf1d9ca54.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f1247f2e5f227f1ab64e8300c6f0f6f9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4359d865b934e823b3d83c94fc57eabf.png)
K-近邻
![](https://i-blog.csdnimg.cn/blog_migrate/481f79829305e9742bae6dccc04ba77e.png)
API
![](https://i-blog.csdnimg.cn/blog_migrate/697097bc355f8c091c9948de2fb0b494.png)
实例
1、处理鸢尾花数据
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
#读取数据
path = r'D:\python\2023\机器学习\data\iris.csv'
df = pd.read_csv(path)
df.columns = ['type1','type2','type3','type4','target']
#df = load_iris()
#print(df)
#取出目标值及特征值
y = df['target']
x = df.drop('target',axis=1)
print(x)
#将数据进行分为训练集及测试集
x_train,x_test,y_train,y_test = train_test_split(x,y,train_size=0.25)
#特征工程
#数据建模算法
knn = KNeighborsClassifier(n_neighbors=5)
#fit,predict,score
knn.fit(x_train,y_train)
#数据预测
y_predict = knn.predict(x_test)
print('预测结果:',y_predict)
#评估准确率
print('预测的准确率:',knn.score(x_test,y_test))
![](https://i-blog.csdnimg.cn/blog_migrate/673c951962a855f3515825809a413aaf.png)
2、处理葡萄酒数据
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
import pandas as pd
from sklearn.model_selection import train_test_split
path = r'D:\python\2023\机器学习\分类data\winequality-red.csv'
df = pd.read_csv(path)
print(df.isnull().sum())
#取出目标值及特征值
y = df['quality']
x = df.drop('quality',axis=1)
#将数据进行分为训练集及测试集
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.25)
#特征工程
st = StandardScaler()
x_train = st.fit_transform(x_train)
x_test = st.transform(x_test)
#数据建模算法
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(x_train,y_train)
#数据预测
y_predict = knn.predict(x_test)
print('预测结果:',y_predict)
#评估准确率
print('预测的准确率:',knn.score(x_test,y_test))
![](https://i-blog.csdnimg.cn/blog_migrate/136432524fe1d0cb2e891d284702a9f8.png)