Python手工线性回归函数

Linear regression function by hand in Python

我已经编写了下面的代码来定义一个用于回归分析的函数,但是它不起作用。

import numpy as np
import pandas as pd

def linear_regress(y, X):
    X_trp = np.transpose(X)
    b = np.linalg.inv(X_trp@X)@X_trp@y
    e = y - X@b
    e_trp = np.transpose(e)
    sigma_sqr = (e_trp@e) / (X.shape[0] - X.shape[1])
    var_b = sigma_sqr@(np.linalg.inv(X_trp@X))
    SE = np.sqrt(var_b)
    z_score = 1.96
    upper = b+SE*z_score
    lower = b-SE*z_score
    CI = [lower, upper]
    results = {"Regression Coefficients": b,
              "Standard Error (SE)": SE,
              "95% Confidence Interval": CI}
    return results

它 returns 当我定义数组和 运行 函数时:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-8307755b9bbc> in <module>
----> 1 linear_regress(indt, dept)

<ipython-input-16-a6336ec1997c> in linear_regress(y, X)
      8     e_trp = np.transpose(e)
      9     sigma_sqr = (e_trp@e) / (X.shape[0] - X.shape[1])
---> 10     var_b = sigma_sqr@(np.linalg.inv(X_trp@X))
     11     SE = np.sqrt(var_b)
     12     z_score = 1.96

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

你能帮我找出问题所在吗?

感谢您的帮助!

如果不正确了解您的数据,很难判断您做错了什么。 但我猜你正在尝试实施 Normal Equation 来增加体重。 如果是这样的话,这似乎太复杂了,这里有一个更简单的方法。

def LinearRegression(X, y):
    X_t = X.T
    theta = np.dot(np.linalg.inv(np.dot(X.T, X)), np.dot(y,X))
    mean_sqr_error = np.mean((y - np.dot(theta,X.T))**2)
    return theta, mean_sqr_error