多元回归分析数据集_白酒数据集分析

本项目主要是利用Python的数据分析相关库对于白酒数据集进行分析,数据集的主要来源是

Wine Quality Data-数据集-阿里云天池​tianchi.aliyun.com
6af5536f8913ab189e6cd099b6641763.png

导入数据分析所需要的库:

%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)

c675db25f5f7dd0b6426974080bf906a.png
df.info()

ad6c9fe2906c05eb6a9b397c8c1e60f7.png
df.describe()

2c5f8a9a35ef5e3b0d67173b2c9be047.png
df['quality'].describe()

#了解一下质量评分的描述性统计的情况

2e77e7116e803e96ea635e95310dd5bc.png

品质评分的范围是0-10, 该数据集的评分范围是3-9。

print(df.isnull().sum())

#可以看出这个数据集没有空值

59873c8451bc9228c4a770a06bb507b6.png

单变量分析

# 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")

1095976791fef10551ba50a7f853ae19.png
#对每个特征变量画出直方图
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')

0fb7eeed76d612939acd1e60bed695db.png

本数据集主要是用来研究白酒的品质和理化性质之间的关系,并建立模型预测白酒品质评分的准确率。品质的评价范围是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')

40971b3bfde4d786c8a0d11f8caad92f.png
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')

bff296ada48bb0204bace1b50f7bd307.png
#总酸度
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")

515265513b39ebf15432073f0724991a.png

甜度(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")

eb003b8791cc342cfd6ef81a2099fca1.png

可以看出大部分的红酒的甜度都不是很高。

双变量分析

白酒和理化性质的关系

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")

b4d67c0c56de734e7948b01fb386084a.png

品质好的酒有更高的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")

8bfae01483bcb848eb97f3ce64478fd6.png

密度和酒精浓度

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")

753694c93837fa386ab4df024f5f1df2.png

酸性物质和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")

fc26a26fab7fc7351325c98d1b66f517.png

多变量分析

与品质相关性最高的三个特征是酒精浓度、挥发性酸度和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")

710a1ecc44561065e56bc0a182cfd03b.png
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")

63f3d46b23a6873aea5ba8bf70003eed.png

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')

f77644d47ac77ba64deb660822793ca4.png

总结

整体来说,白酒的品质主要与酒精浓度、挥发性酸和pH有关。对于品质优于8,或者劣于4的酒,直观上是线性可分的。但是品质为5,6,7的酒很难线性区分。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值