使用 PD 在高斯分布中重新分发数据.数据帧



我有一个熊猫数据帧,其中包含属于每个类(列(的每个样本的概率。几乎99%的类有< 0.01概率,很少有> 0.5概率。出于某种原因,我希望概率在01之间以高斯分布分布。我想在这种情况下,平均值应该是0.5的,但如果可能的话,我也希望能够修改这种分布的平均值。 我想分别对每一行执行此操作,如何使用熊猫数据帧执行此操作?

如果你想重现一个更像高斯的分布,你说的是单点(连续的类分数(。
所以我建议使用瓜斯分布权重来放大分数。
这里有一个例子:

import numpy as np
import pandas as pd
#Preparation of the data
nclasses = 10
nsamples = 5
df_c = []
for nc in range( nsamples ):
a = np.random.rand(nclasses)
a = [n/np.sum(a) for n in a]
df_c.append( a )
df = pd.DataFrame(df_c)
# Now let's weight
for nr in range( df[0].count() ): #iterate over rows
a = df.iloc[nr] #capture the nth row
#generate Gaussian weights
gw = np.random.normal( np.mean(a), np.std(a), len(a) )
#sort gw and a in order to assign one to the other
gw = np.sort(gw)
b_ind = np.argsort(a) #indexes to sort a
b = a[b_ind]          #sorted version of a
# now weight the row
aw_r = a*b # you can reduce the entity adding anotherfactor, like 0.8 for instance
# back from sort
aw = [ aw_r[n] for n in b_ind ]
#update the dataframe
df.iloc[nr] = aw
# there you go!

希望会有所帮助

更新__
如果要将每行的均值调整为相同的值,例如 0.5,则只需减去行均值和目标均值之间的差值(在本例中为 0.5(。

a=np.array([1,2,3,47,2,6])
print( a.mean() ) # 10.1666
target_mean = 0.5
a_adj = a-(np.mean(a) - target_mean)
print( np.mean( a_adj ) ) # 0.5

这意味着在上面的主要示例中,在 df.iloc[nr] 中替换 aw 之前,您应该执行以下操作

aw = aw-(np.mean(aw) - 0.5)

最新更新