gpt4 book ai didi

python - Python 中的 OLS : dimension of estimated coefficient matrix is not OK

转载 作者:行者123 更新时间:2023-12-01 09:32:50 24 4
gpt4 key购买 nike

我编写了一个Python代码来估计(单变量)线性回归模型的参数,但是估计系数矩阵的维数不合适。向量“beta_estimated”(下面报告)的维度应该是 (1×1),但实际上是 (n×n)。有什么想法吗?

# Linear regression in Python (univariate)
import numpy as np
import scipy as sci

x_original = np.random.normal(1,1, 100)
x = np.array([x_original])
#x_transpose = x.T
beta = [5]

y_original = (beta * x) + np.random.normal(0, 10, 100)
y = np.array(y_original)
#y_transpos = y.T
product1 = np.dot(x.T, x)
product2 = np.dot(x.T, y )
Minv = np.linalg.inv(product1)
beta_estimated = np.dot(Minv, product2)

最佳答案

我不确定您想做什么,但如果我不得不猜测该产品是您没有所需尺寸的原因。对于您的情况:

x   # has a shape of (1, 100)
x.T # has a shape of (100, 1)

np.dot(x.T, x) # produces an array with shape (100, 100)
np.dot(x, x.T) # would produce an array with a shape of (1, 1)

因此,如果我认为您应该反转两个点积中的参数np.dot()。你的代码应该变成:

product1 = np.dot(x, x.T)    
product2 = np.dot(y, x.T)

关于python - Python 中的 OLS : dimension of estimated coefficient matrix is not OK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49806587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com