sklearn中的pipeline,gridsearch代码实践

2022-05-02  本文已影响0人  万州客

说实话,这一章内容,很有用的。感觉没有完全抓住,只有朦胧的印象,后期要专门要相关知识来巩固才行的。先作好记录吧,以后好抄代码~~

一,代码

import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline

X, y = make_blobs(n_samples=200, centers=2, cluster_std=5)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=38)
'''

scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

print('训练集数据形态:', X_train_scaled.shape)
print('测试集数据形态:', X_test_scaled.shape)

# 原始的训练集
# plt.scatter(X_train[:, 0], X_train[:, 1])
# 经过预处理的训练集
# plt.scatter(X_train_scaled[:, 0], X_train_scaled[:, 1], marker='^', edgecolors='k')
# plt.title('training set and scaled training set')
# plt.show()

params = {'hidden_layer_sizes': [(50, ), (100, ), (100, 100)],
          'alpha': [0.0001, 0.001, 0.01, 0.1]}
grid = GridSearchCV(MLPClassifier(max_iter=1600, random_state=38,),
                    param_grid=params, cv=3)
grid.fit(X_train_scaled, y_train)
print('模型最佳得分:{:.2f}'.format(grid.best_score_))
print('模型最佳参数:{}'.format(grid.best_params_))

print('测试集得分:{}'.format(grid.score(X_test_scaled, y_test)))

pipeline = Pipeline([('scaler', StandardScaler()),
                     ('mlp', MLPClassifier(max_iter=1600, random_state=38))])
pipeline.fit(X_train, y_train)
print('使用管道模型的MLP模型评分:{:.2f}'.format(pipeline.score(X_test, y_test)))
'''

pipeline = Pipeline([('scaler', StandardScaler()),
                     ('mlp', MLPClassifier(max_iter=1600, random_state=38))])

params = {'mlp__hidden_layer_sizes': [(50, ), (100, ), (100, 100)],
          'mlp__alpha': [0.0001, 0.001, 0.01, 0.1]}
grid = GridSearchCV(pipeline, param_grid=params, cv=3)
grid.fit(X_train, y_train)
print('交叉验证最高分:{:.2f}'.format(grid.best_score_))
print('模型最优参数:{}'.format(grid.best_params_))
print('测试集得分:{}'.format(grid.score(X_test, y_test)))
print(pipeline.steps)

二,输出

C:\Users\ccc\AppData\Local\Programs\Python\Python38\python.exe D:/Code/Metis-Org/app/service/time_series_detector/algorithm/ai_test.py
交叉验证最高分:0.88
模型最优参数:{'mlp__alpha': 0.0001, 'mlp__hidden_layer_sizes': (100,)}
测试集得分:0.92
[('scaler', StandardScaler()), ('mlp', MLPClassifier(max_iter=1600, random_state=38))]

Process finished with exit code 0
上一篇下一篇

猜你喜欢

热点阅读