使用MLPClassifier对葡萄酒数据集进行分类
时间: 2023-12-14 13:38:48 浏览: 86
首先,让我们加载葡萄酒数据集并将其拆分为训练和测试集:
```python
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, test_size=0.3, random_state=42)
```
接下来,我们将导入 `MLPClassifier` 并用默认参数创建模型:
```python
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier()
```
现在,我们可以使用 `fit()` 函数拟合模型:
```python
mlp.fit(X_train, y_train)
```
最后,我们可以使用 `predict()` 函数预测测试集的标签:
```python
y_pred = mlp.predict(X_test)
```
我们可以使用 `accuracy_score` 函数来计算模型在测试集上的准确率:
```python
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))
```
完整代码如下:
```python
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
wine = load_wine()
X_train, X_test, y_train, y_test = train_test_split(wine.data, wine.target, test_size=0.3, random_state=42)
mlp = MLPClassifier()
mlp.fit(X_train, y_train)
y_pred = mlp.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))
```
阅读全文