Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在Python中以矩阵的方式计算SSE,它没有给我期望的值,并且hat矩阵H不是对称的,幂等的?_Python_Numpy_Statistics_Regression - Fatal编程技术网

我在Python中以矩阵的方式计算SSE,它没有给我期望的值,并且hat矩阵H不是对称的,幂等的?

我在Python中以矩阵的方式计算SSE,它没有给我期望的值,并且hat矩阵H不是对称的,幂等的?,python,numpy,statistics,regression,Python,Numpy,Statistics,Regression,它应该给出的SSE的值是839.72,但是由Python中创建的函数计算的值是1595.311,当我测试矩阵H是对称且幂等的时,它不是 np.dot()函数的实现是否正常 y = np.array(data['y']).reshape((-1, 1)) x = np.array(data.iloc[:, 1:5]) def sse(y, x): n = y.shape[0] I = np.identity(n) yt = np.transpose(y) xt =

它应该给出的SSE的值是839.72,但是由Python中创建的函数计算的值是1595.311,当我测试矩阵H是对称且幂等的时,它不是

np.dot()函数的实现是否正常

y = np.array(data['y']).reshape((-1, 1))
x = np.array(data.iloc[:, 1:5])

def sse(y, x):
    n = y.shape[0]
    I = np.identity(n)
    yt = np.transpose(y)
    xt = np.transpose(x)
    H = x.dot(np.linalg.inv(xt.dot(x))).dot(xt)
    sse = yt.dot(I-H).dot(y)

    return sse

np.array_equal(H.T, H) # Return False
np.array_equal(H**2, H) # Return False

数据如下:

y x1 x2 x3 x4
94 122 121 96 89
71 108 115 98 78
82 120 115 95 90
76 118 117 93 95
111 113 112 109 109
64 112 96 90 88
109 109 129 102 108
104 112 119 106 105
80 115 101 95 88
73 111 95 95 84
127 119 118 107 110
88 112 110 100 87
99 120 89 105 97
80 117 118 99 100
99 109 125 108 95
116 116 122 116 102
100 104 83 100 102
96 110 101 103 103
126 117 120 113 108
58 120 77 80 74