pandas 数据分析 相关性_Python实战:红酒数据分析

论坛 期权论坛     
选择匿名的用户   2021-5-28 02:16   12   0
<div>
<p></p>
<div style="text-align:center;">
  <img alt="a578c5f5fffa8cf838bc9bb8439402ee.png" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-7ec177b4fd31ece7124dd3fd01ca8355.png">
</div>
<p>本文运用常见的Python包对红酒的通用数据集进行分析和可视化展示。这个数据集有1599个样本,12个变量,其中11个变量是红酒的理化性质,1个变量是红酒的品质评分(10分制)。首先我们需要调用要使用的包,也可以做一些基本绘图设置,然后就可以读取数据进行数据分析。</p>
<div class="blockcode">
  <pre class="blockcode"><code># 导入需要用到的包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pandas_profiling

# 在notebook中画图
%matplotlib inline

# 精度设定
pd.set_option(&#39;precision&#39;,3)

# 颜色设定
color&#61; sns.color_palette()

# 数据导入
data&#61;pd.read_csv(&#34;E:awinequality-red.csv&#34;,sep&#61;&#39;;&#39;)
data.head(5)</code></pre>
</div>
<p>结果输出如下:</p>
<div class="blockcode">
  <pre class="blockcode"><code>  fixed acidity  volatile acidity  citric acid  ...  sulphates  alcohol  quality
0            7.4              0.70         0.00  ...       0.56      9.4        5
1            7.8              0.88         0.00  ...       0.68      9.8        5
2            7.8              0.76         0.04  ...       0.65      9.8        5
3           11.2              0.28         0.56  ...       0.58      9.8        6
4            7.4              0.70         0.00  ...       0.56      9.4        5

[5 rows x 12 columns]</code></pre>
</div>
<h2>一、使用pandas-profiling进行探索性数据分析</h2>
<p>Pandas-profiling是数据质量探索的主要工具包,主要用来研究各个单变量数据的分布以及变量之间的相关关系分析。它首先对数据简单进行了一个统计性描述,会提示每个变量的缺失值情况,然后其中rejected表示由于和其他的变量之间的相关性很高,可以剔除该变量。此外还会告诉每个变量的分布。</p>
<div class="blockcode">
  <pre class="blockcode"><code># 数据探索html报告
profile &#61; pandas_profiling.ProfileReport(data)
profile.to_file(&#34;winequality-red_output_file.html&#34;)</code></pre>
</div>
<p></p>
<div style="text-align:center;">
  <img alt="0f5e4e46d77906103772d9a0cea6ff5b.png" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8d3e6833fb0d146bc6d7692aa641a5cb.png">
</div>
<figcaption>
  探索性数据分析HTML报告
</figcaption>
<p>从报告结果来看,数据没有缺失值,12个变量均是数值型变量。同时每个变量均有详细的描述性统计和分布可视化展示,红酒品质变量(quality)数据集中范围是3到8,绝大部分的红酒品质是5或6,其他变量分布这里不进行详细的描述。从相关性分析来看,红酒的品质(quality)与酒精(alcohol)、挥发性酸(volatile acidity)相关性最高。</p>
<p>我们也可以做个性化的单变量、双变量、多变量分析来深入探索这个数据,具体见下面三小节。</p>
<h2><b>二、单变量分析</b></h2>
<p>1、样本概况及简单描述性统计</p>
<p>首先通过.info()查看数据集变量的数据类型及样本概况,通过.describe()进行简单的描述性统计。</p>
<div class="blockcode">
  <pre class="blockcode"><code># 数据概览
data.info()</code></pre>
</div>
<p>结果输出如下:</p>
<div class="blockcode">
  <pre class="blockcode"><code>&lt;class &#39;pandas.core.frame.DataFrame&#39;&gt;
RangeIndex: 1599 entries, 0 to 1598
Data columns (total 12 columns):
fixed acidity           1599 non-null float64
volatile acidity        1599 non-null float64
citric acid             1599 non-null float64
residual sugar          1599 non-null float64
chlorides               1599 non-null float64
free sulfur dioxide     1599 non-null float64
total sulfur dioxide    1599 non-null float64
density                 1599 non-null float64
pH                      1599 non-null float64
sulphates               1599 non-null float64
alcohol                 1599 non-null float64
quality                 1599 non-null int64
dtypes: float64(11), int64(1)
memory usage: 150.0 KB</code></pre>
</div>
<p>描述性统计:</p>
<div class="blockcode">
  <pre class="blockcode"><code># 简单的数据统计
data.describe()</code></pre>
</div>
<p>结果输出如下:</p>
<div class="blockcode">
  <pre class="blockcode"><code> fixed acidity  volatile acidity  ...   alcohol   quality
count       1599.000          1599.000  ...  1599.000  1599.000
mean           8.320             0.528  ...    10.423     5.636
std            1.741             0.179  ...     1.066     0.808
min            4.600             0.120  ...     8.400     3.000
25%            7.100             0.390  ...     9.500     5.000
50%            7.900             0.520  ...    10.200     6.000
75%            9.200             0.640  ...    11.100     6.000
max           15.900             1.580  ...    14.900     8.000

[8 rows x 12 columns]</code></pre>
</div>
<p>2、单变量分布</p>
<p>探索各个变量分布的箱线图:</p>
<div class="blockcode">
  <pre class="bloc
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP