def test_shap_rfe(X, y, capsys): clf = DecisionTreeClassifier(max_depth=1) with pytest.warns(None) as record: shap_elimination = ShapRFECV(clf, random_state=1, step=1, cv=2, scoring='roc_auc', n_jobs=4) shap_elimination = shap_elimination.fit(X, y) assert shap_elimination.fitted == True shap_elimination._check_if_fitted() report = shap_elimination.compute() assert report.shape[0] == 3 assert shap_elimination.get_reduced_features_set(1) == ['col_3'] ax1 = shap_elimination.plot(show=False) # Ensure that number of warnings was 0 assert len(record) == 0 # Check if there is any prints out, _ = capsys.readouterr() assert len(out) == 0
def test_shap_rfe_randomized_search(X, y, capsys): clf = DecisionTreeClassifier(max_depth=1) param_grid = {'criterion': ['gini'], 'min_samples_split': [1, 2]} search = RandomizedSearchCV(clf, param_grid, cv=2, n_iter=2) with pytest.warns(None) as record: shap_elimination = ShapRFECV(search, step=0.8, cv=2, scoring='roc_auc', n_jobs=4, verbose=150) report = shap_elimination.fit_compute(X, y) assert shap_elimination.fitted == True shap_elimination._check_if_fitted() assert report.shape[0] == 2 assert shap_elimination.get_reduced_features_set(1) == ['col_3'] ax1 = shap_elimination.plot(show=False) # Ensure that number of warnings was at least 2 for the verbose (2 generated by probatus + possibly more by SHAP) assert len(record) >= 2 # Check if there is any prints out, _ = capsys.readouterr() assert len(out) > 0
def test_shap_rfe_randomized_search_cols_to_keep(X, y, capsys): """ Test with ShapRFECV with column to keep param. """ clf = DecisionTreeClassifier(max_depth=1) param_grid = {"criterion": ["gini"], "min_samples_split": [1, 2]} search = RandomizedSearchCV(clf, param_grid, cv=2, n_iter=2) with pytest.warns(None) as record: shap_elimination = ShapRFECV(search, step=0.8, cv=2, scoring="roc_auc", n_jobs=4, random_state=1) report = shap_elimination.fit_compute( X, y, columns_to_keep=["col_2", "col_3"]) assert shap_elimination.fitted shap_elimination._check_if_fitted() assert report.shape[0] == 2 reduced_feature_set = set( shap_elimination.get_reduced_features_set(num_features=2)) assert reduced_feature_set == set(["col_2", "col_3"]) _ = shap_elimination.plot(show=False) # Ensure that number of warnings was at least 2 for the verbose (2 generated by probatus + possibly more by SHAP) assert len(record) >= 2 # Check if there is any prints out, _ = capsys.readouterr() assert len(out) == 0
def test_shap_rfe_cols_to_keep(X, y, capsys): """ Test for shap_rfe_cv with feautures to keep parameter. """ clf = DecisionTreeClassifier(max_depth=1, random_state=1) with pytest.warns(None) as record: shap_elimination = ShapRFECV(clf, random_state=1, step=2, cv=2, scoring="roc_auc", n_jobs=4, min_features_to_select=1) shap_elimination = shap_elimination.fit( X, y, columns_to_keep=["col_2", "col_3"]) assert shap_elimination.fitted shap_elimination._check_if_fitted() report = shap_elimination.compute() assert report.shape[0] == 2 reduced_feature_set = set( shap_elimination.get_reduced_features_set(num_features=2)) assert reduced_feature_set == set(["col_2", "col_3"]) # Ensure that number of warnings was 0 assert len(record) == 0 # Check if there is any prints out, _ = capsys.readouterr() assert len(out) == 0
def test_shap_rfe_svm(X, y, capsys): """ Test with ShapRFECV with SVM. """ clf = SVC(C=1, kernel="linear", probability=True) with pytest.warns(None) as record: shap_elimination = ShapRFECV(clf, random_state=1, step=1, cv=2, scoring="roc_auc", n_jobs=4) shap_elimination = shap_elimination.fit(X, y) assert shap_elimination.fitted shap_elimination._check_if_fitted() report = shap_elimination.compute() assert report.shape[0] == 3 assert shap_elimination.get_reduced_features_set(1) == ["col_3"] _ = shap_elimination.plot(show=False) # Ensure that number of warnings was 0 assert len(record) == 0 # Check if there is any prints out, _ = capsys.readouterr() assert len(out) == 0
def test_shap_rfe(X, y, sample_weight, capsys): """ Test with ShapRFECV. """ clf = DecisionTreeClassifier(max_depth=1, random_state=1) with pytest.warns(None) as record: shap_elimination = ShapRFECV( clf, random_state=1, step=1, cv=2, scoring="roc_auc", n_jobs=4, ) shap_elimination = shap_elimination.fit( X, y, sample_weight=sample_weight, approximate=True, check_additivity=False ) assert shap_elimination.fitted shap_elimination._check_if_fitted() report = shap_elimination.compute() assert report.shape[0] == 3 assert shap_elimination.get_reduced_features_set(1) == ["col_3"] _ = shap_elimination.plot(show=False) # Ensure that number of warnings was 0 assert len(record) == 0 # Check if there is any prints out, _ = capsys.readouterr() assert len(out) == 0