python 机器学习 交叉验证train_test_split 网格搜索GridSearchCV

交叉验证 用于在数据集中拆分成测试集和验证集,用于模型训练和验证

网格搜索 用于自动查找最合适的模型参数,用于模型调优

复制代码
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import accuracy_score
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from sklearn.preprocessing import StandardScaler
import seaborn as sns
import pandas as pd

matplotlib.use('TkAgg')

iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)

print(X_test)
X_test = scaler.transform(X_test)
print(X_test)

grid_params = {
    'n_neighbors': [3, 5, 7, 9],
    'weights': ['uniform', 'distance'],
    'metric': ['euclidean', 'manhattan']
}
knn = KNeighborsClassifier(n_neighbors=3)
grid_search = GridSearchCV(knn, grid_params, cv=5)
grid_search.fit(X_train, y_train)
print("最佳参数:", grid_search.best_params_)
print("最佳交叉验证得分:", grid_search.best_score_)
print("测试集准确率:", grid_search.score(X_test, y_test))


# plt.figure(figsize=(10, 6))
# plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred, cmap='viridis')
# plt.xlabel('Sepal length')
# plt.ylabel('Sepal width')
# plt.title('KNN Classification')
# plt.show()
#
# print(accuracy_score(y_test, y_pred))
# print(cross_val_score(knn, iris.data, iris.target, cv=5))

F:\Users\FRT\anaconda3\envs\test\python.exe E:\PycharmProjects\pythonProject\3.py

\[6.1 2.8 4.7 1.2

5.7 3.8 1.7 0.3

7.7 2.6 6.9 2.3

6. 2.9 4.5 1.5

6.8 2.8 4.8 1.4

5.4 3.4 1.5 0.4

5.6 2.9 3.6 1.3

6.9 3.1 5.1 2.3

6.2 2.2 4.5 1.5

5.8 2.7 3.9 1.2

6.5 3.2 5.1 2.

4.8 3. 1.4 0.1

5.5 3.5 1.3 0.2

4.9 3.1 1.5 0.1

5.1 3.8 1.5 0.3

6.3 3.3 4.7 1.6

6.5 3. 5.8 2.2

5.6 2.5 3.9 1.1

5.7 2.8 4.5 1.3

6.4 2.8 5.6 2.2

4.7 3.2 1.6 0.2

6.1 3. 4.9 1.8

5. 3.4 1.6 0.4

6.4 2.8 5.6 2.1

7.9 3.8 6.4 2.

6.7 3. 5.2 2.3

6.7 2.5 5.8 1.8

6.8 3.2 5.9 2.3

4.8 3. 1.4 0.3

4.8 3.1 1.6 0.2

4.6 3.6 1. 0.2

5.7 4.4 1.5 0.4

6.7 3.1 4.4 1.4

4.8 3.4 1.6 0.2

4.4 3.2 1.3 0.2

6.3 2.5 5. 1.9

6.4 3.2 4.5 1.5

5.2 3.5 1.5 0.2

5. 3.6 1.4 0.2

5.2 4.1 1.5 0.1

5.8 2.7 5.1 1.9

6. 3.4 4.5 1.6

6.7 3.1 4.7 1.5

5.4 3.9 1.3 0.4

5.4 3.7 1.5 0.2\]

\[ 0.3100623 -0.50256349 0.484213 -0.05282593

-0.17225683 1.89603497 -1.26695916 -1.27039917

2.23933883 -0.98228318 1.76840592 1.43531914

0.18948252 -0.26270364 0.36746819 0.35303182

1.15412078 -0.50256349 0.54258541 0.2177459

-0.53399618 0.93659559 -1.38370397 -1.13511325

-0.29283662 -0.26270364 -0.15788346 0.08245999

1.27470056 0.21701605 0.71770262 1.43531914

0.43064208 -1.94172256 0.36746819 0.35303182

-0.05167705 -0.74242333 0.01723376 -0.05282593

0.79238143 0.4568759 0.71770262 1.0294614

-1.25747488 -0.02284379 -1.44207638 -1.540971

-0.4134164 1.17645543 -1.50044878 -1.40568508

-1.1368951 0.21701605 -1.38370397 -1.540971

-0.89573553 1.89603497 -1.38370397 -1.27039917

0.55122187 0.69673574 0.484213 0.48831773

0.79238143 -0.02284379 1.12630946 1.30003323

-0.29283662 -1.22214302 0.01723376 -0.18811184

-0.17225683 -0.50256349 0.36746819 0.08245999

0.67180165 -0.50256349 1.00956465 1.30003323

-1.37805466 0.4568759 -1.32533157 -1.40568508

0.3100623 -0.02284379 0.60095781 0.75888956

-1.01631531 0.93659559 -1.32533157 -1.13511325

0.67180165 -0.50256349 1.00956465 1.16474731

2.4804984 1.89603497 1.47654389 1.0294614

1.033541 -0.02284379 0.77607503 1.43531914

1.033541 -1.22214302 1.12630946 0.75888956

1.15412078 0.4568759 1.18468187 1.43531914

-1.25747488 -0.02284379 -1.44207638 -1.27039917

-1.25747488 0.21701605 -1.32533157 -1.40568508

-1.49863445 1.41631528 -1.675566 -1.40568508

-0.17225683 3.33519404 -1.38370397 -1.13511325

1.033541 0.21701605 0.30909579 0.2177459

-1.25747488 0.93659559 -1.32533157 -1.40568508

-1.73979401 0.4568759 -1.50044878 -1.40568508

0.55122187 -1.22214302 0.65933022 0.89417548

0.67180165 0.4568759 0.36746819 0.35303182

-0.77515575 1.17645543 -1.38370397 -1.40568508

-1.01631531 1.41631528 -1.44207638 -1.40568508

-0.77515575 2.6156145 -1.38370397 -1.540971

-0.05167705 -0.74242333 0.71770262 0.89417548

0.18948252 0.93659559 0.36746819 0.48831773

1.033541 0.21701605 0.484213 0.35303182

-0.53399618 2.13589481 -1.50044878 -1.13511325

-0.53399618 1.65617512 -1.38370397 -1.40568508\]

最佳参数: {'metric': 'euclidean', 'n_neighbors': 3, 'weights': 'uniform'}

最佳交叉验证得分: 0.9428571428571428

测试集准确率: 1.0

Process finished with exit code 0

相关推荐
AlanAshley1 小时前
无 SIM 卡也能打 110?手机紧急呼叫能力的软硬件技术探究
开发语言·智能手机·php
modelmd1 小时前
Go 切片、映射常见使用场景
开发语言·后端·golang
dialect_9 小时前
KMP算法
开发语言·c++·算法
总要学点什么10 小时前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
前端精髓10 小时前
结构化输出with_structured_output(抽卡)
python·langchain
尹人入圣11 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
开开心心_Every12 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
chenbingjie_c12 小时前
C++ 多态完全指南:从虚函数重写到虚函数表原理
开发语言·c++
Nuanyt12 小时前
Agent 学习笔记(下篇三):上线工程、进阶选择与操作界面(Stage 7 / 7.5 / 8 + 延伸路线)
笔记·python·学习·memory·aiagent·harness