0
$\begingroup$

enter image description here Should I add csv as text in SO question? There's lot more data.

%matplotlib inline
plt.xlabel('Year')
plt.ylabel('Income($US)')
plt.scatter(df.year,df.income,color='red',marker='+')

enter image description here

reg = linear_model.LinearRegression()
reg.fit(df[['year']],df.income)

Output : LinearRegression()

reg.predict('10000')

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in ----> 1 reg.predict('10000')

~/.local/lib/python3.9/site-packages/sklearn/linear_model/_base.py in predict(self, X) 236 Returns predicted values. 237 """ --> 238 return self._decision_function(X) 239 240 _preprocess_data = staticmethod(_preprocess_data)

~/.local/lib/python3.9/site-packages/sklearn/linear_model/base.py in decision_function(self, >X) 218 check_is_fitted(self) 219 --> 220 X = check_array(X, accept_sparse=['csr', 'csc', 'coo']) 221 return safe_sparse_dot(X, self.coef.T, 222 dense_output=True) + self.intercept

~/.local/lib/python3.9/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 if extra_args <= 0: ---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0

~/.local/lib/python3.9/site-packages/sklearn/utils/validation.py in check_array(array, >accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd,

ensure_min_samples, ensure_min_features, estimator) 628 # If input is scalar raise error 629 if array.ndim == 0: --> 630 raise ValueError( 631 "Expected 2D array, got scalar array instead:\narray={}.\n" 632 "Reshape your data either using array.reshape(-1, 1) if "

ValueError: Expected 2D array, got scalar array instead: array=10000. 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 am not sure why I am getting above error. I have income lists of some years. So, I graph it. When I was trying to predict a data from linearRegression I got the error. I am new to ML(Machine Learning)

How to solve it? What am I missing?

$\endgroup$
0
1
$\begingroup$

The input to the predict method should be a 2d array of shape (n_samples, n_features), which in your case with one feature and one sample would be (1, 1). Therefore add a second dimension for the number of samples: reg.predict([['10000']]).

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.