关于numpy错误:奇异矩阵和奇异矩阵翻译的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c–如何在反转奇异矩阵时使Armadillo函数不打印错误?、ios–CGAffineTransf
关于numpy错误:奇异矩阵和奇异矩阵翻译的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c – 如何在反转奇异矩阵时使Armadillo函数不打印错误?、ios – CGAffineTransformInvert:奇异矩阵、numpy 协方差矩阵 numpy.cov、numpy.linalg.LinAlgError:尝试求解时出现奇异矩阵错误等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- numpy错误:奇异矩阵(奇异矩阵翻译)
- c – 如何在反转奇异矩阵时使Armadillo函数不打印错误?
- ios – CGAffineTransformInvert:奇异矩阵
- numpy 协方差矩阵 numpy.cov
- numpy.linalg.LinAlgError:尝试求解时出现奇异矩阵错误
numpy错误:奇异矩阵(奇异矩阵翻译)
错误的Numpy error: Matrix is
singular
具体含义是什么(使用linalg.solve
函数时)?我在Google上进行了搜索,但是找不到任何可以清楚显示此错误的信息。
c – 如何在反转奇异矩阵时使Armadillo函数不打印错误?
#include <iostream> #include <armadillo> using namespace std; using namespace arma; int main(int argc,char** argv) { mat A = randu<mat>(5,5); mat B = zeros<mat>(5,5); inv(A,B); cout << A << "error printed but not fatal" << endl; A = inv(B); cout << A << "never make it this far" << endl; return 0; }
导致:
Johns-MacBook-Pro:test johnsherrill$g++ armaExample.cpp -o example -O2 -larmadillo Johns-MacBook-Pro:test johnsherrill$./example error: inv(): matrix appears to be singular [matrix size: 0x0] error printed but not fatal error: inv(): matrix appears to be singular terminate called after throwing an instance of 'std::runtime_error' what(): inv(): matrix appears to be singular Abort trap: 6
有没有办法解决这个问题,而无需先单独检查B是否可逆?这种类型的错误也印在R中.
解决方法
例如:
#define ARMA_DONT_PRINT_ERRORS #include <armadillo> // or #include <RcppArmadillo.h> if you're using Rcpp
定义描述于
http://arma.sourceforge.net/docs.html#config_hpp
ios – CGAffineTransformInvert:奇异矩阵
CGAffineTransformInvert: singular matrix
在Xcode的日志记录区域.当我在UIWebView中调整网站大小时,这似乎很少发生(幸运的是). [商业网站,不是我自己的.]由于我在我的应用程序中没有进行仿射转换,我想知道这可能是UIWebView的错误/功能.如果是这样,我可以忽略它,因为它似乎没有干扰任何东西吗?
解决方法
numpy 协方差矩阵 numpy.cov
numpy.
cov
(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)[source]
-
Estimate a covariance matrix, given data and weights.
Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples,
, then the covariance matrix element
is the covariance of
and
. The element
is the variance of
.
See the notes for an outline of the algorithm.
Parameters: m : array_like
A 1-D or 2-D array containing multiple variables and observations. Each row (行) of m represents a variable(变量), and each column(列) a single observation of all those variables(样本). Also see rowvar below.
y : array_like, optional
An additional set of variables and observations. y has the same form as that of m.
rowvar : bool, optional
If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.
bias : bool, optional
Default normalization (False) is by
(N - 1)
, whereN
is the number of observations given (unbiased estimate). If bias is True, then normalization is byN
. These values can be overridden by using the keywordddof
in numpy versions >= 1.5.ddof : int, optional
If not
None
the default value implied by bias is overridden. Note thatddof=1
will return the unbiased estimate, even if both fweights and aweights are specified, andddof=0
will return the simple average. See the notes for the details. The default value isNone
.New in version 1.5.
fweights : array_like, int, optional
1-D array of integer freguency weights; the number of times each observation vector should be repeated.
New in version 1.10.
aweights : array_like, optional
1-D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If
ddof=0
the array of weights can be used to assign probabilities to observation vectors.New in version 1.10.
Returns: out : ndarray
The covariance matrix of the variables.
See also
-
corrcoef
- Normalized covariance matrix
Notes
Assume that the observations are in the columns of the observation array m and let
f = fweights
anda = aweights
for brevity. The steps to compute the weighted covariance are as follows:>>> w = f * a >>> v1 = np.sum(w) >>> v2 = np.sum(w * a) >>> m -= np.sum(m * w, axis=1, keepdims=True) / v1 >>> cov = np.dot(m * w, m.T) * v1 / (v1**2 - ddof * v2)
Note that when
a == 1
, the normalization factorv1 / (v1**2 - ddof * v2)
goes over to1 / (np.sum(f) - ddof)
as it should.Examples
Consider two variables,
and
, which correlate perfectly, but in opposite directions:
>>> x = np.array([[0, 2], [1, 1], [2, 0]]).T >>> x array([[0, 1, 2], [2, 1, 0]])
Note how
increases while
decreases. The covariance matrix shows this clearly:
>>> np.cov(x) array([[ 1., -1.], [-1., 1.]])
Note that element
, which shows the correlation between
and
, is negative.
Further, note how x and y are combined:
>>> x = [-2.1, -1, 4.3] >>> y = [3, 1.1, 0.12] >>> X = np.stack((x, y), axis=0) >>> print(np.cov(X)) [[ 11.71 -4.286 ] [ -4.286 2.14413333]] >>> print(np.cov(x, y)) [[ 11.71 -4.286 ] [ -4.286 2.14413333]] >>> print(np.cov(x)) 11.71
总结
理解协方差矩阵的关键就在于牢记它的计算是不同维度之间的协方差,而不是不同样本之间。拿到一个样本矩阵,最先要明确的就是一行是一个样本还是一个维度,心中明确整个计算过程就会顺流而下,这么一来就不会迷茫了。 -
numpy.linalg.LinAlgError:尝试求解时出现奇异矩阵错误
这不依赖于IDE。您的A
矩阵是奇异的,其奇异值之一等于零。这意味着其行列式等于零。将A
乘以B
时,所得矩阵也是奇异的,然后乘以C
得到E
。这意味着E
也是奇异的(如果将两个矩阵相乘并且其中至少一个是奇异的,则结果总是奇异的)。我们可以通过用{p>查看E
的奇异值来确认这一点。
print(np.linalg.svd(E)[1])
在我的机器上打印的
[3.14537743e+03 9.15629385e-01 9.92880407e-17]
请注意,最后一个奇异值基本上为零。
然后,您尝试解决系统Ex = D
,但是由于E
是奇异的,因此会出现numpy.linalg.LinAlgError: Singular matrix
错误。请注意,由于E
是3x3,因此您尝试求解方程的3x3线性系统。由于您只有2个不同于零的奇异值,因此矩阵等级为2。这意味着您的线性系统具有3个变量,但只有
两个方程式。因此,您没有唯一的解决方案,并且np.linalg.solve
失败。
您的线性系统实际上具有无限解。虽然不能取E
的倒数,但仍可以对系统进行求解以找到一种解决方案。常用的方法是使用least squares方法。最小二乘方法不是求解Ex = D
,而是找到使x
的平方范数2最小的Ex - D
。您可以使用numpy进行计算
x = np.linalg.lstsq(E,D,rcond=None)[0]
我们无法确定这是一个解决方案
print(E@x)
可打印
[[1.]
[2.]
[3.]]
,
诊断的第一步是将有问题的值打印出来,例如config_value
及其决定因素:
E
矩阵是奇异的,因此不能求逆;没有解决方案或无穷无尽。
由于您尚未提供他们的解决方案数据,我们无法回答为什么您的同学会得到不同的答案。
今天的关于numpy错误:奇异矩阵和奇异矩阵翻译的分享已经结束,谢谢您的关注,如果想了解更多关于c – 如何在反转奇异矩阵时使Armadillo函数不打印错误?、ios – CGAffineTransformInvert:奇异矩阵、numpy 协方差矩阵 numpy.cov、numpy.linalg.LinAlgError:尝试求解时出现奇异矩阵错误的相关知识,请在本站进行查询。
本文标签: