#先定义两个矩阵
X=np.array([[1,2104,5,1,45],[1,1416,3,2,40],[1,1534,3,2,30],[1,852,2,1,36]])
y=np.array([45,40,30,36])
#内积以后发现
c=np.dot(X.T,X)
c
array([[ 4, 5906, 13, 6, 151],
[ 5906, 9510932, 21074, 8856, 228012],
[ 13, 21074, 47, 19, 507],
[ 6, 8856, 19, 10, 221],
[ 151, 228012, 507, 221, 5821]])
c.I
d=np.dot(c.I,X.T)
Traceback (most recent call last):
File "", line 1, in
d=np.dot(c.I,X.T)
AttributeError: 'numpy.ndarray' object has no attribute 'I'
#说明array进行内积以后已经不是array对象,成为ndarray对象,不能再进行.I,.T,.M的操作。
#解决方法:把结果转为matrix就可以
a=np.matrix([[ 4, 5906, 13, 6, 151],
[ 5906, 9510932, 21074, 8856, 228012],
[ 13, 21074, 47, 19, 507],
[ 6, 8856, 19, 10, 221],
[ 151, 228012, 507, 221, 5821]])
a.I
matrix([[ -4.12181049e+13, 1.93633440e+11, -8.76643127e+13,
-3.06844458e+13, 2.28487459e+12],
[ 1.93633440e+11, -9.09646601e+08, 4.11827338e+11,
1.44148665e+11, -1.07338299e+10],
[ -8.76643127e+13, 4.11827338e+11, -1.86447963e+14,
-6.52609055e+13, 4.85956259e+12],
[ -3.06844458e+13, 1.44148665e+11, -6.52609055e+13,
-2.28427584e+13, 1.70095424e+12],
[ 2.28487459e+12, -1.07338299e+10, 4.85956259e+12,
1.70095424e+12, -1.26659193e+11]])