GVKun编程网logo

numpy错误:奇异矩阵(奇异矩阵翻译)

26

关于numpy错误:奇异矩阵和奇异矩阵翻译的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c–如何在反转奇异矩阵时使Armadillo函数不打印错误?、ios–CGAffineTransf

关于numpy错误:奇异矩阵奇异矩阵翻译的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c – 如何在反转奇异矩阵时使Armadillo函数不打印错误?、ios – CGAffineTransformInvert:奇异矩阵、numpy 协方差矩阵 numpy.cov、numpy.linalg.LinAlgError:尝试求解时出现奇异矩阵错误等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

numpy错误:奇异矩阵(奇异矩阵翻译)

numpy错误:奇异矩阵(奇异矩阵翻译)

错误的Numpy error: Matrix is singular具体含义是什么(使用linalg.solve函数时)?我在Google上进行了搜索,但是找不到任何可以清楚显示此错误的信息。

c – 如何在反转奇异矩阵时使Armadillo函数不打印错误?

c – 如何在反转奇异矩阵时使Armadillo函数不打印错误?

一个伙伴和我正在研究一个R包,并使用RcppArmadillo包来处理一些较重的矩阵代数.到目前为止它已经非常甜蜜,但我们在矩阵求逆方面遇到了一些问题.简而言之,程序正在搜索特定类型的矩阵,并且必须检查在循环的每次迭代中是否存在更新的矩阵的逆(逆本身也是必需的).现在我们正在使用函数inv(A,B),它返回一个布尔值,指示矩阵B是否可逆(如果不是,A设置为0x0矩阵,否则A = inv(B)).如果此函数没有打印错误,那将是很好的,因为返回的布尔值给循环提供正确进行所需的信息.并且似乎只是打印错误而不是“抛出”,如下面的程序所示:

#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中.

解决方法

最简单的方法是在包含Armadillo标头之前定义ARMA_DONT_PRINT_ERRORS.

例如:

#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:奇异矩阵

ios – CGAffineTransformInvert:奇异矩阵

我偶尔会看到错误消息:
CGAffineTransformInvert: singular matrix

在Xcode的日志记录区域.当我在UIWebView中调整网站大小时,这似乎很少发生(幸运的是). [商业网站,不是我自己的.]由于我在我的应用程序中没有进行仿射转换,我想知道这可能是UIWebView的错误/功能.如果是这样,我可以忽略它,因为它似乎没有干扰任何东西吗?

解决方法

通过查看其他帖子,如果您尝试将缩放比例设置为零,则会显示此消息.当您捏合并查看它是否为零(并且与仿射变换错误同时发生)时,NSLog对缩放值有用.

numpy 协方差矩阵 numpy.cov

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, X = [x_1, x_2, ... x_N]^T, then the covariance matrix element C_{ij} is the covariance of x_i and x_j. The element C_{ii} is the variance of x_i.

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), where N is the number of observations given (unbiased estimate). If bias is True, then normalization is by N. These values can be overridden by using the keyword ddof in numpy versions >= 1.5.

ddof : int, optional

If not None the default value implied by bias is overridden. Note that ddof=1 will return the unbiased estimate, even if both fweights and aweights are specified, and ddof=0 will return the simple average. See the notes for the details. The default value is None.

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 fweights and 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 == 1, the normalization factor v1 (v1**2 ddof v2) goes over to (np.sum(f) ddof) as it should.

Examples

Consider two variables, x_0 and x_1, 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 x_0 increases while x_1 decreases. The covariance matrix shows this clearly:

>>> np.cov(x) array([[ 1., -1.],  [-1., 1.]]) 

Note that element C_{0,1}, which shows the correlation between x_0 and x_1, 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:尝试求解时出现奇异矩阵错误

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:尝试求解时出现奇异矩阵错误的相关知识,请在本站进行查询。

本文标签: