Rashaad
Rashaad

Reputation: 73

sklearn linear_model LinearRegression, ValueError: Expected 2D array, got 1D array instead

I am trying to fit data to my model,

This is the data

le = sklearn.preprocessing.LabelEncoder()
date = le.fit_transform(list(data["Date"]))
_open = le.fit_transform(list(data["Open"]))
high = le.fit_transform(list(data["High"]))
low = le.fit_transform(list(data["Low"]))
adj_close = le.fit_transform(list(data["Adj Close"]))
volume = le.fit_transform(list(data["Volume"]))

X = list(date)
y = list(zip(high, low, _open, adj_close, volume))

x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.1)

But when I try to fit the data into the model as displayed below

linear = sklearn.linear_model.LinearRegression()
linear.fit(x_train, y_train)

I get this error

ValueError: Expected 2D array, got 1D array instead:
array=[2088  311 1839 ... 2422   64 1705].
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

Upvotes: 0

Views: 263

Answers (1)

Marc
Marc

Reputation: 279

Try this

x_train= x_train.reshape(-1, 1)
x_test = x_test.reshape(-1, 1)

Upvotes: 1

Related Questions