如何获取x_matrix
的输出(y_matrix
和get_x_and_y_matrix
)并将这些矩阵用作ols_coefficients
函数的输入?每次我都会收到一条错误消息。
def get_x_and_y_matrix(data_set):
ones = np.ones(shape=(len(data_set), 1))
x = data_set[:, 0:2]
y_matrix = data_set[:, 2]
x_matrix = np.concatenate((ones, x), axis=1)
return x_matrix, y_matrix
def ols_coefficients(x, y):
x, y = get_x_and_y_matrix(data_set=generate_data(n_obs))
intercept = np.ones(shape=(len(x), 1))
new_x = np.concatenate((intercept, x), axis=1)
beta_hat = np.dot(np.linalg.inv(np.dot(intercept, new_x)), np.dot(intercept, y))
return beta_hat
答案 0 :(得分:0)
您可以将get_x_and_y_matrix
函数的返回值保存在变量x
和y
中,然后将它们作为参数传递给ols_coefficients
。
def get_x_and_y_matrix(data_set):
# rest of the function
return x_matrix, y_matrix
def ols_coefficients(x, y):
# rest of the function
return beta_hat
x, y = get_x_and_y_matrix(data_set)
beta = ols_coefficients(x, y)