1. 首页 » 生活常识

茎叶图解读与应用统计学分析方法

前言

由于最近一直在学习#Pandas#,有点耽误了统计学的学习,今天尝试结合Pandas做几个应用统计学的练习题。

题目节选自“Introduction to the Practice of Statistics” 9th Edition, 1.2 Displaying Distributions with Graphs的课后习题;

这次练习也应用到了PythonVisualization中的#matplotlib#包,主要用到了设置图形颜色、设置数据标签形状,设置标题和轴标签,图像保存到文件等方法。

1.25 Facebook各个国家的注册用户数

1.25 Your Facebook app can generate a million dollars a month. A report on Facebook suggests that Facebook apps can generate large amounts of money, as much as $1 million a month. The following table gives the numbers of Facebook users by country for the top 10 countries based on the number of users:

(a) Use a bar graph to describe the numbers of users in these countries.

使用柱形图描述每个国家的用户数

(b) Do you think that the United States is an outlier in this data set? Explain your answer.

你是否认为美国的用户数是一个异常值

(c) Describe the major features of your graph in a short paragraph.

请简单描述一下图上的主要指标

1.25 代码实现

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 29 22:26:21 2021

@author: Derek Zhu

"Introduction to the Practice of Statistics" - NINTH EDITION
Written by 
David S. Moore
George P. McCabe
Bruce A. Craig
Chapter 01 - Practices - 1.42
"""
# Step1: Import the Libraries
import pandas as pd

import matplotlib.pyplot as plt

import os

os.chdir("D:/21_SuccessFactor/Statistics")

#%%
# Step2: Prepare the dataset

df_125 = pd.read_excel('statistics data.xlsx', sheet_name='1.25', header=0)

# 对数据进行了排序,展示图表时用
df_125 = df_125.sort_values("Users", ascending=False)
df_125
# =============================================================================
# Out[24]: 
#           Country   Users
# 3         Germany   21.46
# 4          France   23.19
# 5     Philippines   26.87
# 0          Brazil   29.30
# 2          Mexico   29.80
# 7  United Kingdom   30.39
# 9          Turkey   30.63
# 1           India   37.38
# 6       Indonesia   40.52
# 8   United States  155.74
# =============================================================================
# Setup axis
X1 = df_125["Country"]
Y1 = df_125["Users"]

# Drawing a plot
fig1, ax1 = plt.subplots()
ax1.bar(X1, Y1, width=0.8 , color = colors)
plt.xticks(rotation = 300)

# Set labels
ax1.set_title("Facebook Users in Countries")
ax1.set_xlabel("Country")
ax1.set_ylabel("Users in millions")

# Show and save to file
fig1.show()
fig1.savefig("statistics data fig 1.25.png", dpi = 800)

程序运行结果如下所示,美国拥有最高注册用户数155.74万人,德国最少21.46万注册用户;那么美国是否为异常值,我们需要引入Outlier的定义,以及max,min,median,quartile等概念,感兴趣的可以看一下这个内容:观察异常值。

应用统计学 - 课后练习 - 1.2

Bar plot of 1.25

1.42 波士顿马拉松

1.42 The Boston Marathon. Women were allowed to enter the Boston Marathon in 1972. Here are the times (in minutes, rounded to the nearest minute) for the winning women from 1972 to 2015. Make a graph that shows change over time.

What overall pattern do you see? Have times stopped improving in recent years? If so, when did improvement end?

注意问题:你看到了什么样的整体趋势?近年来女子马拉松记录是否停止了改善?如果是,改进何时结束?

1.42 代码实现

这部分我们通过#Pandas#读取原始数据,并将两列Series(序列)类型的数据'Year','Minutes'分别赋值给两个自定义变量,X和Y,X将会是我们绘图的横轴,Y将会是绘图的纵轴。

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 29 22:26:21 2021

@author: Derek Zhu

"Introduction to the Practice of Statistics" - NINTH EDITION
Written by 
David S. Moore
George P. McCabe
Bruce A. Craig
Chapter 01 - Practices - 1.42
"""
# =============================================================================
# 1.42 The Boston Marathon. Women were allowed to enter the Boston Marathon in 1972. Here are the
# times (in minutes, rounded to the nearest minute) for the winning women from 1972 to 2015.
# Make a graph that shows change over time. What overall pattern do you see? Have times stopped improving in
# recent years? If so, when did improvement end?
# 波士顿马拉松女性成绩,以分钟为单位历年成绩
# =============================================================================

# Step1: Import the Libraries
import pandas as pd

import matplotlib.pyplot as plt

import os

os.chdir("D:/21_SuccessFactor/Statistics")

#%%
# Step2: Prepare the dataset
df = pd.read_excel('statistics data.xlsx', sheet_name='1.42', header=0)

df
# =============================================================================
# Out[29]: 
#     Year  Time
# 0   1972   190
# 1   1973   186
# 2   1974   167
# 3   1975   162
# 4   1976   167
# 5   1977   168
# 6   1978   165
# 7   1979   155
# 8   1980   154
# 9   1981   147
# 10  1982   150
# 11  1983   143
# 12  1984   149
# 13  1985   154
# 14  1986   145
# 15  1987   146
# 16  1988   145
# 17  1989   144
# 18  1990   145
# 19  1991   144
# 20  1992   144
# 21  1993   145
# 22  1994   142
# 23  1995   145
# 24  1996   147
# 25  1997   146
# 26  1998   143
# 27  1999   143
# 28  2000   146
# 29  2001   144
# 30  2002   141
# 31  2003   145
# 32  2004   144
# 33  2005   145
# 34  2006   143
# 35  2007   149
# 36  2008   145
# 37  2009   152
# 38  2010   146
# 39  2011   142
# 40  2012   151
# 41  2013   146
# 42  2014   139
# 43  2015   145
# =============================================================================

#%%
# Step 3: Plot the data
# Setup colors
cmap = plt.get_cmap("Oranges")
colors = cmap(0.5)

# Setup axis
X = df['Year']
Y = df['Minutes']

# Drawing a plot
fig, ax = plt.subplots()
ax.plot(X, Y, marker = "o", color = colors)

# Set labels
ax.set_title("Boston Marathon Famale Records")
ax.set_xlabel("Year")
ax.set_ylabel("Minutes")

# Show and save to file
fig.show()
fig.savefig("statistics data fig 1.42.png", dpi = 800)

程序运行后的结果如下所示:

应用统计学 - 课后练习 - 1.2

Practice 1.42

现在我们来回答1.42的问题:

你看到了什么样的整体趋势?近年来女子马拉松记录是否停止了改善?如果是,改进何时结束?

数据整体呈下降趋势,女子完成波士顿马拉松的成绩从190分钟逐渐接近145分钟;从1985年的154分钟之后,整体改进趋势趋于稳定,基本保持在140到150分钟之间;

总结

学习完第二节的总结如下:

要学会通过图表和数据来描述数据集合中的变量以及变量之间的关系;柱形图(Bar)和饼图(Pie)通常用以计数(Counts)或百分比(Percents)来显示分类变量;茎叶图(Stemplots)和直方图(Histograms)通常用来显示定量变量;观察数据分布的时候,通常会考察“形状”,“中心”和“范围”,以及明显的偏差;异常值(Outliers)是在总体分布趋势之外的显异常值,一定要注意观察是否有异常值,以及其具体原因。

数据分析对于我来说,不仅仅是工具,方法;更重要的是思维方式,统计学是一个很重要的弥补数据分析思维的方式,通过观察数据的分布,总体范围,偏差,异常值,我们总能获得一些观察数据的独特视角,这就是数据分析背后的驱动力,更是一种主观意识。

应用统计学 - 课后练习 - 1.2

Statistics Thinking

声明:本文由"麦兜"发布,不代表"破站"立场,转载联系作者并注明出处:https://www.848586.cn/171079.html