def test_feature_importances_2d_coef(): X, y = datasets.make_classification(n_samples=1000, n_features=10, n_informative=3, n_redundant=0, n_repeated=0, shuffle=False, random_state=0, n_classes=4) est = LogisticRegression() for threshold, func in zip(["mean", "median"], [np.mean, np.median]): for order in [1, 2, np.inf]: # Fit SelectFromModel a multi-class problem transformer = SelectFromModel(estimator=LogisticRegression(), threshold=threshold, norm_order=order) transformer.fit(X, y) assert_true(hasattr(transformer.estimator_, 'coef_')) X_new = transformer.transform(X) assert_less(X_new.shape[1], X.shape[1]) # Manually check that the norm is correctly performed est.fit(X, y) importances = norm(est.coef_, axis=0, ord=order) feature_mask = importances > func(importances) assert_array_equal(X_new, X[:, feature_mask])
def test_feature_importances_2d_coef(): X, y = datasets.make_classification( n_samples=1000, n_features=10, n_informative=3, n_redundant=0, n_repeated=0, shuffle=False, random_state=0, n_classes=4, ) est = LogisticRegression() for threshold, func in zip(["mean", "median"], [np.mean, np.median]): for order in [1, 2, np.inf]: # Fit SelectFromModel a multi-class problem transformer = SelectFromModel(estimator=LogisticRegression(), threshold=threshold, norm_order=order) transformer.fit(X, y) assert_true(hasattr(transformer.estimator_, "coef_")) X_new = transformer.transform(X) assert_less(X_new.shape[1], X.shape[1]) # Manually check that the norm is correctly performed est.fit(X, y) importances = norm(est.coef_, axis=0, ord=order) feature_mask = importances > func(importances) assert_array_equal(X_new, X[:, feature_mask])
def test_norm(): X = np.array([[-2, 4, 5], [1, 3, -4], [0, 0, 8], [0, 0, 0]]).astype(float) # Test various axis and order assert_equal(math.sqrt(135), norm(X)) assert_array_equal(np.array([math.sqrt(5), math.sqrt(25), math.sqrt(105)]), norm(X, axis=0)) assert_array_equal(np.array([3, 7, 17]), norm(X, axis=0, ord=1)) assert_array_equal(np.array([2, 4, 8]), norm(X, axis=0, ord=np.inf)) assert_array_equal(np.array([0, 0, 0]), norm(X, axis=0, ord=-np.inf)) assert_array_equal(np.array([11, 8, 8, 0]), norm(X, axis=1, ord=1)) # Test shapes assert_equal((), norm(X).shape) assert_equal((3, ), norm(X, axis=0).shape) assert_equal((4, ), norm(X, axis=1).shape)
def test_norm(): X = np.array([[-2, 4, 5], [1, 3, -4], [0, 0, 8], [0, 0, 0]]).astype(float) # Test various axis and order assert_equal(math.sqrt(135), norm(X)) assert_array_equal( np.array([math.sqrt(5), math.sqrt(25), math.sqrt(105)]), norm(X, axis=0) ) assert_array_equal(np.array([3, 7, 17]), norm(X, axis=0, ord=1)) assert_array_equal(np.array([2, 4, 8]), norm(X, axis=0, ord=np.inf)) assert_array_equal(np.array([0, 0, 0]), norm(X, axis=0, ord=-np.inf)) assert_array_equal(np.array([11, 8, 8, 0]), norm(X, axis=1, ord=1)) # Test shapes assert_equal((), norm(X).shape) assert_equal((3,), norm(X, axis=0).shape) assert_equal((4,), norm(X, axis=1).shape)