在 Python 3 中对数据帧进行过采样并保留其统计属性的最佳方法是什么?
What is the best way to oversample a dataframe preserving its statistical properties in Python 3?
我有以下玩具 df:
FilterSystemO2Concentration (Percentage) ProcessChamberHumidityAbsolute (g/m3) ProcessChamberPressure (mbar)
0 0.156 1 29.5 28.4 29.6 28.4
2 0.149 1.3 29.567 28.9
3 0.149 1 29.567 28.9
4 0.148 1.6 29.6 29.4
这只是一个示例。原来有超过 1200 行。对其进行过采样以保留其统计特性的最佳方法是什么?
我用谷歌搜索了一段时间,我只遇到过用于不平衡 类 的重采样算法。但这不是我想要的,我对平衡 thr 数据不感兴趣,我只是想以或多或少保留原始数据分布和统计属性的方式生成更多样本。
提前致谢
使用scipy.stats.rv_histogram(np.histogram(data)).isf(np.random.random(size=n))
将创建 n 个从数据分布(直方图)中随机选择的新样本。您可以为每一列执行此操作:
示例:
import pandas as pd
import scipy.stats as stats
df = pd.DataFrame({'x': np.random.random(100)*3, 'y': np.random.random(100) * 4 -2})
n = 5
new_values = pd.DataFrame({s: stats.rv_histogram(np.histogram(df[s])).isf(np.random.random(size=n)) for s in df.columns})
df = df.assign(data_type='original').append(new_values.assign(data_type='oversampled'))
df.tail(7)
>> x y data_type
98 1.176073 -0.207858 original
99 0.734781 -0.223110 original
0 2.014739 -0.369475 oversampled
1 2.825933 -1.122614 oversampled
2 0.155204 1.421869 oversampled
3 1.072144 -1.834163 oversampled
4 1.251650 1.353681 oversampled
我有以下玩具 df:
FilterSystemO2Concentration (Percentage) ProcessChamberHumidityAbsolute (g/m3) ProcessChamberPressure (mbar)
0 0.156 1 29.5 28.4 29.6 28.4
2 0.149 1.3 29.567 28.9
3 0.149 1 29.567 28.9
4 0.148 1.6 29.6 29.4
这只是一个示例。原来有超过 1200 行。对其进行过采样以保留其统计特性的最佳方法是什么?
我用谷歌搜索了一段时间,我只遇到过用于不平衡 类 的重采样算法。但这不是我想要的,我对平衡 thr 数据不感兴趣,我只是想以或多或少保留原始数据分布和统计属性的方式生成更多样本。
提前致谢
使用scipy.stats.rv_histogram(np.histogram(data)).isf(np.random.random(size=n))
将创建 n 个从数据分布(直方图)中随机选择的新样本。您可以为每一列执行此操作:
示例:
import pandas as pd
import scipy.stats as stats
df = pd.DataFrame({'x': np.random.random(100)*3, 'y': np.random.random(100) * 4 -2})
n = 5
new_values = pd.DataFrame({s: stats.rv_histogram(np.histogram(df[s])).isf(np.random.random(size=n)) for s in df.columns})
df = df.assign(data_type='original').append(new_values.assign(data_type='oversampled'))
df.tail(7)
>> x y data_type
98 1.176073 -0.207858 original
99 0.734781 -0.223110 original
0 2.014739 -0.369475 oversampled
1 2.825933 -1.122614 oversampled
2 0.155204 1.421869 oversampled
3 1.072144 -1.834163 oversampled
4 1.251650 1.353681 oversampled