|
36 | 36 | from sklearn.feature_extraction.text import HashingVectorizer
|
37 | 37 | from sklearn.feature_selection import SelectKBest, chi2
|
38 | 38 | from sklearn.linear_model import RidgeClassifier
|
| 39 | +from sklearn.pipeline import Pipeline |
39 | 40 | from sklearn.svm import LinearSVC
|
40 | 41 | from sklearn.linear_model import SGDClassifier
|
41 | 42 | from sklearn.linear_model import Perceptron
|
@@ -276,25 +277,14 @@ def benchmark(clf):
|
276 | 277 | results.append(benchmark(MultinomialNB(alpha=.01)))
|
277 | 278 | results.append(benchmark(BernoulliNB(alpha=.01)))
|
278 | 279 |
|
279 |
| - |
280 |
| -class L1LinearSVC(LinearSVC): |
281 |
| - |
282 |
| - def fit(self, X, y): |
283 |
| - # The smaller C, the stronger the regularization. |
284 |
| - # The more regularization, the more sparsity. |
285 |
| - self.transformer_ = LinearSVC(penalty="l1", |
286 |
| - dual=False, tol=1e-3) |
287 |
| - X = self.transformer_.fit_transform(X, y) |
288 |
| - return LinearSVC.fit(self, X, y) |
289 |
| - |
290 |
| - def predict(self, X): |
291 |
| - X = self.transformer_.transform(X) |
292 |
| - return LinearSVC.predict(self, X) |
293 |
| - |
294 | 280 | print('=' * 80)
|
295 | 281 | print("LinearSVC with L1-based feature selection")
|
296 |
| -results.append(benchmark(L1LinearSVC())) |
297 |
| - |
| 282 | +# The smaller C, the stronger the regularization. |
| 283 | +# The more regularization, the more sparsity. |
| 284 | +results.append(benchmark(Pipeline([ |
| 285 | + ('feature_selection', LinearSVC(penalty="l1", dual=False, tol=1e-3)), |
| 286 | + ('classification', LinearSVC()) |
| 287 | +]))) |
298 | 288 |
|
299 | 289 | # make some plots
|
300 | 290 |
|
|
0 commit comments