I started learning Sklearn today and wanted to set up a simple regression model using one X value, then use that model to predict the next 5 Y values.
I keep getting the following error:
ValueError: Expected 2D array, got 1D array instead:
array=[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20].
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.
This is all of the code I currently have:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn import datasets
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score
histYlist = [65.98685126, 56.78302597, 94.79205606, 20.70468985, 90.25959717, 60.6194846, 27.08425346, 15.8715957, 69.41508813, 7.362478092, 82.45058027, 53.44812953, 32.52252966, 43.1278569, 91.2854072, 99.80998002, 89.02868447, 45.31115274, 98.65222136, 70.56292757]
histY = np.array(histYlist)
histY.reshape(-1,1)
histXlist = list()
i = 1
for items in histY :
histXlist.append(i)
i = i + 1
histX = np.array(histXlist)
histX.reshape(-1,1)
print(histX.shape, histY.shape)
#print(histX)
#rint(len(histX))
model = linear_model.LinearRegression()
model.fit(histX,histY)
X_pred = list()
newPredicts = 5
j = 1
while j <= newPredicts :
X_pred.append(len(histX) + j)
j = j + 1
#print(predictX)
Y_pred = model.predict(X_pred)
print(Y_pred)
Hopefully you can help or lead me in the right direction thanks!
reshapeX_predtoo, just like you did forhistXandhistY.