当前位置: 首页>后端>正文

使用python进行机器学习的数据预处理代码实践

一天的时间,实践了这么多,快~

一,代码

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import RobustScaler
from sklearn.preprocessing import Normalizer
from sklearn.datasets import make_blobs
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split

wine = load_wine()
X_train, X_test, y_train, y_test = train_test_split(
    wine.data, wine.target, random_state=62
)
print(X_train.shape, X_test.shape)
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train_pp = scaler.transform(X_train)
X_test_pp = scaler.transform(X_test)
mlp = MLPClassifier(hidden_layer_sizes=[100, 100], max_iter=400,random_state=62)
mlp.fit(X_train_pp, y_train)
print('模型得分: {:.2f}'.format(mlp.score(X_test_pp, y_test)))

'''

X, y = make_blobs(n_samples=40, centers=2, random_state=50, cluster_std=2)
X_1 = StandardScaler().fit_transform(X)
X_2 = MinMaxScaler().fit_transform(X)
X_3 = RobustScaler().fit_transform(X)
X_4 = Normalizer().fit_transform(X)
plt.scatter(X_4[:, 0], X_4[:, 1], c=y, cmap=plt.cm.cool)
plt.show()
'''

二,效果

C:\Users\ccc\AppData\Local\Programs\Python\Python38\python.exe D:/Code/Metis-Org/app/service/time_series_detector/algorithm/ai_test.py
(133, 13) (45, 13)
模型得分: 1.00

Process finished with exit code 0

使用python进行机器学习的数据预处理代码实践,第1张
2022-04-26 17_51_55-.png

https://www.xamrdz.com/backend/3dz1895794.html

相关文章: