KDE有两点失败? [英] KDE fails with two points?

查看:117
本文介绍了KDE有两点失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简单示例返回一个奇异矩阵.为什么?有什么办法可以克服吗?

The following trivial example returns a singular matrix. Why? Any ways to overcome it?

In: from scipy.stats import gaussian_kde
Out:

In:  points
Out: (array([63, 84]), array([46, 42]))

In:  gaussian_kde(points)
Out: (array([63, 84]), array([46, 42]))

LinAlgError: singular matrix

推荐答案

查看回溯,可以看到在反转协方差矩阵时它失败了.这是由于您的数据的确切 multicollinearity .在页面上,如果两个变量共线,即if

Looking at the backtrace, you can see it fails when inverting the covariance matrix. This is due to exact multicollinearity of your data. From the page, you have multicollinearity in your data if two variables are collinear, i.e. if

两个自变量之间的相关性等于1或-1

the correlation between two independent variables is equal to 1 or -1

在这种情况下,这两个变量只有两个样本,并且它们始终是共线的(通常,总是有一条线通过两个不同的点).我们可以检查一下:

In this case, the two variables have only two samples, and they are always collinear (trivially, there exists always one line passing two distinct points). We can check that:

np.corrcoef(array([63,84]),array([46,42]))
[[ 1. -1.]
 [-1.  1.]]

不必一定是共线的,两个变量必须至少具有n=3个样本.为了增加此约束,您需要使用ali_m指出的限制,即样本n的数量应大于或等于变量p的数量.将两者放在一起,

To not be necessarily collinear, two variables must have at least n=3 samples. To add to this constraint, you have the limitation pointed out by ali_m, that the number of samples n should be greater or equal to the number of variables p. Putting the two together,

n>=max(3,p)

在这种情况下,

p=2n>=3是正确的约束.

in this case p=2 and n>=3 is the right constraint.

这篇关于KDE有两点失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆