我用 StandartScaler 腌制了 KNN 模型。
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
當我嘗試通過 StandartScaler().transform() 加載模型并傳遞新值時,它給了我一個錯誤:
sklearn.exceptions.NotFittedError: This StandardScaler instance is not
fitted yet. Call 'fit' with appropriate arguments before using this estimator.
我試圖從字典中加載值
dic = {'a':1, 'b':32323, 'c':12}
sc = StandartScaler()
load = pickle.load(open('KNN.mod'), 'rb'))
load.predict(sc.transform([[dic['a'], dic['b'], dic['c']]]))
據我了解,我必須將新資料擬合到 sc。但如果這樣做,它會給我錯誤的預測。我不確定我的我是否過度擬合或 smth,隨機森林和決策樹在沒有 sc 的情況下可以正常作業。邏輯回歸半確定
uj5u.com熱心網友回復:
您需要同時訓練和腌制整個機器學習管道。這可以使用sklearn的Pipeline工具來完成。在你的情況下,它看起來像:
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.neighbors import NearestNeighbors
pipeline = Pipeline([('scaler', StandardScaler()), ('knn', NearestNeighbors())])
pipe.fit(X_train, y_train)
# save the ml pipeline
pickle.dump(pipeline, open('KNN_pipeline.pkl'), 'wb'))
# load the ml pipeline and do prediction
pipeline = pickle.load(open('KNN_pipeline.pkl'), 'rb'))
pipeline.predict(X_test)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/462413.html
下一篇:選擇資料點鄰域來支持向量
