Pandas: 根据目标分布从 DataFrame 中采样
Pandas: Sampling from a DataFrame according to a target distribution
我有一个 Pandas DataFrame 包含一个数据集 D
个实例,这些实例都有一些连续值 x
。 x
以某种方式分布,比如统一,可以是任何东西。
我想从 D
中抽取 n
个样本,其中 x
具有我可以抽样或近似的目标分布。这来自一个数据集,这里我只取正态分布。
我如何从 D
中采样实例,使得样本中 x
的分布 equal/similar 到我指定的任意分布?
现在,我对值 x
的子集 D
进行采样,使其包含所有 x +- eps
并从中采样。但是当数据集变大时,这会很慢。人们一定想出了更好的解决办法。也许解决方案已经很好但可以更有效地实施?
我可以将 x
拆分成层,这样会更快,但是有没有没有这个的解决方案?
我当前的代码,工作正常但速度很慢(30k/100k 需要 1 分钟,但我有 200k/700k 左右。)
import numpy as np
import pandas as pd
import numpy.random as rnd
from matplotlib import pyplot as plt
from tqdm import tqdm
n_target = 30000
n_dataset = 100000
x_target_distribution = rnd.normal(size=n_target)
# In reality this would be x_target_distribution = my_dataset["x"].sample(n_target, replace=True)
df = pd.DataFrame({
'instances': np.arange(n_dataset),
'x': rnd.uniform(-5, 5, size=n_dataset)
})
plt.hist(df["x"], histtype="step", density=True)
plt.hist(x_target_distribution, histtype="step", density=True)
def sample_instance_with_x(x, eps=0.2):
try:
return df.loc[abs(df["x"] - x) < eps].sample(1)
except ValueError: # fallback if no instance possible
return df.sample(1)
df_sampled_ = [sample_instance_with_x(x) for x in tqdm(x_target_distribution)]
df_sampled = pd.concat(df_sampled_)
plt.hist(df_sampled["x"], histtype="step", density=True)
plt.hist(x_target_distribution, histtype="step", density=True)
与其生成新点并在 df.x
中寻找最近邻,不如定义每个点应根据目标分布进行采样的概率。您可以使用 np.random.choice
。对于像这样的高斯目标分布,在一秒左右的时间内从 df.x
中采样了一百万个点:
x = np.sort(df.x)
f_x = np.gradient(x)*np.exp(-x**2/2)
sample_probs = f_x/np.sum(f_x)
samples = np.random.choice(x, p=sample_probs, size=1000000)
sample_probs
是关键数量,因为它可以连接回数据框或用作 df.sample
的参数,例如:
# sample df rows without replacement
df_samples = df["x"].sort_values().sample(
n=1000,
weights=sample_probs,
replace=False,
)
plt.hist(samples, bins=100, density=True)
的结果:
高斯分布x,均匀目标分布
让我们看看当原始样本是从高斯分布中抽取并且我们希望从均匀目标分布中对其进行采样时此方法的执行情况:
x = np.sort(np.random.normal(size=100000))
f_x = np.gradient(x)*np.ones(len(x))
sample_probs = f_x/np.sum(f_x)
samples = np.random.choice(x, p=sample_probs, size=1000000)
尾巴在这个分辨率下看起来很紧张,但如果我们增加 bin 大小,它们就会变得平滑。
方法
计算 x
中样本的近似概率,格式为:
概率(x_i) ~ delta_x*rho(x_i)
其中rho(x_i)
是密度函数,np.gradient(x)
用作微分值。如果忽略差分权重,f_x
将在重采样中 over-represent 关闭点和 under-represent 稀疏点。我最初犯了这个错误,影响很小是 x 是均匀分布的(但通常可以显着):
我有一个 Pandas DataFrame 包含一个数据集 D
个实例,这些实例都有一些连续值 x
。 x
以某种方式分布,比如统一,可以是任何东西。
我想从 D
中抽取 n
个样本,其中 x
具有我可以抽样或近似的目标分布。这来自一个数据集,这里我只取正态分布。
我如何从 D
中采样实例,使得样本中 x
的分布 equal/similar 到我指定的任意分布?
现在,我对值 x
的子集 D
进行采样,使其包含所有 x +- eps
并从中采样。但是当数据集变大时,这会很慢。人们一定想出了更好的解决办法。也许解决方案已经很好但可以更有效地实施?
我可以将 x
拆分成层,这样会更快,但是有没有没有这个的解决方案?
我当前的代码,工作正常但速度很慢(30k/100k 需要 1 分钟,但我有 200k/700k 左右。)
import numpy as np
import pandas as pd
import numpy.random as rnd
from matplotlib import pyplot as plt
from tqdm import tqdm
n_target = 30000
n_dataset = 100000
x_target_distribution = rnd.normal(size=n_target)
# In reality this would be x_target_distribution = my_dataset["x"].sample(n_target, replace=True)
df = pd.DataFrame({
'instances': np.arange(n_dataset),
'x': rnd.uniform(-5, 5, size=n_dataset)
})
plt.hist(df["x"], histtype="step", density=True)
plt.hist(x_target_distribution, histtype="step", density=True)
def sample_instance_with_x(x, eps=0.2):
try:
return df.loc[abs(df["x"] - x) < eps].sample(1)
except ValueError: # fallback if no instance possible
return df.sample(1)
df_sampled_ = [sample_instance_with_x(x) for x in tqdm(x_target_distribution)]
df_sampled = pd.concat(df_sampled_)
plt.hist(df_sampled["x"], histtype="step", density=True)
plt.hist(x_target_distribution, histtype="step", density=True)
与其生成新点并在 df.x
中寻找最近邻,不如定义每个点应根据目标分布进行采样的概率。您可以使用 np.random.choice
。对于像这样的高斯目标分布,在一秒左右的时间内从 df.x
中采样了一百万个点:
x = np.sort(df.x)
f_x = np.gradient(x)*np.exp(-x**2/2)
sample_probs = f_x/np.sum(f_x)
samples = np.random.choice(x, p=sample_probs, size=1000000)
sample_probs
是关键数量,因为它可以连接回数据框或用作 df.sample
的参数,例如:
# sample df rows without replacement
df_samples = df["x"].sort_values().sample(
n=1000,
weights=sample_probs,
replace=False,
)
plt.hist(samples, bins=100, density=True)
的结果:
高斯分布x,均匀目标分布
让我们看看当原始样本是从高斯分布中抽取并且我们希望从均匀目标分布中对其进行采样时此方法的执行情况:
x = np.sort(np.random.normal(size=100000))
f_x = np.gradient(x)*np.ones(len(x))
sample_probs = f_x/np.sum(f_x)
samples = np.random.choice(x, p=sample_probs, size=1000000)
尾巴在这个分辨率下看起来很紧张,但如果我们增加 bin 大小,它们就会变得平滑。
方法
计算 x
中样本的近似概率,格式为:
概率(x_i) ~ delta_x*rho(x_i)
其中rho(x_i)
是密度函数,np.gradient(x)
用作微分值。如果忽略差分权重,f_x
将在重采样中 over-represent 关闭点和 under-represent 稀疏点。我最初犯了这个错误,影响很小是 x 是均匀分布的(但通常可以显着):