I have a df containing columns 'year' and 'per capita income (US$)'.
plt.scatter(df.year, df['per capita income (US$)'], color='red')
plt.xlabel('Year')
plt.ylabel('Per Capita Income (US$)')
plt.show()
reg = linear_model.LinearRegression()
reg.fit(df[['year']], df['per capita income (US$)'])
reg.predict(2011)
Error message received:
ValueError: Expected 2D array, got scalar array instead:
array=2011.
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.
I adjusted the call of reg.predict() to:
reg.predict([[2011]])
Code executed without error, however, the .predict() function didn't return the desired output.
print(df.columns)
Index(['year', 'per capita income (US$)'], dtype='object')