数据预处理中,数据采样有随机采样,等间隔采样,分层采样,加权采样,过滤采样以及概率采样。给出上述采样的示例代码
时间: 2024-01-26 12:03:30 浏览: 63
aaa.rar_学位论文_论文 数据采集_随机采样_高速采样_高速采集
我可以为您提供一些示例代码,具体如下:
1. 随机采样:
import random
data = [1,2,3,4,5,6,7,8,9,10]
sample_data = random.sample(data, 3)
print(sample_data)
2. 等间隔采样:
data = [1,2,3,4,5,6,7,8,9,10]
interval = 2
sample_data = [data[i] for i in range(0, len(data), interval)]
print(sample_data)
3. 分层采样:
import random
data = {1:[1,2,3],2:[4,5,6],3:[7,8,9],4:[10,11,12]}
sample_data = []
for k,v in data.items():
sample_data += random.sample(v,1)
print(sample_data)
4. 加权采样:
import random
data = [1,2,3,4,5,6,7,8,9,10]
weights = [0.2, 0.1, 0.05, 0.15, 0.2, 0.1, 0.05, 0.1, 0.025, 0.025]
sample_data = random.choices(data, weights=weights, k=3)
print(sample_data)
5. 过滤采样:
data = [1,2,3,4,5,6,7,8,9,10]
filter_func = lambda x: x%2==0
sample_data = list(filter(filter_func, data))
print(sample_data)
6. 概率采样:
import random
data = [1,2,3,4,5,6,7,8,9,10]
sample_data = []
for x in data:
prob = random.random()
if prob < 0.2: # 20%概率采样
sample_data.append(x)
print(sample_data)
阅读全文