问题描述
我正在尝试使用来自 gaussian_kde
的 scipy
进行密度估计,但我总是收到
numpy.linalg.LinAlgError: singular matrix
不管我做什么。我过去曾使用过 gaussian_kde
,但仅用于单变量分布。
我想估计 784 个随机变量的联合密度。每个变量都是 28×28 图像中的一个像素。我有一组许多图像(对于那些知道这些是来自 MNIST 数据集的手写数字的人),我正在尝试复制 the documentation of gaussian_kde
所说的内容。我从链接中获取了示例,但不是两个变量,而是 784。这是我的 MWE:
from torchvision import transforms,datasets
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
raw_data = datasets.MNIST('',train=True,download=True,transform=transforms.Compose([transforms.ToTensor()]))
numpy_imgs = {}
for digit in [0,1,2,3,4,5,6,7,8,9]:
numpy_imgs[digit] = []
for train in raw_data:
numpy_imgs[train[1]].append(train[0].view(28,28).cpu().detach().numpy())
digit = 0 # Select one of the digits.
stacked_for_kde = np.array([img.ravel() for img in numpy_imgs[digit]]).transpose()
kernels = stats.gaussian_kde(
stacked_for_kde,bw_method = stacked_for_kde.shape[1]**(-1./(stacked_for_kde.shape[0]+4)),# This comes from the documentation https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.html
)
基于堆栈跟踪,问题是在选择带宽时。这就是为什么我只尝试使用一个常数,但错误仍然存在的原因。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)