0

I'm running a rating on a my clip from Sentinel-2 as a reference to the diagram shown on: https://github.com/ceholden/open-geo-tutorial/blob/master/Python/chapters/chapter_5_classification.ipynb

Following the step by step example I arrived at the prediction of every pixel, but in my case launching my code riche gives me the following error:

# Take our full image, ignore the Fmask band, and reshape into long 2d array (nrow * ncol, nband) for classification
new_shape = (img.shape[0] * img.shape[1], img.shape[2] -1 )

img_as_array = img[:, :, :7].reshape(new_shape)
print('Reshaped from {o} to {n}'.format(o=img.shape,
                                    n=img_as_array.shape))

# Now predict for each pixel
class_prediction = rf.predict(img_as_array)

# Reshape our classification map
class_prediction = class_prediction.reshape(img[:, :, 0].shape)

(Output): Reshaped from (4644, 5661, 8) to (26289684, 7)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-4f25164e3bae> in <module>
      7 
      8 # Now predict for each pixel
----> 9 class_prediction = rf.predict(img_as_array)
      10 
      11 # Reshape our classification map

~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in predict(self, X)
      543             The predicted classes.
      544         """
----> 545         proba = self.predict_proba(X)
      546 
      547         if self.n_outputs_ == 1:

~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in predict_proba(self, X)
     586         check_is_fitted(self, 'estimators_')
     587         # Check data
---> 588         X = self._validate_X_predict(X)
     589 
     590         # Assign chunk of trees to jobs

~\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py in _validate_X_predict(self, X)
     357                                  "call `fit` before exploiting the model.")
     358 
---> 359         return self.estimators_[0]._validate_X_predict(X, check_input=True)
     360 
     361     @property

 ~\Anaconda3\lib\site-packages\sklearn\tree\tree.py in _validate_X_predict(self, X, check_input)
     400                              "match the input. Model n_features is %s and "
     401                              "input n_features is %s "
---> 402                              % (self.n_features_, n_features))
     403 
     404         return X

 ValueError: Number of features of the model must match the input. Model n_features is 8 and input n_features is 7 

Being new with python I ask you how to solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.