[深度学习] (sklearn)多层感知机对葡萄酒的分类

时间:2021年12月2日

from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler

import pandas as pd

# 导入数据
wine = load_wine()
# 划分训练集与测试集
x_train, x_test, y_train, y_test = train_test_split(wine.data, wine.target, test_size=0.3, stratify=wine.target)
# 显示数据
frame = pd.DataFrame(x_train)
frame.columns = wine.feature_names
print(frame.head())

# 多层感知机,设置隐含层为1层100
model = MLPClassifier(hidden_layer_sizes=(100,))
model.fit(x_train, y_train)
predict_y = model.predict(x_test)
# 测试精度
score = accuracy_score(y_test, predict_y)
print(f"第一次测试的精度为:{score}")

# 使用标准预处理模块预处理数据,将原始数据归一化
scaler = StandardScaler()
fit = scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
# 再次训练并预测
model.fit(x_train, y_train)
predict_y = model.predict(x_test)
score = accuracy_score(y_test, predict_y)
print(f"第二次测试的精度为:{score}")

你可能感兴趣的:(深度学习,深度学习)