def test_rfe(): generator = check_random_state(0) iris = load_iris() X = np.c_[iris.data, generator.normal(size=(len(iris.data), 6))] X_sparse = sparse.csr_matrix(X) y = iris.target # dense model clf = SVC(kernel="linear") rfe = RFE(estimator=clf, n_features_to_select=4, step=0.1) rfe.fit(X, y) X_r = rfe.transform(X) clf.fit(X_r, y) assert len(rfe.ranking_) == X.shape[1] # sparse model clf_sparse = SVC(kernel="linear") rfe_sparse = RFE(estimator=clf_sparse, n_features_to_select=4, step=0.1) rfe_sparse.fit(X_sparse, y) X_r_sparse = rfe_sparse.transform(X_sparse) assert X_r.shape == iris.data.shape assert_array_almost_equal(X_r[:10], iris.data[:10]) assert_array_almost_equal(rfe.predict(X), clf.predict(iris.data)) assert rfe.score(X, y) == clf.score(iris.data, iris.target) assert_array_almost_equal(X_r, X_r_sparse.toarray())
for break_ties, title, ax in zip((False, True), titles, sub.flatten()): svm = SVC(kernel="linear", C=1, break_ties=break_ties, decision_function_shape='ovr').fit(X, y) xlim = [X[:, 0].min(), X[:, 0].max()] ylim = [X[:, 1].min(), X[:, 1].max()] xs = np.linspace(xlim[0], xlim[1], 1000) ys = np.linspace(ylim[0], ylim[1], 1000) xx, yy = np.meshgrid(xs, ys) pred = svm.predict(np.c_[xx.ravel(), yy.ravel()]) colors = [plt.cm.Accent(i) for i in [0, 4, 7]] points = ax.scatter(X[:, 0], X[:, 1], c=y, cmap="Accent") classes = [(0, 1), (0, 2), (1, 2)] line = np.linspace(X[:, 1].min() - 5, X[:, 1].max() + 5) ax.imshow(-pred.reshape(xx.shape), cmap="Accent", alpha=.2, extent=(xlim[0], xlim[1], ylim[1], ylim[0])) for coef, intercept, col in zip(svm.coef_, svm.intercept_, classes): line2 = -(line * coef[1] + intercept) / coef[0] ax.plot(line2, line, "-", c=colors[col[0]]) ax.plot(line2, line, "--", c=colors[col[1]])