本项目主要是利用Python的数据分析相关库对于白酒数据集进行分析,数据集的主要来源是
Wine Quality Data-数据集-阿里云天池tianchi.aliyun.com导入数据分析所需要的库:
%matplotlib inline
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
color = sns.color_palette()
pd.set_option('precision', 3)
读取数据:
df = pd.read_csv(r"winequality-white.csv",sep = ';')
探索性数据分析
df.head(10)
df.info()
df.describe()
df['quality'].describe()
#了解一下质量评分的描述性统计的情况
品质评分的范围是0-10, 该数据集的评分范围是3-9。
print(df.isnull().sum())
#可以看出这个数据集没有空值
单变量分析
# set plot style
plt.style.use('ggplot')
#对于每个特征变量画出箱型图
colnm = df.columns.tolist()
fig = plt.figure(figsize = (10, 6))
for i in range(12):
plt.subplot(2,6, i+1)
sns.boxplot(df[colnm[i]], orient='v', width = 0.5, color = color[0])
plt.ylabel(colnm[i], fontsize = 12)
plt.tight_layout()
print("nFigure 1 :Univariate Boxplots")
#对每个特征变量画出直方图
colnm = df.columns.tolist()
plt.figure(figsize = (10, 8))
for i in range(12):
plt.subplot(4, 3, i+1)
df[colnm[i]].hist(bins = 100, color = color[0])
plt.xlabel(colnm[i], fontsize = 12)
plt.ylabel('Frequency')
plt.tight_layout()
print('nFigure 2 :Univariate Histograms')
本数据集主要是用来研究白酒的品质和理化性质之间的关系,并建立模型预测白酒品质评分的准确率。品质的评价范围是0-10, 这个数据集主要范围是3到9。
酸度相关的特征
本数据集有7各酸度相关的特征:fixed acidity, volatile acidity, citric acid, free sulfur dioxide, total sulfur dioxide, sulphates,pH。前6个特征都与红酒的pH相关。 pH是对数的尺度,所以对前6个特征取对数然后做histogram。另外,pH值主要是与fixed acidity有关,fixed acidity比volatile acidity和citric acid高1到2个数量级(Figure 4),比free sulfur dioxide, total sulfur dioxide, sulphates高3个数量级。一个新特征total acid来自于前三个特征的和。
acidityFeat = ['fixed acidity', 'volatile acidity', 'citric acid',
'free sulfur dioxide', 'total sulfur dioxide', 'sulphates']
plt.figure(figsize = (10, 4))
for i in range(6):
ax = plt.subplot(2,3,i + 1)
v = np.log10(np.clip(df[acidityFeature[i]].values, a_min = 0.001,
a_max = None))
plt.hist(v, bins = 50, color = color[0])
plt.xlabel('log(' + acidityFeat[i] + ')', fontsize = 12)
plt.ylabel('Frequency')
plt.tight_layout()
print('nFigure 3: Acidity Features in log10 Scale')
plt.figure(figsize = (6,3))
bins = 10 ** (np.linspace(-2, 2))
plt.hist(df['fixed acidity'], bins = bins, edgecolor = 'k', label =
'Fixed Acidity')
plt.hist(df['volatile acidity'], bins = bins, edgecolor = 'k', label =
'Volatile Acidity')
plt.hist(df['citric acid'], bins = bins, edgecolor = 'k', alpha = 0.8,
label = 'Citric Acid')
plt.xscale('log')
plt.xlabel('Acid Concentration (g/dm^3)')
plt.ylabel('Frequency')
plt.title('Histogram of Acid Concentration')
plt.legend()
plt.tight_layout()
print('Figure 4')
#总酸度
df['total acid'] = df['fixed acidity'] + df['volatile acidity'] + df['citric acid']
plt.figure(figsize = (8, 3))
plt.subplot(121)
plt.hist(df['total acid'], bins = 50, color = color[0])
plt.xlabel('total acid')
plt.ylabel('Frequency')
plt.subplot(222)
plt.hist(np.log(df['total acid']), bins = 50, color = color[0])
plt.xlabel('log(total acid)')
plt.ylabel('Frequency')
plt.tight_layout()
print("Figure 5: Total Acid Histogram")
甜度(sweetness)
白酒的甜并不是糖类的甜。白酒的甜味和糖形成的甜味有差别,属甘甜兼有醇厚感和绵柔感,在品尝时常常在呈味感中来得比较迟,呈后味,称“回甜”。干红(<=4 g/L), 半干(4-12 g/L),半甜(12-45 g/L),和甜(>45 g/L)。
df['sweetness'] = pd.cut(df['residual sugar'], bins = [0, 4, 12, 45],
labels=["dry", "medium dry", "semi-sweet"])
plt.figure(figsize = (5,3))
df['sweetness'].value_counts().plot(kind = 'bar', color = color[0])
plt.xticks(rotation=0)
plt.xlabel('sweetness', fontsize = 12)
plt.ylabel('Frequency', fontsize = 12)
plt.tight_layout()
print("Figure 6: Sweetness")
可以看出大部分的红酒的甜度都不是很高。
双变量分析
白酒和理化性质的关系
sns.set_style('ticks')
sns.set_context("notebook", font_scale=1.1)
colnm = df.columns.tolist()[:11] + ['total acid']
plt.figure(figsize = (10, 8))
for i in range(12):
plt.subplot(4,3,i + 1)
sns.boxplot(x = 'quality', y = colnm[i], data = df, color = color[1], width = 0.6)
plt.ylabel(colnm[i], fontsize = 12)
plt.tight_layout()
print("nFigure 7: Physicochemical Properties and Wine Quality by Boxplot")
品质好的酒有更高的PH和酒精度数,更低的挥发性酸、密度、硫酸盐。 其中酒精度数和品质的相关性更高。 柠檬酸、残留糖分、氯离子、二氧化硫似乎对酒的品质影响不大。
画出相关性分析图
sns.set_style("dark")
plt.figure(figsize = (10,8))
colnm = df.columns.tolist()[:11] + ['total acid', 'quality']
mcorr = df[colnm].corr()
mask = np.zeros_like(mcorr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
cmap = sns.diverging_palette(220, 10, as_cmap=True)
g = sns.heatmap(mcorr, mask=mask, cmap=cmap, square=True, annot=True,
fmt='0.2f')
print("nFigure 8: Pairwise Correlation Plot")
密度和酒精浓度
sns.set_style('ticks')
sns.set_context("notebook", font_scale = 1.4)
plt.figure(figsize = (6,4))
sns.regplot(x = 'density', y = 'alcohol', data = df, scatter_kws = {
's':10
}, color = color[1])
plt.xlim(0.986,1.040)
plt.ylim(6.5,16)
print("nFigure 9: Desity vs Alcohol")
酸性物质和pH
pH和非挥发性酸性物质有-0.43的相关性。因为非挥发性酸性物质的含量远远高于其他酸性物质,总酸性物质(total acidity)这个特征并没有太多意义。
acidity_related = ['fixed acidity', 'volatile acidity', 'total sulfur dioxide',
'sulphates', 'total acid']
plt.figure(figsize = (10, 6))
for i in range(5):
plt.subplot(2,3,i + 1)
sns.regplot(x='pH', y = acidity_related[i], data = df, scatter_kws = {
's':10
}, color = color[1])
plt.tight_layout()
print("Figure 10: pH vs acid")
多变量分析
与品质相关性最高的三个特征是酒精浓度、挥发性酸度和pH
#酒精浓度、挥发性酸度和品质
plt.style.use('ggplot')
sns.lmplot(x = 'alcohol', y = 'volatile acidity', hue = 'quality',
data = df, fit_reg = False, scatter_kws={'s':10},
height = 5)
print("Figure 11-1:Scatter Plots of Alcohol, Volatile Acid and Quality")
sns.lmplot(x = 'alcohol', y = 'volatile acidity', col='quality', hue=
'quality',
data = df, fit_reg = False, height = 3, aspect = 0.9, col_wrap=3,
scatter_kws={'s':20})
print("Figure 11-2: Scatter Plots of Alcohol, Volatile Acid and Quality")
pH,非挥发性酸和柠檬酸
#pH和非挥发性酸以及柠檬酸有相关性。整体趋势是浓度越高,pH越低。
sns.set_style('ticks')
sns.set_context("notebook", font_scale = 1.4)
plt.figure(figsize = (6,5))
cm = plt.cm.get_cmap('RdBu')
sc = plt.scatter(df['fixed acidity'],df['citric acid'],c=df['pH'],
vmin=2.6, vmax=4, s=15, cmap=cm)
bar = plt.colorbar(sc)
bar.set_label('pH', rotation = 0)
plt.xlabel('fixed acidity')
plt.ylabel('ctric acid')
plt.xlim(4,18)
plt.ylim(0,1)
print('Figure 12: pH with Fixed Acidity and Citric Acid')
总结
整体来说,白酒的品质主要与酒精浓度、挥发性酸和pH有关。对于品质优于8,或者劣于4的酒,直观上是线性可分的。但是品质为5,6,7的酒很难线性区分。