现在我一直在解决扩展新数据的问题。在我的方案中,我已经训练并测试了模型,所有 x_train 和 x_test 都使用 sklearn.MinMaxScaler() 进行了缩放。然后,应用于实时过程,我如何在训练和测试数据的相同规模下缩放新输入。
步骤如下
featuresData = df[features].values # Array of all features with the length of thousands
sc = MinMaxScaler(feature_range=(-1,1), copy=False)
featuresData = sc.fit_transform(featuresData)
#Running model to make the final model
model.fit(X,Y)
model.predict(X_test)
#Saving to abcxyz.h5
然后用新数据实现
#load the model abcxyz.h5
#catching new data
#Scaling new data to put into the loaded model << I'm stucking in this step
#...
那么如何缩放新数据进行预测,然后逆变换为最终结果呢?从我的逻辑来看,在训练模型之前,它需要以与旧缩放器相同的方式进行缩放
请帮忙!
最佳答案
从你使用 scikit-learn 的方式来看,你需要已经保存了变压器:
import joblib
# ...
sc = MinMaxScaler(feature_range=(-1,1), copy=False)
featuresData = sc.fit_transform(featuresData)
joblib.dump(sc, 'sc.joblib')
# with new data
sc = joblib.load('sc.joblib')
transformData = sc.transform(newData)
# ...
使用 scikit-learn 的最佳方法是将转换与模型合并。这样,您只需保存包含转换管道的模型。
from sklearn import svm
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
clf = svm.SVC(kernel='linear')
sc = MinMaxScaler(feature_range=(-1,1), copy=False)
model = Pipeline([('scaler', sc), ('svc', clf)])
#...
当您这样做时
model.fit
,首先模型会做fit_transform
用于引擎盖下的洁牙机。与 model.predict
, transform
将涉及您的缩放器。
关于python - 如何在旧的 MinMaxScaler 上重新缩放新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59575492/