【问题标题】:Linear Regression issues线性回归问题
【发布时间】:2018-11-17 12:39:29
【问题描述】:

我正在尝试对 2 列数据(IMF_VALUES、BBG_FV)进行线性回归

我有这个代码:

import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
import pandas as pd
raw_data = pd.read_csv("IMF and BBG Fair Values.csv")
ISO_TH = raw_data[["IMF_VALUE","BBG_FV"]]


filtered_TH = ISO_TH[np.isfinite(raw_data['BBG_FV'])]

npMatrix = np.matrix(filtered_TH) 
IMF_VALUE, BBG_FV = npMatrix[:,0], npMatrix[:,1]


regression = linear_model.LinearRegression
regression.fit(IMF_VALUE, BBG_FV)

当我将它作为测试运行时,我得到了这个错误,我真的不知道为什么:

TypeError                                 Traceback (most recent call last)
<ipython-input-28-1ee2fa0bbed1> in <module>()
      1 regression = linear_model.LinearRegression
----> 2 regression.fit(IMF_VALUE, BBG_FV)

TypeError: fit() missing 1 required positional argument: 'y'

【问题讨论】:

  • 检查IMF_VALUEBBG_FV 值的形状。 BBG_FV 可能不是数组。
  • 您忘记初始化回归对象。你需要做:regression = linear_model.LinearRegression()。观察最后的括号。没有它们,regression 是一个类,而不是初始化的对象模型。

标签: python scikit-learn statistics linear-regression


【解决方案1】:

确保两者都是一维数组:

regression.fit(np.array(IMF_VALUE).reshape(-1,1), np.array(BBG_FV).reshape(-1,1))

【讨论】:

  • 嘿 - 非常感谢你们。错误消失了!我会继续努力!