1

I tried running my Machine Learning LinearRegression code, but it is not working. Here is the code:

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pandas as pd
df = pd.read_csv(r'C:\Users\SVISHWANATH\Downloads\datasets\GGP_data.csv')
df["OHLC"] = (df.open+df.high+df.low+df.close)/4
df['HLC'] = (df.high+df.low+df.close)/3
df.index = df.index+1
reg = LinearRegression()
reg.fit(df.index, df.OHLC)

Basically, I just imported a few libraries, used the read_csv function, and called the LinearRegression() function, and this is the error:

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

Thanks!

1 Answer 1

1

As mentioned in the error message, you need to give the fit method a 2D array. df.index is a 1D array. You can do it this way:

reg.fit(df.index.values.reshape(-1, 1), df.OHLC)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.