def test_unequal_length_X(): assert_raises(ValueError, ('y and X must contain the same number of samples. ' 'Got y: 4, X: 3'), check_Xy, X[1:], y)
def test_unequal_length_y(): assert_raises(ValueError, ('y and X must contain the same number of samples. ' 'Got y: 3, X: 4'), check_Xy, X, y[1:])
def test_check_array_1(): X = np.array([1, 2, 3]) cl = _BaseCluster(print_progress=0, random_seed=1) assert_raises(ValueError, 'X must be a 2D array. Try X[:, numpy.newaxis]', cl._check_array, X)
def test_check_labels_interger_notok(): y = np.array([1., 2.], dtype=np.float64) cl = BlankClassifier(print_progress=0, random_seed=1) assert_raises(AttributeError, 'y must be an integer array.\nFound float64', cl._check_target_array, y)
def test_check_labels_positive_notok(): y = np.array([1, 1, -1]) cl = BlankClassifier(print_progress=0, random_seed=1) assert_raises(AttributeError, 'y array must not contain negative labels.\nFound [-1 1]', cl._check_target_array, y)
def test_check_array_1(): X = np.array([1, 2, 3]) est = BlankModel() assert_raises(ValueError, 'X must be a 2D array. Try X[:, numpy.newaxis]', est._check_arrays, X)
def test_float_fail(): y = np.array([1, 2], dtype=np.int64) reg = BlankRegressor() assert_raises(AttributeError, 'y must be a float array.\nFound int64', reg._check_target_array, y)
def test_y_int_ary(): sr.fit(X[:, :2], y) assert_raises(ValueError, 'y must be an integer array. Found float64. ' 'Try passing the array as y.astype(np.integer)', plot_decision_regions, X[:, :2], y.astype(np.float), sr)
def test_filler_feature_values_fail(): sr.fit(X, y) assert_raises(ValueError, 'Filler values must be provided when ' 'X has more than 2 training features.', plot_decision_regions, X, y, sr)
def test_float_fail(): y = np.array([1, 2], dtype=np.int64) reg = _BaseRegressor(print_progress=0, random_seed=1) assert_raises(AttributeError, 'y must be a float array.\nFound int64', reg._check_target_array, y)
def test_check_array_1(): X = np.array([1, 2, 3]) est = _BaseEstimator(print_progress=0, random_seed=1) assert_raises(ValueError, 'X must be a 2D array. Try X[:, numpy.newaxis]', est._check_arrays, X)
def test_fit_1(): X = np.array([[1], [2], [3]]) est = _BaseSupervisedEstimator(print_progress=0, random_seed=1) assert_raises(TypeError, "fit() missing 1 required positional argument: 'y'", est.fit, X)
def test_fit_1(): X = np.array([[1], [2], [3]]) est = BlankClassifier(print_progress=0, random_seed=1) assert_raises(TypeError, "fit() missing 1 required positional argument: 'y'", est.fit, X)
def test_fit_1(): X = np.array([[1], [2], [3]]) est = BlankRegressor() assert_raises(TypeError, "fit() missing 1 required positional argument: 'y'", est.fit, X)
def test_feature_index_fail(): sr.fit(X, y) assert_raises(ValueError, 'Unable to unpack feature_index. Make sure feature_index ' 'only has two dimensions.', plot_decision_regions, X, y, sr, feature_index=(0, 1, 2), filler_feature_values={2: 0.5})
def test_raise_error_if_input_is_not_binary(): df2 = pd.DataFrame(one_ary, columns=cols).copy() df2.iloc[3, 3] = 2 assert_raises(ValueError, 'The allowed values for a DataFrame are True, ' 'False, 0, 1. Found value 2', apriori, df2)
def test_input_array_1d(): t = np.array([[1, 2], [3, 4]]) assert_raises(ValueError, 'One or more input arrays are not 1-dimensional.', mcnemar_table, t, t, t)
def test_check_arrays_3(): X = list([[1], [2], [3]]) tfr = _TfBaseRegressor(print_progress=0, random_seed=1) assert_raises(ValueError, 'X must be a numpy array', tfr._check_arrays, X)
def test_check_array_2(): X = list([[1], [2], [3]]) est = _BaseEstimator(print_progress=0, random_seed=1) assert_raises(ValueError, 'X must be a numpy array', est._check_arrays, X)
def test_check_array_2(): X = list([[1], [2], [3]]) cl = _BaseCluster(print_progress=0, random_seed=1) assert_raises(ValueError, 'X must be a numpy array', cl._check_array, X)
def test_predict_1(): X = np.array([[1], [2], [3]]) cl = _BaseCluster(print_progress=0, random_seed=1) assert_raises(AttributeError, 'Model is not fitted, yet.', cl.predict, X)
def test_check_labels_not_ok_1(): y = np.array([1, 3, 2]) cl = BlankClassifier(print_progress=0, random_seed=1) assert_raises(AttributeError, 'Labels not in {(1, 2), (0, 1)}.\nFound (1, 2, 3)', cl._check_target_array, y, {(0, 1), (1, 2)})
def test_predict_1(): X = np.array([[1], [2], [3]]) tfr = _TfBaseRegressor(print_progress=0, random_seed=1) assert_raises(AttributeError, 'Model is not fitted, yet.', tfr.predict, X)
def test_predict_fail(): X = np.array([[1], [2], [3]]) est = BlankRegressor() est._is_fitted = False assert_raises(AttributeError, 'Model is not fitted, yet.', est.predict, X)
def test_format_kwarg_dictionaries_user_kwargs_invalid_type(): invalid_kwargs = 'not a dictionary' message = ('d must be of type dict or None, but got ' '{} instead'.format(type(invalid_kwargs))) assert_raises(TypeError, message, format_kwarg_dictionaries, user_kwargs=invalid_kwargs)
def test_predict_fail(): X = np.array([[1], [2], [3]]) est = BlankClassifier(print_progress=0, random_seed=1) est._is_fitted = False assert_raises(AttributeError, 'Model is not fitted, yet.', est.predict, X)
def test_predict_1(): X = np.array([[1], [2], [3]]) est = _BaseEstimator(print_progress=0, random_seed=1) assert_raises(AttributeError, 'Model is not fitted, yet.', est.predict, X)
def test_invalid_labels_2(): y1 = np.where(y == 0, -1, 1) lr = LogisticRegression(epochs=15, eta=0.01, random_seed=1) assert_raises(AttributeError, 'y array must not contain negative labels.\nFound [-1 1]', lr.fit, X, y1, {(-1, 1)})
def test_invalid_method(): msg = 'method must be "approximate" or "exact", got na' assert_raises(AttributeError, msg, permutation_test, [1, 2, 3], [3, 4, 5], lambda x, y: np.mean(x) - np.mean(y), method='na')
def test_invalid_type_y(): expect = "y must be a NumPy array. Found <class 'list'>" if (sys.version_info < (3, 0)): expect = expect.replace('class', 'type') assert_raises(ValueError, expect, check_Xy, X, [1, 2, 3, 4])
def test_contourf_kwargs_type(): kwargs = 'not a dictionary' sr.fit(X[:, :2], y) message = ('d must be of type dict or None, but got ' '{} instead'.format(type(kwargs))) assert_raises(TypeError, message, plot_decision_regions, X[:, :2], y, sr, contourf_kwargs=kwargs)
def test_invalid_labels_1(): y2 = np.where(y1 == 0, 2, 1) ada = Adaline(epochs=15, eta=0.01, random_seed=1) if sys.version_info >= (3, 0): objtype = '{(0, 1)}' else: objtype = 'set([(0, 1)])' expect = 'Labels not in %s.\nFound (1, 2)' % objtype assert_raises(AttributeError, expect, ada.fit, X, y2, {(0, 1)})
def test_wrong_feature_number(): knn = KNeighborsClassifier() expect = ('Number of features to be selected must be' ' larger than the number of features' ' specified via `fixed_features`. Got' ' `k_features=1` and `fixed_features=2`') assert_raises(ValueError, expect, SFS, knn, k_features=1, fixed_features=(1, 3))
def test_invalid_labels_1(): y1 = np.where(y == 0, 2, 1) lr = LogisticRegression(epochs=15, eta=0.01, random_seed=1) if sys.version_info >= (3, 0): objtype = '{(0, 1)}' else: objtype = 'set([(0, 1)])' expect = 'Labels not in %s.\nFound (1, 2)' % objtype assert_raises(AttributeError, expect, lr.fit, X, y1, {(0, 1)})
def test_invalid_address_bits(): msg_1 = "address_bits must be an integer. Got <class 'float'>." # for Python 2.7: if sys.version_info[0] == 2: msg_1 = msg_1.replace('<class', '<type') assert_raises(AttributeError, msg_1, make_multiplexer_dataset, 1.2) msg_2 = "Number of address_bits must be greater than 0. Got -1." assert_raises(AttributeError, msg_2, make_multiplexer_dataset, -1)
def test_labels(): X = np.array([[1, 2], [3, 4]]) y = np.array([-1, 1]) lr = SoftmaxRegression(epochs=200, eta=0.005, minibatches=1, random_seed=1) assert_raises(AttributeError, 'y array must not contain negative labels.\nFound [-1 1]', lr.fit, X, y)
def test_sparsedataframe_notzero_column(self): dfs = pd.SparseDataFrame(self.df) dfs.columns = [i for i in range(len(dfs.columns))] self.fpalgo(dfs) dfs = pd.SparseDataFrame(self.df) dfs.columns = [i + 1 for i in range(len(dfs.columns))] assert_raises( ValueError, 'Due to current limitations in Pandas, ' 'if the SparseDataFrame has integer column names,' 'names, please make sure they either start ' 'with `0` or cast them as string column names: ' '`df.columns = [str(i) for i in df.columns`].', self.fpalgo, dfs)
def test__clf_with_no_proba_fail(): X, y = iris_data() clf = OneRClassifier() clf.fit(X, y) x_ref = X[15] s = ("Your `model` does not support " "`predict_proba`. Set `y_desired_proba` " " to `None` to use `predict`instead.") assert_raises(AttributeError, s, create_counterfactual, x_ref, 2, clf, X, 1., 100, 123)
def test_not_fitted_predict(): lr = LinearRegression() svr_rbf = SVR(kernel='rbf') ridge = Ridge(random_state=1) stregr = StackingRegressor(regressors=[lr, ridge], meta_regressor=svr_rbf, store_train_meta_features=True) X_train, X_test, y_train, y_test = train_test_split(X2, y, test_size=0.3) expect = ("Estimator not fitted, " "call `fit` before exploiting the model.") assert_raises(NotFittedError, expect, stregr.predict, X_train)
def test_kfeatures_type_4(): iris = load_iris() X = iris.data y = iris.target knn = KNeighborsClassifier() expect = ('k_features tuple max value must be in range(1, X.shape[1]+1).') sfs = SFS(estimator=knn, k_features=(1, 5)) assert_raises(AttributeError, expect, sfs.fit, X, y)
def test_wrong_feature_range(): knn = KNeighborsClassifier() expect = ('The minimum number of features to be ' 'selected must be larger than the number of ' 'features specified via ' '`fixed_features`. ' 'Got `k_features=(1, 3)` and `len(fixed_features)=2`') assert_raises(ValueError, expect, SFS, knn, k_features=(1, 3), fixed_features=(1, 3))
def test_kfeatures_type_2(): iris = load_iris() X = iris.data y = iris.target knn = KNeighborsClassifier() expect = 'k_features must be a positive integer or tuple' sfs = SFS(estimator=knn, k_features='abc') assert_raises(AttributeError, expect, sfs.fit, X, y)
def test_invalid_dtype_y(): if (sys.version_info > (3, 0)): expect = ('y must be an integer array. Found <U1. ' 'Try passing the array as y.astype(np.integer)') else: expect = ('y must be an integer array. Found |S1. ' 'Try passing the array as y.astype(np.integer)') assert_raises(ValueError, expect, check_Xy, X, np.array(['a', 'b', 'c', 'd']))
def test_kfeatures_type_1(): iris = load_iris() X = iris.data y = iris.target knn = KNeighborsClassifier() expect = ('k_features must be a positive integer between 1 and X.shape[1],' ' got 0') sfs = SFS(estimator=knn, k_features=0) assert_raises(AttributeError, expect, sfs.fit, X, y)
def test_model_have_same_len(): y_true = np.array([1, 1, 0]) y_1 = np.array([0, 1, 0]) y_2 = np.array([1, 1, 0]) y_3 = np.array([1, 1]) assert_raises(ValueError, ('Each prediction array must have' ' the same number of samples.'), cochrans_q, y_true, y_1, y_2, y_3)
def test_kfeatures_type_5(): iris = load_iris() X = iris.data y = iris.target knn = KNeighborsClassifier() expect = ('he min k_features value must be' ' larger than the max k_features value.') sfs = SFS(estimator=knn, k_features=(3, 1)) assert_raises(AttributeError, expect, sfs.fit, X, y)
def test_cloning(): oht = OnehotTransactions() oht.fit(dataset) oht2 = clone(oht) msg = ("'OnehotTransactions' object has no attribute 'columns_'") assert_raises(AttributeError, msg, oht2.transform, dataset) trans = oht2.fit_transform(dataset) np.testing.assert_array_equal(expect, trans)
def test_minmaxfeatures_1(): iris = load_iris() X = iris.data y = iris.target knn = KNeighborsClassifier() efs = EFS(estimator=knn, min_features=3, max_features=2) expect = ('min_features must be <= max_features') assert_raises(AttributeError, expect, efs.fit, X, y)
def test_maxfeatures_1(): iris = load_iris() X = iris.data y = iris.target knn = KNeighborsClassifier() efs = EFS(estimator=knn, min_features=1, max_features=0) expect = ('max_features must be smaller than 5 and larger than 0') assert_raises(AttributeError, expect, efs.fit, X, y)
def test_get_metric_dict_not_fitted(): knn = KNeighborsClassifier(n_neighbors=4) sfs1 = SFS(knn, k_features=2, forward=True, floating=False, cv=0, clone_estimator=False, verbose=0, n_jobs=1) expect = 'SequentialFeatureSelector has not been fitted, yet.' assert_raises(AttributeError, expect, sfs1.get_metric_dict)
def test_clone_params_fail(): iris = load_iris() X = iris.data y = iris.target assert_raises(TypeError, ("Cannot clone object" " '<class 'mlxtend.classifier." "softmax_regression.SoftmaxRegression'>'" " (type <class 'type'>): it does not seem to be a" " scikit-learn estimator as it does not" " implement a 'get_params' methods."), SFS, SoftmaxRegression, k_features=3, clone_estimator=True)
def test_get_metric_dict_not_fitted(): knn = KNeighborsClassifier(n_neighbors=4) efs1 = EFS(knn, min_features=2, max_features=2, scoring='accuracy', cv=0, clone_estimator=False, print_progress=False, n_jobs=1) expect = 'ExhaustiveFeatureSelector has not been fitted, yet.' assert_raises(AttributeError, expect, efs1.get_metric_dict)
def test_not_fitted_predict(): lr = LinearRegression() svr_rbf = SVR(kernel='rbf') ridge = Ridge(random_state=1) stregr = StackingRegressor(regressors=[lr, ridge], meta_regressor=svr_rbf, store_train_meta_features=True) X_train, X_test, y_train, y_test = train_test_split(X2, y, test_size=0.3) expect = ("This StackingRegressor instance is not fitted yet. Call " "'fit' with appropriate arguments before using this method.") assert_raises(NotFittedError, expect, stregr.predict, X_train) assert_raises(NotFittedError, expect, stregr.predict_meta_features, X_train)
def test_use_clones(): np.random.seed(123) clf1 = LogisticRegression(solver='liblinear', multi_class='ovr') clf2 = RandomForestClassifier(n_estimators=10) clf3 = GaussianNB() EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], use_clones=True).fit(X, y) assert_raises( exceptions.NotFittedError, "This RandomForestClassifier instance is not fitted yet." " Call 'fit' with appropriate arguments" " before using this estimator.", clf2.predict, X) EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], use_clones=False).fit(X, y) clf2.predict(X)
def test_transform_not_fitted(): iris = load_iris() X = iris.data knn = KNeighborsClassifier(n_neighbors=4) efs1 = EFS(knn, min_features=2, max_features=2, scoring='accuracy', cv=0, clone_estimator=False, print_progress=False, n_jobs=1) expect = 'ExhaustiveFeatureSelector has not been fitted, yet.' assert_raises(AttributeError, expect, efs1.transform, X)
def test_transform_not_fitted(): iris = load_iris() X = iris.data knn = KNeighborsClassifier(n_neighbors=4) sfs1 = SFS(knn, k_features=2, forward=True, floating=False, cv=0, clone_estimator=False, verbose=0, n_jobs=1) expect = 'SequentialFeatureSelector has not been fitted, yet.' assert_raises(AttributeError, expect, sfs1.transform, X)
def test_not_fitted(): np.random.seed(123) meta = LogisticRegression() clf1 = RandomForestClassifier() clf2 = GaussianNB() sclf = StackingClassifier(classifiers=[clf1, clf2], use_probas=True, meta_classifier=meta) assert_raises( NotFittedError, "This StackingClassifier instance is not fitted yet." " Call 'fit' with appropriate arguments" " before using this method.", sclf.predict, iris.data) assert_raises( NotFittedError, "This StackingClassifier instance is not fitted yet." " Call 'fit' with appropriate arguments" " before using this method.", sclf.predict_proba, iris.data)
def test_train_size(): X, y = iris_data() clf1 = LogisticRegression() clf2 = DecisionTreeClassifier() expected_err_msg = ("train_size must be of type int or float. " "Got <class 'NoneType'>.") if sys.version_info < (3, 0): expected_err_msg = expected_err_msg.replace('<class', '<type') assert_raises(ValueError, expected_err_msg, paired_ttest_resampled, clf1, clf2, X, y, test_size=None)
def test_get_metric_dict_not_fitted(): iris = load_iris() X = iris.data y = iris.target knn = KNeighborsClassifier(n_neighbors=4) sfs1 = SFS(knn, k_features=2, forward=True, floating=False, scoring='accuracy', cv=0, skip_if_stuck=True, clone_estimator=False, verbose=0, n_jobs=1) expect = 'SequentialFeatureSelector has not been fitted, yet.' assert_raises(AttributeError, expect, sfs1.get_metric_dict)
def test_clone_params_fail(): if sys.version_info >= (3, 0): objtype = 'class' else: objtype = 'type' expect = ("Cannot clone object" " '<class 'mlxtend.classifier." "softmax_regression.SoftmaxRegression'>'" " (type <%s 'type'>): it does not seem to be a" " scikit-learn estimator as it does not" " implement a 'get_params' methods.") % objtype assert_raises(TypeError, expect, SFS, SoftmaxRegression, scoring='accuracy', k_features=3, clone_estimator=True)