dataquestio / project-walkthroughs

Data science, machine learning, and web development project code for https://www.youtube.com/c/Dataquestio .
954 stars 1.11k forks source link

The model.predict() function parameter is malformed. #7

Open yoyofreeman opened 1 year ago

yoyofreeman commented 1 year ago

market_prediction.ipynb

Code In[42]


from sklearn.metrics import precision_score
preds = model.predict(test[predictors])
preds = pd.Series(preds, index=test.index)
precision_score(test["Target"], preds)

Error info


/home/tony/anaconda3/envs/prophet/lib/python3.9/site-packages/sklearn/base.py:450: UserWarning: X does not have valid feature names, but RandomForestClassifier was fitted with feature names
  warnings.warn(
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [90], line 2
      1 from sklearn.metrics import precision_score
----> 2 preds = model.predict(test[predictors])
      3 preds = pd.Series(preds, index=test.index)
      4 precision_score(test["Target"], preds)

File ~/anaconda3/envs/prophet/lib/python3.9/site-packages/sklearn/ensemble/_forest.py:832, in ForestClassifier.predict(self, X)
    811 def predict(self, X):
    812     """
    813     Predict class for X.
    814 
   (...)
    830         The predicted classes.
    831     """
--> 832     proba = self.predict_proba(X)
    834     if self.n_outputs_ == 1:
    835         return self.classes_.take(np.argmax(proba, axis=1), axis=0)

File ~/anaconda3/envs/prophet/lib/python3.9/site-packages/sklearn/ensemble/_forest.py:874, in ForestClassifier.predict_proba(self, X)
    872 check_is_fitted(self)
    873 # Check data
--> 874 X = self._validate_X_predict(X)
    876 # Assign chunk of trees to jobs
    877 n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs)

File ~/anaconda3/envs/prophet/lib/python3.9/site-packages/sklearn/ensemble/_forest.py:605, in BaseForest._validate_X_predict(self, X)
    602 """
    603 Validate X whenever one tries to predict, apply, predict_proba."""
    604 check_is_fitted(self)
--> 605 X = self._validate_data(X, dtype=DTYPE, accept_sparse="csr", reset=False)
    606 if issparse(X) and (X.indices.dtype != np.intc or X.indptr.dtype != np.intc):
    607     raise ValueError("No support for np.int64 index based sparse matrices")

File ~/anaconda3/envs/prophet/lib/python3.9/site-packages/sklearn/base.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)
    578     out = X
    579 elif no_val_X and not no_val_y:

File ~/anaconda3/envs/prophet/lib/python3.9/site-packages/sklearn/utils/validation.py:879, 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, input_name)
    877     # If input is 1D raise error
    878     if array.ndim == 1:
--> 879         raise ValueError(
    880             "Expected 2D array, got 1D array instead:\narray={}.\n"
    881             "Reshape your data either using array.reshape(-1, 1) if "
    882             "your data has a single feature or array.reshape(1, -1) "
    883             "if it contains a single sample.".format(array)
    884         )
    886 if dtype_numeric and array.dtype.kind in "USV":
    887     raise ValueError(
    888         "dtype='numeric' is not compatible with arrays of bytes/strings."
    889         "Convert your data to numeric values explicitly instead."
    890     )

ValueError: Expected 2D array, got 1D array instead:
array=[3.82533e+03 4.04695e+09 3.78100e+03 3.82982e+03 3.75210e+03].
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.

My system environment


~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.5 LTS (Focal Fossa)"

conda 22.9.0
ipython                   8.6.0 
ipython_genutils          0.2.0
numpy                     1.23.4
numpy-base                1.23.4
pandas                    1.5.1
python                    3.9.15
python-dateutil           2.8.2
python-fastjsonschema     2.16.2
scikit-learn              1.1.3
yfinance                  0.1.87

Can you give me some tips? Thank you.