def test_one_estimator_print_change_only(print_changed_only): pca = PCA(n_components=10) with config_context(print_changed_only=print_changed_only): pca_repr = str(pca) html_output = estimator_html_repr(pca) assert pca_repr in html_output
def test_fallback_exists(): """Check that repr fallback is in the HTML.""" pca = PCA(n_components=10) html_output = estimator_html_repr(pca) assert (f'<div class="sk-text-repr-fallback"><pre>{html.escape(str(pca))}' in html_output)
def test_show_arrow_pipeline(): """Show arrow in pipeline for top level in pipeline""" pipe = Pipeline([("scale", StandardScaler()), ("log_Reg", LogisticRegression())]) html_output = estimator_html_repr(pipe) assert ('class="sk-toggleable__label sk-toggleable__label-arrow">Pipeline' in html_output)
def test_duck_typing_nested_estimator(): # Test duck typing metaestimators with GP kernel = RationalQuadratic(length_scale=1.0, alpha=0.1) gp = GaussianProcessRegressor(kernel=kernel) html_output = estimator_html_repr(gp) assert f"<pre>{str(kernel)}" in html_output assert f"<pre>{str(gp)}" in html_output
def test_stacking_regressor(final_estimator): reg = StackingRegressor(estimators=[("svr", LinearSVR())], final_estimator=final_estimator) html_output = estimator_html_repr(reg) assert str(reg.estimators[0][0]) in html_output assert "LinearSVR</label>" in html_output if final_estimator is None: assert "RidgeCV</label>" in html_output else: assert final_estimator.__class__.__name__ in html_output
def test_duck_typing_nested_estimator(): # Test duck typing metaestimators with random search kernel_ridge = KernelRidge(kernel=ExpSineSquared()) param_distributions = {"alpha": [1, 2]} kernel_ridge_tuned = RandomizedSearchCV( kernel_ridge, param_distributions=param_distributions, ) html_output = estimator_html_repr(kernel_ridge_tuned) assert "estimator: KernelRidge</label>" in html_output
def test_ovo_classifier_duck_typing_meta(): # Test duck typing metaestimators with OVO ovo = OneVsOneClassifier(LinearSVC(penalty="l1")) html_output = estimator_html_repr(ovo) # inner estimators do not show changes with config_context(print_changed_only=True): assert f"<pre>{str(ovo.estimator)}" in html_output assert "LinearSVC</label>" in html_output # outer estimator assert f"<pre>{str(ovo)}" in html_output
def test_birch_duck_typing_meta(): # Test duck typing meta estimators with Birch birch = Birch(n_clusters=AgglomerativeClustering(n_clusters=3)) html_output = estimator_html_repr(birch) # inner estimators do not show changes with config_context(print_changed_only=True): assert f"<pre>{str(birch.n_clusters)}" in html_output assert "AgglomerativeClustering</label>" in html_output # outer estimator contains all changes assert f"<pre>{str(birch)}" in html_output
def test_stacking_classsifer(final_estimator): estimators = [('mlp', MLPClassifier(alpha=0.001)), ('tree', DecisionTreeClassifier())] clf = StackingClassifier( estimators=estimators, final_estimator=final_estimator) html_output = estimator_html_repr(clf) assert str(clf) in html_output # If final_estimator's default changes from LogisticRegression # this should be updated if final_estimator is None: assert "LogisticRegression(" in html_output else: assert final_estimator.__class__.__name__ in html_output
def test_estimator_html_repr_pipeline(): num_trans = Pipeline( steps=[("pass", "passthrough"), ("imputer", SimpleImputer(strategy="median"))]) cat_trans = Pipeline(steps=[ ("imputer", SimpleImputer(strategy="constant", missing_values="empty")), ("one-hot", OneHotEncoder(drop="first")), ]) preprocess = ColumnTransformer([ ("num", num_trans, ["a", "b", "c", "d", "e"]), ("cat", cat_trans, [0, 1, 2, 3]), ]) feat_u = FeatureUnion([ ("pca", PCA(n_components=1)), ( "tsvd", Pipeline([ ("first", TruncatedSVD(n_components=3)), ("select", SelectPercentile()), ]), ), ]) clf = VotingClassifier([ ("lr", LogisticRegression(solver="lbfgs", random_state=1)), ("mlp", MLPClassifier(alpha=0.001)), ]) pipe = Pipeline([("preprocessor", preprocess), ("feat_u", feat_u), ("classifier", clf)]) html_output = estimator_html_repr(pipe) # top level estimators show estimator with changes assert str(pipe) in html_output for _, est in pipe.steps: assert f'<div class="sk-toggleable__content"><pre>{str(est)}' in html_output # low level estimators do not show changes with config_context(print_changed_only=True): assert str(num_trans["pass"]) in html_output assert "passthrough</label>" in html_output assert str(num_trans["imputer"]) in html_output for _, _, cols in preprocess.transformers: assert f"<pre>{cols}</pre>" in html_output # feature union for name, _ in feat_u.transformer_list: assert f"<label>{name}</label>" in html_output pca = feat_u.transformer_list[0][1] assert f"<pre>{str(pca)}</pre>" in html_output tsvd = feat_u.transformer_list[1][1] first = tsvd["first"] select = tsvd["select"] assert f"<pre>{str(first)}</pre>" in html_output assert f"<pre>{str(select)}</pre>" in html_output # voting classifier for name, est in clf.estimators: assert f"<label>{name}</label>" in html_output assert f"<pre>{str(est)}</pre>" in html_output
def _repr_mimebundle_(self, **kwargs): """Mime bundle used by jupyter kernels to display estimator""" output = {"text/plain": repr(self)} if get_config()["display"] == 'diagram': output["text/html"] = estimator_html_repr(self) return output
def _repr_html_inner(self): """This function is returned by the @property `_repr_html_` to make `hasattr(estimator, "_repr_html_") return `True` or `False` depending on `get_config()["display"]`. """ return estimator_html_repr(self)
def test_estimator_html_repr_pipeline(): num_trans = Pipeline(steps=[ ('pass', 'passthrough'), ('imputer', SimpleImputer(strategy='median')) ]) cat_trans = Pipeline(steps=[ ('imputer', SimpleImputer(strategy='constant', missing_values='empty')), ('one-hot', OneHotEncoder(drop='first')) ]) preprocess = ColumnTransformer([ ('num', num_trans, ['a', 'b', 'c', 'd', 'e']), ('cat', cat_trans, [0, 1, 2, 3]) ]) feat_u = FeatureUnion([ ('pca', PCA(n_components=1)), ('tsvd', Pipeline([('first', TruncatedSVD(n_components=3)), ('select', SelectPercentile())])) ]) clf = VotingClassifier([ ('lr', LogisticRegression(solver='lbfgs', random_state=1)), ('mlp', MLPClassifier(alpha=0.001)) ]) pipe = Pipeline([ ('preprocessor', preprocess), ('feat_u', feat_u), ('classifier', clf) ]) html_output = estimator_html_repr(pipe) # top level estimators show estimator with changes assert str(pipe) in html_output for _, est in pipe.steps: assert (f"<div class=\"sk-toggleable__content\">" f"<pre>{str(est)}") in html_output # low level estimators do not show changes with config_context(print_changed_only=True): assert str(num_trans['pass']) in html_output assert 'passthrough</label>' in html_output assert str(num_trans['imputer']) in html_output for _, _, cols in preprocess.transformers: assert f"<pre>{cols}</pre>" in html_output # feature union for name, _ in feat_u.transformer_list: assert f"<label>{name}</label>" in html_output pca = feat_u.transformer_list[0][1] assert f"<pre>{str(pca)}</pre>" in html_output tsvd = feat_u.transformer_list[1][1] first = tsvd['first'] select = tsvd['select'] assert f"<pre>{str(first)}</pre>" in html_output assert f"<pre>{str(select)}</pre>" in html_output # voting classifer for name, est in clf.estimators: assert f"<label>{name}</label>" in html_output assert f"<pre>{str(est)}</pre>" in html_output