File: test_selector_mixin.py

package info (click to toggle)
scikit-learn 0.11.0-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,900 kB
  • sloc: python: 34,740; ansic: 8,860; cpp: 8,849; pascal: 230; makefile: 211; sh: 14
file content (32 lines) | stat: -rw-r--r-- 1,076 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
import scipy.sparse as sp

from nose.tools import assert_true

from sklearn.utils.testing import assert_less

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
from sklearn.svm import LinearSVC

iris = load_iris()


def test_transform_linear_model():
    for clf in (LogisticRegression(C=0.1),
                LinearSVC(C=0.01, dual=False),
                SGDClassifier(alpha=0.1, n_iter=10, shuffle=True, seed=0)):
        for func in (np.array, sp.csr_matrix):
            X = func(iris.data)
            clf.set_params(penalty="l1")
            clf.fit(X, iris.target)
            X_new = clf.transform(X)
            if isinstance(clf, SGDClassifier):
                assert_true(X_new.shape[1] <= X.shape[1])
            else:
                assert_less(X_new.shape[1], X.shape[1])
            clf.set_params(penalty="l2")
            clf.fit(X_new, iris.target)
            pred = clf.predict(X_new)
            assert_true(np.mean(pred == iris.target) >= 0.7)