Python 中的随机抽样

本节总结了你在 Python 中进行随机抽样所学到的方法,并介绍一种新方法。

回顾:从表格中的总体抽样

如果你是从个体数据表示在表格行中的总体中进行抽样,那么你可以使用 Table 方法 sample随机选择表格的行。也就是说,你可以使用 sample 来选择一个个体的随机样本。

默认情况下,sample 以随机放回的方式进行均匀抽样。这是诸如掷骰子等随机实验的自然模型。

[In ]:
from datascience import *
path_data = '../../../assets/data/'
import matplotlib
matplotlib.use('Agg')
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
import numpy as np
[In ]:
faces = np.arange(1, 7)
die = Table().with_columns('Face', faces)
die
Face
1
2
3
4
5
6

运行下面的单元格以模拟 7 次掷骰子。

[In ]:
die.sample(7)
Face
5
3
3
5
5
1
6

有时更自然的方式是随机不放回地抽样个体。这称为简单随机样本。参数 with_replacement=False 允许你这样做。

[In ]:
actors = Table.read_table(path_data + 'actors.csv')
actors
Actor              | Total Gross | Number of Movies | Average per Movie | #1 Movie                     | Gross
Harrison Ford      | 4871.7      | 41               | 118.8             | Star Wars: The Force Awakens | 936.7
Samuel L. Jackson  | 4772.8      | 69               | 69.2              | The Avengers                 | 623.4
Morgan Freeman     | 4468.3      | 61               | 73.3              | The Dark Knight              | 534.9
Tom Hanks          | 4340.8      | 44               | 98.7              | Toy Story 3                  | 415
Robert Downey, Jr. | 3947.3      | 53               | 74.5              | The Avengers                 | 623.4
Eddie Murphy       | 3810.4      | 38               | 100.3             | Shrek 2                      | 441.2
Tom Cruise         | 3587.2      | 36               | 99.6              | War of the Worlds            | 234.3
Johnny Depp        | 3368.6      | 45               | 74.9              | Dead Man's Chest             | 423.3
Michael Caine      | 3351.5      | 58               | 57.8              | The Dark Knight              | 534.9
Scarlett Johansson | 3341.2      | 37               | 90.3              | The Avengers                 | 623.4
... (40 rows omitted)
[In ]:
# Simple random sample of 5 rows
actors.sample(5, with_replacement=False)
Actor             | Total Gross | Number of Movies | Average per Movie | #1 Movie         | Gross
Morgan Freeman    | 4468.3      | 61               | 73.3              | The Dark Knight  | 534.9
Orlando Bloom     | 2815.8      | 17               | 165.6             | Dead Man's Chest | 423.3
Cameron Diaz      | 3031.7      | 34               | 89.2              | Shrek 2          | 441.2
Michael Caine     | 3351.5      | 58               | 57.8              | The Dark Knight  | 534.9
Leonardo DiCaprio | 2518.3      | 25               | 100.7             | Titanic          | 658.7

由于 sample 以行被选择的顺序给出整个样本,你可以在抽样表格上使用 Table 方法来回答关于样本的许多问题。例如,你可以找到骰子显示六点的次数,或抽样演员出演的平均电影数量,或两个指定演员是否出现在样本中。你可能需要多行代码来获取某些此类信息。

回顾:从数组中的总体抽样

如果你是从数据表示为数组的总体中进行抽样,你可以使用 NumPy 函数 np.random.choice随机选择数组的元素

默认情况下,np.random.choice 以随机放回的方式抽样。

[In ]:
# The faces of a die, as an array
faces
array([1, 2, 3, 4, 5, 6])
[In ]:
# 7 rolls of the die
np.random.choice(faces, 7)
array([4, 1, 6, 3, 5, 4, 6])

参数 replace=False 允许你获得简单随机样本,即随机无放回地抽取的样本。

[In ]:
# Array of actor names
actor_names = actors.column('Actor')
[In ]:
# Simple random sample of 5 actor names
np.random.choice(actor_names, 5, replace=False)
array(['Jonah Hill', 'Julia Roberts', 'Bruce Willis', 'Eddie Murphy',
       'Matt Damon'], dtype='<U22')

sample 一样,np.random.choice 也给出被抽样元素的整个序列。你可以使用数组操作来回答关于样本的许多问题。例如,你可以找出哪个演员是第二个被抽取的,或者骰子哪个面出现超过一次。某些答案可能需要多行代码。

从分类分布中抽样

有时我们关注的是抽样个体的分类属性。例如,我们可能观察硬币是正面还是反面朝上。或者我们可能对随机选择的选民的政策偏好感兴趣。

在这种情况下,我们经常需要不同类别中抽样选民的比例。如果我们有整个样本,我们可以计算这些比例。datascience 库中的 sample_proportions 函数为我们完成了这项工作。它专门用于从分类分布中随机放回地抽样,并返回每个类别中抽样元素的比例。

sample_proportions 函数接受两个参数: - 样本大小 - 总体中类别的分布,作为比例列表或数组,其和为 1

它返回一个数组,包含从总体中抽取的给定大小的随机样本中各类别的分布。该数组由所有不同类别中的样本比例组成,顺序与它们在总体分布中出现的顺序相同。

例如,假设某个植物物种的每株植物开红花的概率为 25%,开粉花的概率为 50%,开白花的概率为 25%,且与其他植物的花色无关。你可以使用 sample_proportions 来查看该物种 300 株植物中不同颜色的比例。

[In ]:
# Species distribution of flower colors:
# Proportions are in the order Red, Pink, White
species_proportions = [0.25, 0.5, .25]

sample_size = 300

# Distribution of sample
sample_distribution = sample_proportions(sample_size, species_proportions)
sample_distribution
array([0.24333333, 0.50333333, 0.25333333])

正如你所预期的,样本中的比例之和为 1。

[In ]:
sum(sample_distribution)
1.0

species_proportions 中的类别顺序为红花、粉花、白花。sample_proportions 保留该顺序。如果你只想要样本中开粉花植物的比例,你可以使用 item

[In ]:
# Sample proportion of Heads
sample_distribution.item(1)
0.5033333333333333

你可以使用 sample_proportions 和数组操作仅基于样本个体在不同类别中的比例来回答问题。你将无法回答需要关于样本更详细信息的问题,例如哪些抽样植物具有每种不同的颜色。

[In ]: