首页 > 解决方案 > 如何组合/联合两个单独的 numpy 高斯集?

问题描述

我想组合两个独立的随机高斯数据集,一个有自己的均值和标准,另一个有离群均值和标准。我的代码是这样的:

import random
import numpy as np
import numpy.random as ra
from numpy.random import seed 

#This makes the random numbers generated not change when rerunning the code
np.random.seed(0)

#Creating two Gaussian sets, one with mean 0 and std 1, the second is outlier with mean 3 and std 1
#Each set contains 1,000 trials, first set contains 99 points while outlier set contains 1 point for each trial (for 1% outlier)

data = np.random.normal(loc=0, scale=1, size=(1000, 99))
dataoutlier = np.random.normal(loc=3, scale=1, size=(1000, 1))

现在我怎样才能将它结合起来,使异常值与每次试验的第一组一致?我认为使用 np.union1d 会起作用,但这会将所有试验组合成一个巨大的数组。任何帮助将不胜感激!

标签: pythonnumpystatistics

解决方案


为了按列组合两个 numpy 数组,您可以使用append方法。

np.append(data, dataoutlier, axis=1)

推荐阅读