这是我的预测模型。
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')
它显示此错误:
ValueError: X has 26 features, but StandardScaler is expecting 25 features as input.
完整的错误信息
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.
编辑:
new_data_array.shape
(1, 26)
回答1
在使用定标器之前,必须使用训练数据对定标器进行训练。
您似乎跳过了这一部分。如果不是,请发布您用于适合标准缩放器的代码。
似乎您训练的缩放器是在形状为 (num_samples, 25)
的数据集上完成的。
确保在执行 scaler = preprocessing.StandardScaler().fit(data)
之前检查 data.shape[1]
是否为 26
。