'How to reshape input data array while building a prediction model?

This is my prediction model.

input_data = (13,15,-0.0002,-0.0004,100,518.67,644.26,1610.89,1430.32,14.62,21.61,550.99,2388.15,9153.32,1.3,48.22,2388.16,8211.76,8.5477,0.03,397,2388,100,38.53,23.1291,1)

 # changing input_data to an array using numpy
   input_data_array = np.asarray(input_data)

 #reshaping the array 
  new_data_array = input_data_array.reshape(1, -1)
#standardize the data
 standard_data = scalar.transform(new_data_array)

#predicting if the machine will function or not
prediction = model.predict(standard_data)

if prediction == 1:
  print('\nUnit will fail.\n')
else:
  print('\nNo issues with this unit.\n')

It's showing this error:

ValueError: X has 26 features, but StandardScaler is expecting 25 features as input.

Full error message

ValueError                                Traceback (most recent call last)
<ipython-input-17-f946e9218fa1> in <module>()
        7 new_data_array = input_data_array.reshape(1, -1)
        8 #standardize the data
  ----> 9 standard_data = scalar.transform(new_data_array)
        10 
        11 #predicting if the machine will function or not 

        2 frames
      /usr/local/lib/python3.7/dist-packages/sklearn/base.py in _check_n_features(self, X, reset)
        399         if n_features != self.n_features_in_:
        400             raise ValueError(
    --> 401                 f"X has {n_features} features, but {self.__class__.__name__} "
        402                 f"is expecting {self.n_features_in_} features as input."
        403             )

        ValueError: X has 26 features, but StandardScaler is expecting 25 features as input.

EDIT:

new_data_array.shape

(1, 26)


Solution 1:[1]

Before using a scaler, the scaler must be trained using training data.

It seems that you have skipped this part. If not please post the code you have used to fit the standard scaler.

Seems like the scaler that you trained was done on a dataset with a shape (num_samples, 25).

Make sure that before doing scaler = preprocessing.StandardScaler().fit(data) you check that data.shape[1] is 26.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1