编写不易如果觉得不错,麻烦关注一下~
参考:
https://blog.csdn.net/weixin_39813574/article/details/111015907
https://www.cnblogs.com/bingjianing/p/9117330.html
https://www.cnblogs.com/yanshw/p/11898408.html
https://blog.csdn.net/daniaokuye/article/details/116272253
1.求均值与协方差,下面是二维正太分布
>>> time=[1,2,3,4,5,6]
>>> cost=[4,5,3,4,8,9]
>>> import numpy as np
>>> np.array([time,cost])
array([[1, 2, 3, 4, 5, 6],
[4, 5, 3, 4, 8, 9]])
>>> np.array([time,cost]).T
array([[1, 4],
[2, 5],
[3, 3],
[4, 4],
[5, 8],
[6, 9]])
>>> var_matrix=np.array([time,cost]).T
>>> var_matrix
array([[1, 4],
[2, 5],
[3, 3],
[4, 4],
[5, 8],
[6, 9]])
>>> mean = np.mean(var_matrix,axis=0)
>>> mean
array([3.5, 5.5])
>>> var_matrix.shape
(6, 2)
>>> sigma = np.cov(var_matrix.T)
>>> sigma
array([[3.5, 3.5],
[3.5, 5.9]])
2.三维截图(应该默认高维可以!)
下面搜寻有没有直接求取多元高斯或正态分布密度函数的函数!
多元高斯分布密度函数
上式为 x 服从 k 元正态分布,x 为 k 维向量;|Σ| 代表协方差矩阵的行列式
下面测试了(0,0,0)点,和均值点的密度。
X 为3维 6个数据样本, mean 为均值,D为样本个数,利用最后一个式子可以直接求出协方差矩阵这里使用了样本数-1 作为分母
>>> X
tensor([[1., 2., 3., 4., 5., 6.],
[4., 5., 3., 4., 8., 9.],
[2., 2., 2., 2., 3., 3.]])
>>> ttt
tensor([[3.5000, 3.5000, 3.5000, 3.5000, 3.5000, 3.5000],
[5.5000, 5.5000, 5.5000, 5.5000, 5.5000, 5.5000],
[2.3330, 2.3330, 2.3330, 2.3330, 2.3330, 2.3330]])
>>> D
6
>>> 1/(D-1)*((X-ttt)@(X-ttt).t())
tensor([[3.5000, 3.5000, 0.8000],
[3.5000, 5.9000, 1.2000],
[0.8000, 1.2000, 0.2667]])