在python中使用矩阵

时间:2018-10-01 14:37:16

标签: python numpy matrix python-requests

如何获取x_matrix的输出(y_matrixget_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

1 个答案:

答案 0 :(得分:0)

您可以将get_x_and_y_matrix函数的返回值保存在变量xy中,然后将它们作为参数传递给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)