【问题标题】:Ordinary Least Squares linear regression predictions from X & Beta without Sklearn没有 Sklearn 的 X 和 Beta 的普通最小二乘线性回归预测
【发布时间】:2021-03-24 18:30:35
【问题描述】:

如何从以下获得具有 Beta 和 X 值的预测:

数据

import numpy as np
x = np.array([20, 25, 28, 29, 30])
y = np.array([20000, 25000, 26000, 30000, 32000])

变换

# Reshape X    
X = x.reshape((-1, 1))

# Append ones to X:
X = np.append(np.ones((len(X), 1)), X, axis=1)

# Matrix X
X = np.matrix(X)

# Transpose X
XT = np.matrix.transpose(X)

# Matrix y
y = y.reshape((-1, 1))
y = np.matrix(y)

# Multiplication
XT_X = np.matmul(XT, X)
XT_y = np.matmul(XT, y)

# Betas
Betas = np.matmul(np.linalg.inv(XT_X), XT_y)

现在我需要获得我的 预测 来绘制并查看它的拟合程度:

import seaborn as sns
X = x
sns.regplot(X, prediction, fit_reg=False)
sns.regplot(X, y, fit_reg=False)

我如何计算预测,根据 X 值获得我的新 y?

谢谢!

【问题讨论】:

    标签: python numpy linear-regression


    【解决方案1】:

    X @ Betas:

    predictions = np.array(X @ Betas).ravel()
    x_values = np.array(X[:, 1]).ravel()  # disregard the 1s column which is for bias
    y_values = np.array(y).ravel()
    
    sns.regplot(x_values, predictions, fit_reg=False, label="preds")
    sns.regplot(x_values, y_values, fit_reg=False, label="truth")
    plt.legend()
    

    我不确定你为什么选择np.matrix;我认为你可以坚持使用np.array

    【讨论】:

    • 我遇到了一个错误,因为我把那些留在了 X 中。这非常有效,非常感谢。
    猜你喜欢
    • 2019-05-14
    • 2015-02-12
    • 2010-12-19
    • 2013-10-11
    • 2016-11-19
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多