采样技术及其在 Python 中的实现
一、说明
关于用python抽样,一般我们用均匀分布抽样,高斯分布抽样,但还有许多分布的抽样如何实现?本文针对此类问题,探讨如何用python去实践。
二、抽样基本概念
在进行研究或得出关于某个群体的结论时,任何人都不可能收集到该群体的所有数据。因此,人们会选择一个样本数据,该样本数据能够代表整个数据群体。这种方法基本上被称为抽样。这里我们会遇到两个关键词:总体和样本数据。总体是指从中创建样本的数据群体。
抽样方法有两种:i) 概率分布抽样;ii) 非概率抽样
三、概率分布抽样
概率抽样是一种从总体中选择样本的方法,它确保总体中的每个成员被选中的概率均等且已知。这意味着总体中的每个个体都有公平且无偏的机会被选入样本,从而提高了从样本中获得的结果的可靠性和有效性。
概率抽样有多种类型,以下列举几种:
i) 简单随机抽样:
理论:在这种方法中,总体中的每个成员都被分配一个唯一的编号,然后使用随机数生成器或随机数表来选择样本。这种方法的一大优点是它是概率抽样中最直接的方法。
Python实现:
#importing the random module
import random
#defining the population from where sample will be created
population = list(range(1, 100))
#defining the size of sample
sample_size = 10
#perform simple random sampling by using the random.sample() function
sample = random.sample(population, sample_size)
#it will print 10 random numbers within the range provided
print("Simple random sampling of 10 numbers are: ", sample)
##output:
##Simple random sampling of 10 numbers are: [40, 34, 60, 96, 8, 95, 94, 73, 93, 26]
ii) 系统抽样:
理论:系统抽样或区间抽样 是一种概率抽样技术,用于系统地从总体中选择样本。在系统抽样中,首先对总体进行某种排序,然后每隔n个个体抽取一个样本,其中“n”是预先设定的抽样间隔。例如,如果抽样间隔为5,则总体中每隔5个个体抽取一个样本。
Python实现
import numpy as np
# Define the population
population = np.arange(1, 100)
# Define the sample size and sampling interval
# We can provide sample size and sampling interval as per user
# Here sampling interval is 9 (99//10 = 9)
sample_size = 10
sampling_interval = len(population) // sample_size
# Define the starting point of the sample
# refer to https://numpy.org/doc/stable/reference/random/generated/numpy.random.randint.html for random.randint()
start_point = np.random.randint(0, sampling_interval)
# Perform systematic sampling
sample = population[start_point::sampling_interval]
# Print the sample
print("Systematic or interval sampling of 10 numbers are: ",sample)
##Output:
##Systematic or interval sampling of 10 numbers are: [ 6 15 24 33 42 51 60 69 78 87 96]
##Random numbers are generated from integer 6 with 9th interger as 15
iii) 分层抽样:
理论:当总体具有不同的类别或子群体时,样本框可以被划分为独立的“层”。每个层都作为一个独立的子总体进行抽样,可以从中随机选择个体元素。这种子总体抽样方法称为分层抽样。分层抽样的一个典型例子是政治调查。一个城镇的人口构成基于不同的种族、宗教、性别、教育程度、文化背景等因素。因此,基于这些因素的分层抽样可能比简单随机抽样或系统抽样更具代表性。
Python实现
import pandas as pd
from sklearn.model_selection import train_test_split
# Load the data into a Pandas DataFrame
data=pd.read_csv("https://raw.githubusercontent.com/ayan-zz/Statistics_python/main/titanic.csv")
# Specify the stratification variable
stratify_by = 'Sex'
# Split the data into training and testing sets, with stratification
train, test = train_test_split(data, test_size=0.3, stratify=data[stratify_by])
# Check the distribution of the stratification variable in the training and testing sets
print("Train dataset:\n", train[stratify_by].value_counts())
print("Test dataset:\n", test[stratify_by].value_counts())
##Output:
Train dataset:
male 403
female 220
Name: Sex, dtype: int64
Test dataset:
male 174
female 94
Name: Sex, dtype: int64
iv) 整群抽样:
理论:只有当总体中存在明显的异质群体或聚类时,才应进行整群抽样。所选的聚类必须互斥且穷尽,才能构成总体的代表性样本。样本的选取采用简单随机抽样法,从各个聚类中抽取。聚类和群体内的所有元素都包含在研究范围内。这称为第一阶段整群抽样。如果对每个选定聚类中的元素应用随机抽样技术,则称为第二阶段抽样。例如,跨国公司客户调查。该公司希望开展一项调查以收集客户反馈。该公司拥有一个客户地址数据库,并根据地理位置将数据库划分为若干聚类。然后,从这些聚类中随机抽取样本,并对所选聚类内的所有客户进行调查。
import random
# create a list of population data
population_data = [10, 15, 20, 26, 29, 33, 35, 37, 41, 42, 46, 48, 50, 52, 55, 58, 61, 64, 68, 72]
# set the desired cluster size
cluster_size = 5
# randomly select a starting point for the first cluster
starting_point = random.randint(0, cluster_size + 1)
# create a list to store the sampled data
sampled_data = []
# loop through the population data by clusters of size cluster_size
for i in range(starting_point, len(population_data), cluster_size):
# append the data from the current cluster to the sampled data list
sampled_data.append(population_data[i+1:i+cluster_size])
# print the sampled data
print(starting_point)
print(sampled_data)
## Output
# 0
# [[23, 26, 29], [37, 41, 42], [50, 52, 55], [64, 68, 72]]
四、非概率抽样
非概率抽样是指样本选择并非随机,且样本可能无法直接代表总体的一种抽样方法。在非概率抽样中,总体中特定成员被选入样本的概率是未知的,例如,地理位置的接近程度、样本的可获得性等等。
它们可以分为以下几种类型:
i) 便利抽样:抽样依据研究者的便利性,包括地理位置的接近性和数据的可获得性。这些数据点可以方便快捷地收集用于研究。
ii) 配额抽样:这是一种非概率抽样方法,参与者是根据预先设定的配额选择的。配额抽样常用于市场调研,研究人员希望确保样本在某些特征(例如年龄、性别或收入)方面能够代表总体。
iii) 目的性(判断性)抽样:这种抽样方法根据特定标准选择参与者。目的性抽样常用于定性研究,研究者希望从具有特定观点或经历的个体那里收集深入的信息。例如:一位研究者对被诊断患有罕见遗传疾病的个体的经历感兴趣。研究者找到一家专门治疗该疾病的诊所,并从该诊所招募参与者。研究者还可以使用其他纳入标准,例如年龄或性别,来进一步细化样本。
四)滚雪球抽样:这种抽样方法适用于难以确定总体的情况。首先确定一个初始标准,然后根据该标准初步选择样本,再请这些样本推荐其他符合条件的人。例如:求职中的推荐。
更多推荐
所有评论(0)