对Wine数据集进行数据分类、聚类代码实现
时间: 2024-05-14 13:18:50 浏览: 77
数据挖掘wine数据集分类实验报告及代码
5星 · 资源好评率100%
数据分类代码实现:
```python
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 读取数据集
data = pd.read_csv('wine.csv')
# 划分训练集和测试集
X = data.iloc[:, 1:]
y = data.iloc[:, 0]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# 构建k-NN模型
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
# 预测并计算准确率
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
```
数据聚类代码实现:
```python
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 读取数据集
data = pd.read_csv('wine.csv')
# 构建KMeans模型
kmeans = KMeans(n_clusters=3)
kmeans.fit(data.iloc[:, 1:])
# 可视化聚类结果
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(data.iloc[:, 1], data.iloc[:, 2], data.iloc[:, 3], c=kmeans.labels_)
ax.set_xlabel('Alcohol')
ax.set_ylabel('Malic Acid')
ax.set_zlabel('Ash')
plt.show()
```
以上代码仅供参考,具体实现可以根据自己的需求进行调整。
阅读全文