线性回归脚本在Python中无法运行

我尝试运行我的机器学习线性回归代码,但它无法正常工作。以下是我的代码:

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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注