红葡萄酒质量影响因素分析

1.载入数据,清洗重复数据

data = pd.read_csv('winequality-red.csv')
pd.set_option('display.max_columns', None)
duplicated_data = data[data.duplicated(keep=False)]
print(duplicated_data) # 打印重复数据
打印重复数据
      fixed acidity  volatile acidity  citric acid  residual sugar  chlorides  \
0               7.4             0.700         0.00             1.9      0.076   
4               7.4             0.700         0.00             1.9      0.076   
9               7.5             0.500         0.36             6.1      0.071   
11              7.5             0.500         0.36             6.1      0.071   
22              7.9             0.430         0.21             1.6      0.106   
...             ...               ...          ...             ...        ...   
1567            7.2             0.695         0.13             2.0      0.076   
1579            6.2             0.560         0.09             1.7      0.053   
1581            6.2             0.560         0.09             1.7      0.053   
1592            6.3             0.510         0.13             2.3      0.076   
1596            6.3             0.510         0.13             2.3      0.076   

      free sulfur dioxide  total sulfur dioxide  density    pH  sulphates  \
0                    11.0                  34.0  0.99780  3.51       0.56   
4                    11.0                  34.0  0.99780  3.51       0.56   
9                    17.0                 102.0  0.99780  3.35       0.80   
11                   17.0                 102.0  0.99780  3.35       0.80   
22                   10.0                  37.0  0.99660  3.17       0.91   
...                   ...                   ...      ...   ...        ...   
1567                 12.0                  20.0  0.99546  3.29       0.54   
1579                 24.0                  32.0  0.99402  3.54       0.60   
1581                 24.0                  32.0  0.99402  3.54       0.60   
1592                 29.0                  40.0  0.99574  3.42       0.75   
1596                 29.0                  40.0  0.99574  3.42       0.75   

      alcohol  quality  
0         9.4        5  
4         9.4        5  
9        10.5        5  
11       10.5        5  
22        9.5        5  
...       ...      ...  
1567     10.1        5  
1579     11.3        5  
1581     11.3        5  
1592     11.0        6  
1596     11.0        6  

考虑到quality(质量)评分是不同的评酒师给出的评分,因此重复值应当保留。

2.查找空值

def check_na(data_para):
    print(data_para.isnull().sum())
    missing_col = data_para.columns[data_para.isnull().any()].tolist()
    print(missing_col)
check_na(data)

结果打印:

--------查找空值---------
fixed acidity           0
volatile acidity        0
citric acid             0
residual sugar          0
chlorides               0
free sulfur dioxide     0
total sulfur dioxide    0
density                 0
pH                      0
sulphates               0
alcohol                 0
quality                 0

所有特征列的空值数均为0,即不存在空值

3.观察数据大致情况描述

       fixed acidity  volatile acidity  citric acid  residual sugar  \
count    1359.000000       1359.000000  1359.000000     1359.000000   
mean        8.310596          0.529478     0.272333        2.523400   
std         1.736990          0.183031     0.195537        1.352314   
min         4.600000          0.120000     0.000000        0.900000   
25%         7.100000          0.390000     0.090000        1.900000   
50%         7.900000          0.520000     0.260000        2.200000   
75%         9.200000          0.640000     0.430000        2.600000   
max        15.900000          1.580000     1.000000       15.500000   

         chlorides  free sulfur dioxide  total sulfur dioxide      density  \
count  1359.000000          1359.000000           1359.000000  1359.000000   
mean      0.088124            15.893304             46.825975     0.996709   
std       0.049377            10.447270             33.408946     0.001869   
min       0.012000             1.000000              6.000000     0.990070   
25%       0.070000             7.000000             22.000000     0.995600   
50%       0.079000            14.000000             38.000000     0.996700   
75%       0.091000            21.000000             63.000000     0.997820   
max       0.611000            72.000000            289.000000     1.003690   

                pH    sulphates      alcohol      quality  
count  1359.000000  1359.000000  1359.000000  1359.000000  
mean      3.309787     0.658705    10.432315     5.623252  
std       0.155036     0.170667     1.082065     0.823578  
min       2.740000     0.330000     8.400000     3.000000  
25%       3.210000     0.550000     9.500000     5.000000  
50%       3.310000     0.620000    10.200000     6.000000  
75%       3.400000     0.730000    11.100000     6.000000  
max       4.010000     2.000000    14.900000     8.000000  

从上述数据粗略可知,
1.该葡萄酒的quality(质量)评分在3-8之间,平均分为5.6,中位数为6,说明该批葡萄酒的整体质量处于中等水平。
2.ph值处于2.7-4.0之间,density(密度)也维持在1之间,alcohol(酒精)等数据也在合理范围内,另外所有数据均没有负数,初步说明没有录入错误的异常值。

4.直方图观测

data.hist(figsize=(10, 10),bins=30, alpha=0.5, density=True,grid=True,layout=(4, 3))
plt.show()

在这里插入图片描述

从直方图可知,
1.大部分特征项处于右偏态,即大部分特征的平均数>中位数>众数
2.质量(quality)评分多在5、6之间

5.偏度、峰度

# 偏度
print("偏度\n", data.skew().sort_values(ascending=False))
# 峰度
print("峰度\n", data.kurt().sort_values(ascending=False))
# 极值
print("极值\n", (data.max() - data.min()).sort_values(ascending=False))

打印信息:

偏度
chlorides               5.680347
residual sugar          4.540655
sulphates               2.428672
total sulfur dioxide    1.515531
free sulfur dioxide     1.250567
fixed acidity           0.982751
alcohol                 0.860829
volatile acidity        0.671593
citric acid             0.318337
quality                 0.217802
pH                      0.193683
density                 0.071288
峰度
chlorides               41.715787
residual sugar          28.617595
sulphates               11.720251
total sulfur dioxide     3.809824
free sulfur dioxide      2.023562
volatile acidity         1.225542
fixed acidity            1.132143
density                  0.934079
pH                       0.806943
quality                  0.296708
alcohol                  0.200029
citric acid             -0.788998
极值
total sulfur dioxide    283.00000
free sulfur dioxide      71.00000
residual sugar           14.60000
fixed acidity            11.30000
alcohol                   6.50000
quality                   5.00000
sulphates                 1.67000
volatile acidity          1.46000
pH                        1.27000
citric acid               1.00000
chlorides                 0.59900
density                   0.01362

1.从偏度可更加精确得知,所有数据均不服从正态分布。其中右偏最严重的是chlorides(氯化物),其次分别是residual sugar(剩余糖分)、sulphates(硫酸盐)、total sulfur dioxide(总二氧化硫)、free sulfur dioxide(游离二氧化硫)。
2.从峰度可知,chlorides(氯化物)、residual sugar(剩余糖分)、sulphates(硫酸盐)峰值较高,数据较为集中;而citric acid(柠檬酸)峰值为负数,数据较为分散。
3.结合峰度和偏度,可以看出pH、density、quality接近正态分布
4.观察极值,可得出total sulfur dioxide极值最大,其次是free sulfur dioxide、residual sugar、fixed acidity。结合直方图以及数据的大致情况描述,可知该四项存在一些远大于均值的异常值。

6.将数据进行归一化

# 归一化后的箱线图
columns = ["fixed acidity", "volatile acidity", "citric acid", "residual sugar", "chlorides", "free sulfur dioxide", "total sulfur dioxide",
           "density", "pH", "sulphates", "alcohol", "quality"]
plt.boxplot(scaled_data,
            showmeans=True, meanprops={"marker": "D", "markerfacecolor": "red"},
            labels=columns
            )
plt.xticks(rotation=15)
plt.show()

在这里插入图片描述
(1)residual sugar、chlorides箱体较扁,说明50%的数据在均值附近波动。且存在大量异常值,多处于上方,数据呈右偏趋势,偏离均值分布。
(2)citric acid、quality、alcohel的异常值较少

7.相关性分析

# 相关性分析
plt.figure(figsize=(13, 13))
corr_data = data.corr()
sns.heatmap(corr_data,linewidths=0.1,cmap='YlGnBu',annot=True)
plt.xticks(rotation=15)
plt.show()

在这里插入图片描述

1.与quality(质量)评分存在正相关的有fixed acidity(非挥发性酸)、citric acid(柠檬酸)、residual sugar(剩余糖分)、sulphates(硫酸盐)、alcohol(酒精),其中alcohol(酒精)sulphates(硫酸盐)、**citric acid(柠檬酸)正相关程度相对其他特征较大,分别为0.48、0.25、0.23,说明其比率越高,评分越高;
而volatile acidity(挥发性酸度)、chlorides(氯化物)、free sulfur dioxide(游离二氧化硫)、total sulfur dioxide(总二氧化硫)、density(密度)、ph呈负相关关系,其中
volatile acidity(挥发性酸度)**负相关程度最大,为-0.39;而free sulfur dioxide、ph负相关程度最小,可忽略。
2.对于alcohol、sulphates、citric acid这三个中对quality具有积极影响且程度较大的做进一步探究,可以发现alcohol与density具有相对较大的负相关关系(-为0.5)。而sulphates与chlorides又具有0.37的正相关关系,但又因为chlorides对quality有-0.13的负相关关系。因此对于sulphates的比率要保持于一个适中的水平。
对于citric acid,其与fixed acidity(非挥发性酸)有0.67的正相关关系,程度较高;而与volatile acidity跟pH具有负相关关系,负相关关系也比较高,因为可以考虑适当提升fixed acidity的占比,降低volatile acidity跟pH的占比。

总的来说,可提高着重提高alcohol(酒精)sulphates(硫酸盐)、**citric acid(柠檬酸)的比例,降低volatile acidity(挥发性酸度)**的比例。除此之外,fixed acidity(非挥发性酸)也可稍微增加,因为其对citric acid(柠檬酸)的具有正相关关系。其余的特征变量影响相对较小。

  • 0
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值