def test_transformers(): # test if transformers do something sensible on training set # also test all shapes / shape errors estimators = all_estimators() transformers = [(name, E) for name, E in estimators if issubclass(E, TransformerMixin)] iris = load_iris() X, y = iris.data, iris.target X, y = shuffle(X, y, random_state=0) X, y = X[:10], y[:10] n_samples, n_features = X.shape X = Scaler().fit_transform(X) X -= X.min() for name, Trans in transformers: if Trans in dont_test or Trans in meta_estimators: continue # these don't actually fit the data: if Trans in [AdditiveChi2Sampler, Binarizer, Normalizer]: continue # catch deprecation warnings with warnings.catch_warnings(record=True): trans = Trans() if hasattr(trans, 'compute_importances'): trans.compute_importances = True if Trans is SelectKBest: # SelectKBest has a default of k=10 # which is more feature than we have. trans.k = 1 # fit trans.fit(X, y) X_pred = trans.fit_transform(X, y=y) assert_equal(X_pred.shape[0], n_samples) if hasattr(trans, 'transform'): X_pred2 = trans.transform(X) assert_array_almost_equal(X_pred, X_pred2, 2) # raises error on malformed input for transform assert_raises(ValueError, trans.transform, X.T)
def test_transformers(): # test if transformers do something sensible on training set # also test all shapes / shape errors estimators = all_estimators() transformers = [(name, E) for name, E in estimators if issubclass(E, TransformerMixin)] X, y = make_blobs(n_samples=30, centers=[[0, 0, 0], [1, 1, 1]], random_state=0, n_features=2, cluster_std=0.1) n_samples, n_features = X.shape X = Scaler().fit_transform(X) X -= X.min() succeeded = True for name, Trans in transformers: if Trans in dont_test or Trans in meta_estimators: continue # these don't actually fit the data: if Trans in [AdditiveChi2Sampler, Binarizer, Normalizer]: continue # catch deprecation warnings with warnings.catch_warnings(record=True): trans = Trans() if hasattr(trans, 'compute_importances'): trans.compute_importances = True if Trans is SelectKBest: # SelectKBest has a default of k=10 # which is more feature than we have. trans.k = 1 # fit if Trans in (_PLS, PLSCanonical, PLSRegression, CCA, PLSSVD): y_ = np.vstack([y, 2 * y + np.random.randint(2, size=len(y))]) y_ = y_.T else: y_ = y try: trans.fit(X, y_) X_pred = trans.fit_transform(X, y=y_) if isinstance(X_pred, tuple): for x_pred in X_pred: assert_equal(x_pred.shape[0], n_samples) else: assert_equal(X_pred.shape[0], n_samples) except Exception as e: print trans print e print succeeded = False if hasattr(trans, 'transform'): if Trans in (_PLS, PLSCanonical, PLSRegression, CCA, PLSSVD): X_pred2 = trans.transform(X, y_) else: X_pred2 = trans.transform(X) if isinstance(X_pred, tuple) and isinstance(X_pred2, tuple): for x_pred, x_pred2 in zip(X_pred, X_pred2): assert_array_almost_equal(x_pred, x_pred2, 2, "fit_transform not correct in %s" % Trans) else: assert_array_almost_equal(X_pred, X_pred2, 2, "fit_transform not correct in %s" % Trans) # raises error on malformed input for transform assert_raises(ValueError, trans.transform, X.T) assert_true(succeeded)
def test_transformers(): # test if transformers do something sensible on training set # also test all shapes / shape errors estimators = all_estimators() transformers = [(name, E) for name, E in estimators if issubclass(E, TransformerMixin)] X, y = make_blobs(n_samples=30, centers=[[0, 0, 0], [1, 1, 1]], random_state=0, n_features=2, cluster_std=0.1) n_samples, n_features = X.shape X = Scaler().fit_transform(X) X -= X.min() succeeded = True for name, Trans in transformers: if Trans in dont_test or Trans in meta_estimators: continue # these don't actually fit the data: if Trans in [AdditiveChi2Sampler, Binarizer, Normalizer]: continue # catch deprecation warnings with warnings.catch_warnings(record=True): trans = Trans() if hasattr(trans, 'compute_importances'): trans.compute_importances = True if Trans is SelectKBest: # SelectKBest has a default of k=10 # which is more feature than we have. trans.k = 1 # fit if Trans in (_PLS, PLSCanonical, PLSRegression, CCA, PLSSVD): y_ = np.vstack([y, 2 * y + np.random.randint(2, size=len(y))]) y_ = y_.T else: y_ = y try: trans.fit(X, y_) X_pred = trans.fit_transform(X, y=y_) if isinstance(X_pred, tuple): for x_pred in X_pred: assert_equal(x_pred.shape[0], n_samples) else: assert_equal(X_pred.shape[0], n_samples) except Exception as e: print trans print e print succeeded = False if hasattr(trans, 'transform'): if Trans in (_PLS, PLSCanonical, PLSRegression, CCA, PLSSVD): X_pred2 = trans.transform(X, y_) else: X_pred2 = trans.transform(X) if isinstance(X_pred, tuple) and isinstance(X_pred2, tuple): for x_pred, x_pred2 in zip(X_pred, X_pred2): assert_array_almost_equal( x_pred, x_pred2, 2, "fit_transform not correct in %s" % Trans) else: assert_array_almost_equal( X_pred, X_pred2, 2, "fit_transform not correct in %s" % Trans) # raises error on malformed input for transform assert_raises(ValueError, trans.transform, X.T) assert_true(succeeded)