这是用户在 2024-5-25 16:33 为 https://edstem.org/au/courses/14775/lessons/47631/slides/324139 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Week 7: Regression 第 7 周回归

Summary 摘要

Further NumPy: 进一步的 NumPy

array.reshape(shape)
array 矩阵.reshape(shape 形状)
np.linspace(start, end, number of values)
np.linspace(start, end, 数值个数)
np.linspace(start, end, number of values)
np.linspace(start, end, 数值个数)
np.ones(shape)
np.ones(shape 形状)
  • Linear algebra operations
    线性代数运算

    • transpose: matrix.T
      转置: matrix .T

    • dot product: np.dot(vector_1, vector_2 )
      点积: np.dot( vector_1 , vector_2 )

    • multiplication: np.matmul(matrix_1, matrix_2 ) OR matrix_1 @ matrix_2
      乘法: np.matmul( matrix_1 , matrix_2 )matrix_1 @ matrix_2

    • inverse: np.linalg.inv(matrix )
      逆: np.linalg.inv( matrix )

Linear Regression: 线性回归

y=β0+β1x+ϵy = \beta_0 + \beta_1 x + \epsilon

y=β0+β1x1+β2x2++βpxp+ϵ y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \dots + \beta_p x_p + \epsilon

[y1y2y3......yn]=[1x11x12...x1p1x21x22...x2p1x31x32...x3p..............................1xn1xn2...xnp][β0β1β2......βp]+[ϵ1ϵ2ϵ3......ϵn]\left[\begin{matrix}y_1\\y_2\\y_3\\...\\...\\y_n\end{matrix}\right]=\left [ \begin{matrix}1&x_{11}&x_{12}&...&x_{1p}\\ 1&x_{21}&x_{22}&...&x_{2p}\\1&x_{31}&x_{32}&...&x_{3p}\\...&... &... &... &...\\... &... &... &... &...\\ 1&x_{n1}&x_{n2}&...&x_{np}\end{matrix} \right]\left[\begin{matrix}\beta_0\\\beta_1\\\beta_2\\...\\...\\\beta_p\end{matrix}\right]+\left[\begin{matrix}\epsilon_1\\\epsilon_2\\\epsilon_3\\...\\...\\\epsilon_n\end{matrix}\right]

or

y=Xβ+ϵ\boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{\epsilon}

  • Ordinary least squares: analytic solution for regression coefficients
    普通最小二乘法:回归系数的解析解

β^=(XTX)1XTy\widehat{\boldsymbol \beta} = (\mathbf X^T \mathbf X )^{-1} \mathbf X^T \mathbf y

Scikit-Learn: Scikit-Learn

from sklearn.linear_model import LinearRegression linear_reg = LinearRegression() linear_reg.fit(features, target variable) linear_reg.predict(features)
from sklearn.linear_model import LinearRegression linear_reg = LinearRegression() linear_reg.fit(features, target variable) linear_reg.predict(features)

Note: .coef_ is a list. This means that you will need to use .coef_[0] to extract out the value of β1​
注意: .coef_ 是一个列表。这意味着您需要使用 .coef_[0] 提取出 β1 的值。