TransWikia.com

unable to predict by LinearRegression

Data Science Asked on July 11, 2021

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?

One Answer

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']]).

Correct answer by Oxbowerce on July 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP