Last answered:

03 Apr 2020

Posted on:

03 Apr 2020

0

Simple Linear Regression with sklearn - Summary Table

when I tried to follow the instruction of the following reg.predict(1740)   it shows me it is not a 2D array, how to make it work? How to make a single value become a 2D array Thanks.  
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-476016193ddb> in <module>
      1 # There is a dedicated method should we want to predict values
      2 # Note that the result is an array, as we can predict more than one value at a time
----> 3 reg.predict(1740)

~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in predict(self, X)
    223             Returns predicted values.
    224         """
--> 225         return self._decision_function(X)
    226 
    227     _preprocess_data = staticmethod(_preprocess_data)

~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py in _decision_function(self, X)
    205         check_is_fitted(self)
    206 
--> 207         X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
    208         return safe_sparse_dot(X, self.coef_.T,
    209                                dense_output=True) + self.intercept_

~\anaconda3\lib\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, warn_on_dtype, estimator)
    547                     "Reshape your data either using array.reshape(-1, 1) if "
    548                     "your data has a single feature or array.reshape(1, -1) "
--> 549                     "if it contains a single sample.".format(array))
    550             # If input is 1D raise error
    551             if array.ndim == 1:

ValueError: Expected 2D array, got scalar array instead:
array=1740.
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.
1 answers ( 0 marked as helpful)
Instructor
Posted on:

03 Apr 2020

2
Hi liqian, thanks for reaching out! Instead of the line: reg.predict(1740) try the following two lines: new_data = np.array(1740).reshape(-1,1)
reg.predict(new_data) This should fix the issue. Let me know how it goes!   Best,  Eli

Submit an answer