Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中,对数据帧进行过采样以保持其统计特性的最佳方法是什么?_Python_Python 3.x_Dataframe_Resampling_Oversampling - Fatal编程技术网

在Python3中,对数据帧进行过采样以保持其统计特性的最佳方法是什么?

在Python3中,对数据帧进行过采样以保持其统计特性的最佳方法是什么?,python,python-3.x,dataframe,resampling,oversampling,Python,Python 3.x,Dataframe,Resampling,Oversampling,我有以下玩具: FilterSystemO2Concentration (Percentage) ProcessChamberHumidityAbsolute (g/m3) ProcessChamberPressure (mbar) 0 0.156 1 29.5 28.4

我有以下玩具:

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_histogramnp.histogramdata.isfnp.random.randomsize=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

使用scipy.stats.rv_histogramnp.histogramdata.isfnp.random.randomsize=n将从数据的分布直方图中随机选择n个新样本。您可以对每个列执行此操作:这将尊重分布,尽管它不会考虑交叉相关性。这对你有用吗?嗨@Mstaino非常感谢你,我想它会有用的。如果您可以将此作为一个答案发布,说明如何将此功能应用于每一列并创建一个新的过采样df,我将很高兴将其作为答案接受谢谢Mstaino!!这正是我想要的: