Skip to content
Advertisement

why i can’t predict my x value in linear regression model using reg.predict ( )

[c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnbase.py:450: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names
  warnings.warn(
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
d:pythontugas PTIcoba coding.ipynb Cell 5 in <cell line: 1>()
----> 1 reg.predict(2600)

File c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnlinear_model_base.py:386, in LinearModel.predict(self, X)
    372 def predict(self, X):
    373     """
    374     Predict using the linear model.
    375 
   (...)
    384         Returns predicted values.
    385     """
--> 386     return self._decision_function(X)

File c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnlinear_model_base.py:369, in LinearModel._decision_function(self, X)
    366 def _decision_function(self, X):
    367     check_is_fitted(self)
--> 369     X = self._validate_data(X, accept_sparse=["csr", "csc", "coo"], reset=False)
    370     return safe_sparse_dot(X, self.coef_.T, dense_output=True) + self.intercept_

File c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnbase.py:577, in BaseEstimator._validate_data(self, X, y, reset, validate_separately, **check_params)
    575     raise ValueError("Validation should be done on X, y or both.")
    576 elif not no_val_X and no_val_y:
--> 577     X = check_array(X, input_name="X", **check_params)
...
    878     if array.ndim == 1:

ValueError: Expected 2D array, got scalar array instead:
array=2600.
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.

Advertisement

Answer

You provide a scalar value to .predict method. You need to provide a 2-dimensional array:

X = 2600
X = np.array(X)
print(X.shape) # shape: ()
X = X.reshape(-1,1)
print(X.shape) # shape: (1,1)
reg.predict(X)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement