繁体   English   中英

有没有办法在 Python 中创建相关变量来设置这些变量的标准偏差?

[英]Is there a way to create correlated variables in Python setting the standard deviation of these variables?

我想为四个变量创建假数据:身高、体重、年龄和收入。

我使用过这个脚本:

cov_matrix = [[1, 0.7, 0, 0],
              [0.7, 1, 0, 0],
              [0, 0, 1, 0.4],
              [0, 0, 0.4, 1]]
correlated = np.random.multivariate_normal([165, 65, 30, 15000], cov_matrix, size=250)
data = pd.DataFrame({
 "Height": correlated[:, 0],
 "Weight": correlated[:, 1],
 "Age": correlated[:, 2],
 "Income": correlated[:, 3]
})

但是结果不够好,四个变量的标准差(sd)大约为1,我希望我的数据有更多的离散性。 例如,变量“高度”的 sd 为 30。

有没有可能在 Python 中实现这一点?

要获得每个特征的方差,只需将这些值放在协方差矩阵的对角线上。 然而,需要对非对角线元素进行缩放以解决特征差异。

a1 = 0.7*np.sqrt(30*12)
a2 = 0.4*np.sqrt(19*50)
cov_matrix = np.array([[30.0,   a1,  0.0, 0.0],
                       [  a1, 12.0,  0.0, 0.0],
                       [ 0.0,  0.0, 19.0, a2],
                       [ 0.0,  0.0,   a2, 50.0]])

correlated = np.random.multivariate_normal([165, 65, 30, 15000], cov_matrix, size=1000)
print(correlated.var(axis=0))
print(np.corrcoef(correlated.T))

差异:

[28.02834149 11.14644597 18.68960579 49.46234297]

特征之间的互相关系数矩阵:

[[ 1.          0.67359842 -0.02016119 -0.02607946]
 [ 0.67359842  1.         -0.00338224 -0.01021924]
 [-0.02016119 -0.00338224  1.          0.37187791]
 [-0.02607946 -0.01021924  0.37187791  1.        ]]

或者,根据原始协方差矩阵生成数据,然后缩放和移动每个特征以获得所需的均值和标准差。 这将保留最初预期的相关系数。 请注意,平均值是缩放添加的否则缩放会改变平均值。

# generate correlated features with zero-mean and unit variance
correlated = np.random.multivariate_normal(np.zeros(4), cov_matrix, size=1000)

# multiply by the desired standard deviation to scale the data and add the mean
correlated = correlated.dot(np.diag(np.sqrt([30.0, 12.0, 19.0, 50.]))) + np.array([165, 65, 30, 15000])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM