我有一个计算概率的函数,如下所示:def multinormpdf(x, mu, var): # calculate probability of multi Gaussian distribution k = len(x) det = np.linalg.det(var) inv = np.linalg.inv(var) denominator = math.sqrt(((2*math.pi)**k)*det) numerator = np.dot((x - mean).transpose(), inv) numerator = np.dot(numerator, (x - mean)) numerator = math.exp(-0.5 * numerator) return numerator/denominator我有均值向量、协方差矩阵和 2D numpy 数组用于测试mu = np.array([100, 105, 42]) # mean vectorvar = np.array([[100, 124, 11], # covariance matrix [124, 150, 44], [11, 44, 130]])arr = np.array([[42, 234, 124], # arr is 43923794 x 3 matrix [123, 222, 112], [42, 213, 11], ...(so many values about 40,000,000 rows), [23, 55, 251]])我必须计算每个值的概率,所以我使用了这个代码for i in arr: print(multinormpdf(i, mu, var)) # I already know mean_vector and variance_matrix但是速度太慢了...有没有更快的方法来计算概率?或者有什么方法可以像“批处理”一样一次计算测试 arr 的概率?
添加回答
举报
0/150
提交
取消