CoolFace
Apppublic

sklearn-docs/classification

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
15likes
app.py172 linesDownload Raw Back to root
1import numpy as np2import matplotlib.pyplot as plt3from matplotlib.colors import ListedColormap4from sklearn.model_selection import train_test_split5from sklearn.preprocessing import StandardScaler6from sklearn.datasets import make_moons, make_circles, make_classification7from sklearn.neural_network import MLPClassifier8from sklearn.neighbors import KNeighborsClassifier9from sklearn.svm import SVC10from sklearn.gaussian_process import GaussianProcessClassifier11from sklearn.gaussian_process.kernels import RBF12from sklearn.tree import DecisionTreeClassifier13from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier14from sklearn.naive_bayes import GaussianNB15from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis16from sklearn.inspection import DecisionBoundaryDisplay17from sklearn.datasets import make_blobs, make_circles, make_moons18import gradio as gr19import math20from functools import partial21 22 23 24### DATASETS25 26def normalize(X):27    return StandardScaler().fit_transform(X)28 29 30def linearly_separable():31    X, y = make_classification(32        n_features=2, n_redundant=0, n_informative=2, random_state=1, n_clusters_per_class=133    )34    rng = np.random.RandomState(2)35    X += 2 * rng.uniform(size=X.shape)36    linearly_separable = (X, y)37    return linearly_separable38 39DATA_MAPPING = {40    "Moons": make_moons(noise=0.3, random_state=0),41    "Circles":make_circles(noise=0.2, factor=0.5, random_state=1),42    "Linearly Separable Random Dataset": linearly_separable(),43}44 45 46#### MODELS47 48def get_groundtruth_model(X, labels):49    # dummy model to show true label distribution50    class Dummy:51        def __init__(self, y):52            self.labels_ = labels53 54    return Dummy(labels)55    56DATASETS = [57    make_moons(noise=0.3, random_state=0),58    make_circles(noise=0.2, factor=0.5, random_state=1),59    linearly_separable()60]61NAME_CLF_MAPPING = {62    "Ground Truth":get_groundtruth_model,63    "Nearest Neighbors":KNeighborsClassifier(3),64    "Linear SVM":SVC(kernel="linear", C=0.025),65    "RBF SVM":SVC(gamma=2, C=1),66    "Gaussian Process":GaussianProcessClassifier(1.0 * RBF(1.0)),67    "Decision Tree":DecisionTreeClassifier(max_depth=5),68    "Random Forest":RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),69    "Neural Net":MLPClassifier(alpha=1, max_iter=1000),70    "AdaBoost":AdaBoostClassifier(),71    "Naive Bayes":GaussianNB(),72}73 74 75 76#### PLOT77FIGSIZE = 7,778figure = plt.figure(figsize=(25, 10))79i = 180 81 82 83 84def train_models(selected_data, clf_name):85    cm = plt.cm.RdBu86    cm_bright = ListedColormap(["#FF0000", "#0000FF"])87    clf = NAME_CLF_MAPPING[clf_name]88    89    X, y = DATA_MAPPING[selected_data]90    X = StandardScaler().fit_transform(X)91    X_train, X_test, y_train, y_test = train_test_split(92        X, y, test_size=0.4, random_state=4293    )94    95    x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.596    y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.597    if clf_name != "Ground Truth":98        clf.fit(X_train, y_train)99        score = clf.score(X_test, y_test)100        fig, ax = plt.subplots(figsize=FIGSIZE)101        ax.set_title(clf_name, fontsize = 10)102        103        DecisionBoundaryDisplay.from_estimator(104                clf, X, cmap=cm, alpha=0.8, ax=ax, eps=0.5105            ).plot()106        return fig107    else:108        #########109        110        for ds_cnt, ds in enumerate(DATASETS):111            X, y = ds112 113            x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5114            y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5115 116            # just plot the dataset first117            cm = plt.cm.RdBu118            cm_bright = ListedColormap(["#FF0000", "#0000FF"])119            fig, ax = plt.subplots(figsize=FIGSIZE)120            ax.set_title("Input data")121            # Plot the training points122 123            ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright, edgecolors="k")124            # Plot the testing points125            ax.scatter(126                X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6, edgecolors="k"127            )128            ax.set_xlim(x_min, x_max)129            ax.set_ylim(y_min, y_max)130            ax.set_xticks(())131            ax.set_yticks(())132 133            return fig134 135 136 137        ###########138description = "Learn how different statistical classifiers perform in different datasets."139 140def iter_grid(n_rows, n_cols):141    # create a grid using gradio Block142    for _ in range(n_rows):143        with gr.Row():144            for _ in range(n_cols):145                with gr.Column():146                    yield147 148title = "Compare Classifiers!"149with gr.Blocks(title=title) as demo:150    gr.Markdown(f"## {title}")151    gr.Markdown(description)152 153    input_models = list(NAME_CLF_MAPPING)154    input_data = gr.Radio(155        choices=["Moons", "Circles", "Linearly Separable Random Dataset"],156        value="Moons"157    )158    counter = 0159 160 161    for _ in iter_grid(2, 5):162        if counter >= len(input_models):163            break164 165        input_model = input_models[counter]166        plot = gr.Plot(label=input_model)167        fn = partial(train_models, clf_name=input_model)168        input_data.change(fn=fn, inputs=[input_data], outputs=plot)169        counter += 1170 171demo.launch(debug=True)172