用矩阵逆不理解这个AttributeError

时间:2018-10-11 01:37:59

标签: python scikit-learn linear-regression matrix-inverse

我写了一个有关线性回归的小程序。但是,有一个我找不到原因的错误。该程序如下:

from sklearn.datasets import load_svmlight_file
from sklearn.model_selection import train_test_split
import numpy as np
X, Y = load_svmlight_file(r"C:\Users\asus\Desktop\data.txt")
trainx,testx=train_test_split(X,shuffle=False)
trainy,testy=train_test_split(Y,shuffle=False)
traverse_trainx=trainx.T
#闭式解
temp1=(traverse_trainx*trainx).I
temp1.I

错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-130-92af132d9e41> in <module>()
      8 traverse_trainx=trainx.T
      9 #闭式解
---> 10 temp1=(traverse_trainx*trainx).I
     11 temp1.I
     12 

G:\anaconda\lib\site-packages\scipy\sparse\base.py in __getattr__(self, attr)
    684             return self.getnnz()
    685         else:
--> 686             raise AttributeError(attr + " not found")
    687 
    688     def transpose(self, axes=None, copy=False):

AttributeError: I not found

1 个答案:

答案 0 :(得分:0)

如果要查找矩阵逆,则必须使用numpy.linalg.invscipy.linalg.pinv2之类的函数。

您得到的错误是因为数组上没有.I属性。