相关疑难解决方法(0)

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

我正在使用当前稳定版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)训练数据才能获得合理的结果 …

python classification normalization svm scikit-learn

25
推荐指数
2
解决办法
2万
查看次数