我尝试运行我的机器学习线性回归代码,但它无法正常工作。以下是我的代码:
from sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LinearRegressionimport pandas as pddf = pd.read_csv(r'C:\Users\SVISHWANATH\Downloads\datasets\GGP_data.csv')df["OHLC"] = (df.open+df.high+df.low+df.close)/4df['HLC'] = (df.high+df.low+df.close)/3df.index = df.index+1reg = LinearRegression()reg.fit(df.index, df.OHLC)
基本上,我只是导入了一些库,使用了read_csv函数,并调用了LinearRegression()函数,得到的错误信息如下:
ValueError: Expected 2D array, got 1D array instead:array=[ 1 2 3 ... 1257 1258 1259].Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample
谢谢!
回答:
正如错误信息中提到的,你需要给fit方法传递一个2D数组。df.index是一个1D数组。你可以这样做:
reg.fit(df.index.values.reshape(-1, 1), df.OHLC)