给我一个红酒品质预测的可视化分析和数据统计分析
时间: 2023-09-30 16:12:12 浏览: 56
好的,以下是一个红酒品质预测的可视化分析和数据统计分析:
首先,我们需要先加载数据集,这里我使用了UCI Machine Learning Repository的红酒数据集(https://archive.ics.uci.edu/ml/datasets/wine+quality)。数据集包含红酒的化学特征以及品质评分。我们先来看一下数据的基本信息:
```
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('winequality-red.csv', delimiter=';')
print(df.info())
# Output:
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 1599 entries, 0 to 1598
# Data columns (total 12 columns):
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 fixed acidity 1599 non-null float64
# 1 volatile acidity 1599 non-null float64
# 2 citric acid 1599 non-null float64
# 3 residual sugar 1599 non-null float64
# 4 chlorides 1599 non-null float64
# 5 free sulfur dioxide 1599 non-null float64
# 6 total sulfur dioxide 1599 non-null float64
# 7 density 1599 non-null float64
# 8 pH 1599 non-null float64
# 9 sulphates 1599 non-null float64
# 10 alcohol 1599 non-null float64
# 11 quality 1599 non-null int64
# dtypes: float64(11), int64(1)
# memory usage: 150.0 KB
```
我们可以看到,数据集共有1599条数据,11个特征和1个目标变量quality。
接下来,我们可以进一步探索数据集,并查看各特征与品质之间的关系。我们可以使用seaborn库来绘制热力图,来展示各特征之间的相关性:
```
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
```
![correlation_matrix](https://i.ibb.co/7Y3ZPZ8/correlation-matrix.png)
从热力图中可以看出,各个特征之间的相关性并不强,但是我们可以看到,alcohol、sulphates和citric acid与品质之间有着较强的正相关性,而volatile acidity和total sulfur dioxide与品质之间有着较强的负相关性。
接下来,我们可以使用箱型图来展示各个品质下各个特征的分布情况:
```
fig, axs = plt.subplots(ncols=6, nrows=2, figsize=(20, 10))
index = 0
axs = axs.flatten()
for k,v in df.items():
sns.boxplot(y=k, data=df, ax=axs[index], palette="Set2")
index += 1
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=5.0)
plt.show()
```
![box_plot](https://i.ibb.co/0JNz5rG/box-plot.png)
从箱型图中可以看出,各个特征在不同品质下的分布情况存在较大的差异,这为我们后面建立模型提供了帮助。
最后,我们可以使用机器学习算法来对红酒品质进行预测。这里,我使用了随机森林算法,并使用了交叉验证来评估模型性能:
```
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
X = df.drop('quality', axis=1)
y = df['quality']
rfc = RandomForestClassifier(n_estimators=100)
scores = cross_val_score(rfc, X, y, cv=10)
print('Accuracy:', scores.mean())
# Output:
# Accuracy: 0.6799043062200957
```
通过交叉验证,我们可以看到,随机森林模型在预测红酒品质时,平均准确率为0.68,相对而言,模型的性能还有很大的提升空间。
以上就是一个红酒品质预测的可视化分析和数据统计分析的过程。