[英]Machine Learning (python) Saving a scaler in a model
我正在使用以下縮放器:
scaler = preprocessing.MinMaxScaler()
X_train = pd.DataFrame(scaler.fit_transform(dataset_train),
columns=dataset_train.columns,
index=dataset_train.index)
X_train.sample(frac=1)
X_test = pd.DataFrame(scaler.transform(dataset_test),
columns=dataset_test.columns,
index=dataset_test.index)
我用於異常檢測的模型是sequential()。
我知道如何保存模型,但我的問題是如何將縮放器保存在模型中,所以我可以簡單地將模型應用到新的 df 中。
謝謝你。
這是 sklearn 管道的一個很好的用例。 sklearn 管道允許您將預處理步驟與模型鏈接起來。 大多數你想應用.fit()
方法的東西都可以放在sklearn 管道中。 這樣,當您持久保存管道時,重要信息會在您保存的模型對象中捕獲。 您可以在 sklearn 管道頁面上找到更多信息。 這是一個例子:
from sklearn.preprocessing import MinMaxScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn import datasets
from sklearn.model_selection import train_test_split
# creating an example dataset
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
dataset_train, dataset_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42)
#example of creating and predicting with a sklearn pipeline
pipeline = Pipeline([('scaler', MinMaxScaler()), ('rf', RandomForestClassifier())])
pipeline.fit(dataset_train, y_train)
pipeline.predict(dataset_test)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.