JavaScript
x
35
35
1
[c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnbase.py:450: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names
2
warnings.warn(
3
Output exceeds the size limit. Open the full output data in a text editor
4
---------------------------------------------------------------------------
5
ValueError Traceback (most recent call last)
6
d:pythontugas PTIcoba coding.ipynb Cell 5 in <cell line: 1>()
7
----> 1 reg.predict(2600)
8
9
File c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnlinear_model_base.py:386, in LinearModel.predict(self, X)
10
372 def predict(self, X):
11
373 """
12
374 Predict using the linear model.
13
375
14
( )
15
384 Returns predicted values.
16
385 """
17
--> 386 return self._decision_function(X)
18
19
File c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnlinear_model_base.py:369, in LinearModel._decision_function(self, X)
20
366 def _decision_function(self, X):
21
367 check_is_fitted(self)
22
--> 369 X = self._validate_data(X, accept_sparse=["csr", "csc", "coo"], reset=False)
23
370 return safe_sparse_dot(X, self.coef_.T, dense_output=True) + self.intercept_
24
25
File c:UsersACERAppDataLocalProgramsPythonPython39libsite-packagessklearnbase.py:577, in BaseEstimator._validate_data(self, X, y, reset, validate_separately, **check_params)
26
575 raise ValueError("Validation should be done on X, y or both.")
27
576 elif not no_val_X and no_val_y:
28
--> 577 X = check_array(X, input_name="X", **check_params)
29
30
878 if array.ndim == 1:
31
32
ValueError: Expected 2D array, got scalar array instead:
33
array=2600.
34
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.
35
Advertisement
Answer
You provide a scalar value to .predict
method. You need to provide a 2-dimensional array:
JavaScript
1
7
1
X = 2600
2
X = np.array(X)
3
print(X.shape) # shape: ()
4
X = X.reshape(-1,1)
5
print(X.shape) # shape: (1,1)
6
reg.predict(X)
7