多元正态分布概率密度函数的三维绘图

2024-03-29 06:52:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我从这里改编了以下代码:https://scipython.com/blog/visualizing-the-bivariate-gaussian-distribution/ 处理我的数据。在

我的数据

hour    Cost
20  58.00
20  336.00
20  34.50
20  106.50
20  118.00
...
11  198.36
11  276.00
11  40.00
11  308.00
11  140.00
11  72.00
11  116.50
11  290.00
11  266.00
11  66.00
11  100.00
11  79.00
11  106.00
11  160.00

我的代码:

^{pr2}$

假设小时和成本任意随机向量

  • 如何修复此错误?在
C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\scipy\stats\_multivariate.py in __init__(self, mean, cov, allow_singular, seed, maxpts, abseps, releps)
    725         self._dist = multivariate_normal_gen(seed)
    726         self.dim, self.mean, self.cov = self._dist._process_parameters(
--> 727                                                             None, mean, cov)
    728         self.cov_info = _PSD(self.cov, allow_singular=allow_singular)
    729         if not maxpts:

C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\scipy\stats\_multivariate.py in _process_parameters(self, dim, mean, cov)
    397 
    398         if mean.ndim != 1 or mean.shape[0] != dim:
--> 399             raise ValueError("Array 'mean' must be a vector of length %d." % dim)
    400         if cov.ndim == 0:
    401             cov = cov * np.eye(dim)

ValueError: Array 'mean' must be a vector of length 173873952.
  • 我如何知道我的数据中任何一对(小时、成本)的概率并将它们可视化?在

对不起,我不是英语母语。在

所以我的问题停留了一段时间没有答案,我采纳了@ImportanceOfBeingErnest的建议来简化示例并使其成为可验证的示例:

这是一个简单的例子:

time=[1,2,3,4,5,6]
cost=[4,5,3,4,8,9]
var_matrix=np.array([time,cost]).T
mean = np.mean(var_matrix,axis=0)
sigma = np.cov(var_matrix.T)
y = multivariate_normal.pdf(var_matrix, mean=mean, cov=sigma,allow_singular=True)
  • 如何绘制三维图形显示(成本、时间)和成对概率密度值。在

提前谢谢。在


Tags: 数据代码selfifvarnpmeancov
1条回答
网友
1楼 · 发布于 2024-03-29 06:52:04

您可以直接应用^{} documentation中给出的示例

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import multivariate_normal

time=[1,2,3,4,5,6]
cost=[4,5,3,4,8,9]

var_matrix=np.array([time,cost]).T
mean = np.mean(var_matrix,axis=0)
sigma = np.cov(var_matrix.T)
dist = multivariate_normal(mean, cov=sigma)

x, y = np.mgrid[1:6.02:.05, 3:9.02:.05]
pos = np.empty(x.shape + (2,))
pos[:, :, 0] = x; pos[:, :, 1] = y

z = dist.pdf(pos)

plt.contourf(x,y,z)
plt.show()

enter image description here

相关问题 更多 >