Python 鸢尾花的识别

from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.datasets import load_iris

load_data = load_iris()

# 归一化
scaler = MinMaxScaler()
load_data.data = scaler.fit_transform(load_data.data)

# 相关系数法
select = SelectKBest(f_classif, k=2)
load_data.data = select.fit_transform(load_data.data, load_data.target)

# 把数据集和标签划分成训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(load_data.data, load_data.target, test_size=0.2)

# 创建KNN分类器
knn = KNeighborsClassifier(n_neighbors=20)

# 代入训练集
knn.fit(x_train, y_train)

# 预测
y_pre = knn.predict(x_test)
total = 0
right = 0
for i in range(y_pre.size):
    total += 1
    if y_test[i] == y_pre[i]:
        right += 1
print("预测正确率为 : {:.2%}".format(right/total))
print(y_pre)
print(y_test)

运行截图:
Python 鸢尾花的识别_第1张图片

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