wine数据集MLP分类并可视化代码
时间: 2023-09-03 11:16:51 浏览: 152
以下是使用MLP对Wine数据集进行分类并可视化的代码示例:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_wine
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
# 加载数据
wine = load_wine()
X = wine.data
y = wine.target
# 数据标准化
scaler = StandardScaler()
X_std = scaler.fit_transform(X)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_std, y, test_size=0.3, random_state=0)
# MLP分类
mlp = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000)
mlp.fit(X_train, y_train)
# 预测测试集结果
y_pred = mlp.predict(X_test)
# 计算准确率
acc = accuracy_score(y_test, y_pred)
print("Accuracy:", acc)
# 可视化分类结果
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_pred)
plt.show()
```
在这个示例中,我们首先加载了Wine数据集并进行了标准化处理。然后我们将数据集划分为训练集和测试集。接下来,我们使用MLPClassifier来训练模型,并使用测试集预测结果。最后,我们计算了模型的准确率,并使用Matplotlib库可视化了分类结果。
阅读全文