使用make_pipeline的StandardScaler

如果我使用make_pipeline,我是否还需要使用fittransform函数来拟合我的模型和转换数据,还是它会自动执行这些功能?
另外,StandardScaler是否也执行归一化,还是仅执行缩放?
解释代码:我想应用PCA,然后使用SVM进行归一化处理。

pca = PCA(n_components=4).fit(X) X = pca.transform(X)# training a linear SVM classifier 5-foldfrom sklearn.svm import SVCfrom sklearn.model_selection import cross_val_scoreclf = make_pipeline(preprocessing.StandardScaler(), SVC(kernel = 'linear'))   scores = cross_val_score(clf, X, y, cv=5)

我还有些困惑,如果我在下面的代码中不使用fit函数,会发生什么情况:

from sklearn.svm import SVCfrom sklearn.model_selection import cross_val_scoreclf = SVC(kernel = 'linear', C = 1)scores = cross_val_score(clf, X, y, cv=5)


回答:

StandardScaler既执行归一化也执行缩放。

cross_val_score()会为你拟合(转换)数据集,因此你不需要显式调用它。

更常见的方法是将所有步骤(StandardScaler, PCA, SVC)放在一个pipeline中,并使用GridSearchCV来调整超参数和选择最佳参数(估计器)。

示例:

pipe = Pipeline([        ('scale', StandardScaler()),        ('reduce_dims', PCA(n_components=4)),        ('clf', SVC(kernel = 'linear', C = 1))])param_grid = dict(reduce_dims__n_components=[4,6,8],                  clf__C=np.logspace(-4, 1, 6),                  clf__kernel=['rbf','linear'])grid = GridSearchCV(pipe, param_grid=param_grid, cv=3, n_jobs=1, verbose=2)grid.fit(X_train, y_train)print(grid.score(X_test, y_test))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注