如何在scikit-learn中对SVM应用标准化?

pem*_*ahl 25 python classification normalization svm scikit-learn

我正在使用当前稳定版0.13的scikit-learn.我正在使用类将线性支持向量分类器应用于某些数据sklearn.svm.LinearSVC.

关于 scikit-learn文档中的预处理章节中,我已经阅读了以下内容:

在学习算法的目标函数中使用的许多元素(例如支持向量机的RBF内核或线性模型的l1和l2正则化器)假设所有特征都以零为中心并且具有相同顺序的方差.如果某个要素的方差比其他要大一个数量级,那么它可能会主导目标函数并使估算工具无法按预期正确地学习其他要素.

问题1:标准化对于SVM通常是否有用,对于那些具有线性内核函数的人来说也是如此?

问题2:据我所知,我必须计算训练数据的均值和标准差,并使用该类对测试数据应用相同的转换sklearn.preprocessing.StandardScaler.但是,我不明白的是,在将训练数据提供给SVM分类器之前,我是否还必须转换训练数据或仅转换测试数据.

也就是说,我必须这样做:

scaler = StandardScaler()
scaler.fit(X_train)                # only compute mean and std here
X_test = scaler.transform(X_test)  # perform standardization by centering and scaling

clf = LinearSVC()
clf.fit(X_train, y_train)
clf.predict(X_test)
Run Code Online (Sandbox Code Playgroud)

或者我必须这样做:

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)  # compute mean, std and transform training data as well
X_test = scaler.transform(X_test)  # same as above

clf = LinearSVC()
clf.fit(X_train, y_train)
clf.predict(X_test)
Run Code Online (Sandbox Code Playgroud)

简而言之,我是否必须使用scaler.fit(X_train)或使用scaler.fit_transform(X_train)训练数据才能获得合理的结果LinearSVC

And*_*ler 35

都不是.

scaler.transform(X_train)没有任何影响.该transform操作不到位.你必须做

X_train = scaler.fit_transform(X_train)

X_test = scaler.transform(X_test)
Run Code Online (Sandbox Code Playgroud)

要么

X_train = scaler.fit(X_train).transform(X_train)
Run Code Online (Sandbox Code Playgroud)

您始终需要对训练或测试数据执行相同的预处理.是的,如果标准化反映了您对数据的信任,那么标准化总是好的.特别是对于kernel-svms,它通常是至关重要的.


小智 6

为什么不一次使用Pipeline链接(或组合)变压器和估算器?为您省去单独拟合和转换数据的麻烦,然后使用估算器.它也会节省一些空间.

from sklearn.pipeline import Pipeline

pipe_lrSVC = Pipeline([('scaler', StandardScaler()), ('clf', LinearSVC())])
pipe_lrSVC.fit(X_train, y_train)
y_pred = pipe_lrSVC.predict(X_test)
Run Code Online (Sandbox Code Playgroud)