Error while using reg.predict.
I have to do the following steps
new_data = pd.DataFrame(data=[1740,1780],columns=['SAT'])
reg.predict(new_data)
I get an error when I do reg.predict(1780).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-e0998427f9f5> in <module>
----> 1 reg.predict([1780])
/usr/local/Cellar/jupyterlab/3.0.7/libexec/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)
/usr/local/Cellar/jupyterlab/3.0.7/libexec/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_
/usr/local/Cellar/jupyterlab/3.0.7/libexec/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
/usr/local/Cellar/jupyterlab/3.0.7/libexec/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)
635 # If input is 1D raise error
636 if array.ndim == 1:
--> 637 raise ValueError(
638 "Expected 2D array, got 1D array instead:\narray={}.\n"
639 "Reshape your data either using array.reshape(-1, 1) if "
ValueError: Expected 2D array, got 1D array instead:
array=[1780].
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.
Hello Rajeev,
Thank you for reaching out. Whenever such errors arise, it is best practice to carefully examine the error message Python offers.
In this case, at the bottom of the error we read:
ValueError: Expected 2D array, got 1D array instead:
array=[1780].
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.
That would suggest that predict(X) expects an argument that is array-like, namely - a 2D array. Indeed, as discussed in the
video lesson, referring to sklean's official documentation on LinearRegression could also confirm our suspicions.
(check at: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html).
It goes, X: array-like or sparse matrix, shape (n_samples, n_features)
---------------------------------------
The solution is simply to cast your data to an ndarray (using np.array) and to reshape it as suggested (using array.reshape(-1, 1)):
reg.predict(np.array(1780).reshape(-1, 1))
This would yield the result for a single input. For multiple-entry input, use pd.DataFrame like in the video lecture.
--------------------------------------
Best,
A., The 365 Team
try this one,
reg.predict([[1780]])
now it is converted to 2D array
I encountered this problem as well, and arrived at the same solution as Haidar.
However, I'm confused as to why the lecturer in the video is able to use reg.predict(1760). Has there been an update to this method that no longer allows scalars?