sklearn-docs/MNIST_classification_using_multinomial_logistic_L1
8
1import plotly.express as px2import numpy as np3 4from sklearn.datasets import fetch_openml5from sklearn.linear_model import LogisticRegression6from sklearn.model_selection import train_test_split7from sklearn.preprocessing import StandardScaler8from sklearn.utils import check_random_state9import gradio as gr10 11 12# Load data from https://www.openml.org/d/55413X, y = fetch_openml(14 "mnist_784", version=1, return_X_y=True, as_frame=False, parser="pandas"15)16 17print("Data loaded")18random_state = check_random_state(0)19permutation = random_state.permutation(X.shape[0])20X = X[permutation]21y = y[permutation]22X = X.reshape((X.shape[0], -1))23 24 25scaler = StandardScaler()26 27 28def dataset_display(digit, count_per_digit, binary_image):29 if digit not in range(10):30 # return a figure displaying an error message31 return px.imshow(32 np.zeros((28, 28)),33 labels=dict(x="Pixel columns", y="Pixel rows"),34 title=f"Digit {digit} is not in the data",35 )36 37 binary_value = True if binary_image == 1 else False38 digit_idxs = np.where(y == str(digit))[0]39 random_idxs = np.random.choice(digit_idxs, size=count_per_digit, replace=False)40 41 fig = px.imshow(42 np.array([X[i].reshape(28, 28) for i in random_idxs]),43 labels=dict(x="Pixel columns", y="Pixel rows"),44 title=f"Examples of Digit {digit} in Data",45 facet_col=0,46 facet_col_wrap=5,47 binary_string=binary_value,48 )49 50 return fig51 52 53def predict(img):54 try:55 img = img.reshape(1, -1)56 except:57 return "Show Your Drawing Skills"58 59 try:60 img = scaler.transform(img)61 prediction = clf.predict(img)62 return prediction[0]63 except:64 return "Train the model first"65 66 67def train_model(train_sample=5000, c=0.1, tol=0.1, solver="sage", penalty="l1"):68 X_train, X_test, y_train, y_test = train_test_split(69 X, y, train_size=train_sample, test_size=1000070 )71 72 penalty_dict = {73 "l2": ["lbfgs", "newton-cg", "newton-cholesky", "sag", "saga"],74 "l1": ["liblinear", "saga"],75 "elasticnet": ["saga"],76 }77 78 if solver not in penalty_dict[penalty]:79 return (80 "Solver not supported for the selected penalty",81 "Change the Combination",82 None,83 )84 85 global clf86 global scaler87 scaler = StandardScaler()88 X_train = scaler.fit_transform(X_train)89 X_test = scaler.transform(X_test)90 91 clf = LogisticRegression(C=c, penalty=penalty, solver=solver, tol=tol)92 clf.fit(X_train, y_train)93 sparsity = np.mean(clf.coef_ == 0) * 10094 score = clf.score(X_test, y_test)95 96 coef = clf.coef_.copy()97 scale = np.abs(coef).max()98 99 fig = px.imshow(100 np.array([coef[i].reshape(28, 28) for i in range(10)]),101 labels=dict(x="Pixel columns", y="Pixel rows"),102 title=f"Classification vector for each digit",103 range_color=[-scale, scale],104 facet_col=0,105 facet_col_wrap=5,106 facet_col_spacing=0.01,107 color_continuous_scale="RdBu",108 zmin=-scale,109 zmax=scale,110 )111 112 return score, sparsity, fig113 114 115with gr.Blocks() as demo:116 gr.Markdown("# MNIST classification using multinomial logistic + L1 ")117 gr.Markdown(118 """This interactive demo is based on the [MNIST classification using multinomial logistic + L1](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sparse_logistic_regression_mnist.html#sphx-glr-auto-examples-linear-model-plot-sparse-logistic-regression-mnist-py) example from the popular [scikit-learn](https://scikit-learn.org/stable/) library, which is a widely-used library for machine learning in Python. The primary goal of this demo is to showcase the use of logistic regression in classifying handwritten digits from the [MNIST](https://en.wikipedia.org/wiki/MNIST_database) dataset, which is a well-known benchmark dataset in computer vision. The dataset is loaded from [OpenML](https://www.openml.org/d/554), which is an open platform for machine learning research that provides easy access to a large number of datasets.119The model is trained using the scikit-learn library, which provides a range of tools for machine learning, including classification, regression, and clustering algorithms, as well as tools for data preprocessing and model evaluation. The demo calculates the score and sparsity metrics using test data, which provides insight into the model's performance and sparsity, respectively. The score metric indicates how well the model is performing, while the sparsity metric provides information about the number of non-zero coefficients in the model, which can be useful for interpreting the model and reducing its complexity.120 """121 )122 123 with gr.Tab("Explore the Data"):124 gr.Markdown("## ")125 with gr.Row():126 digit = gr.Slider(0, 9, label="Select the Digit", value=5, step=1)127 count_per_digit = gr.Slider(128 1, 10, label="Number of Images", value=10, step=1129 )130 binary_image = gr.Slider(0, 1, label="Binary Image", value=0, step=1)131 132 gen_btn = gr.Button("Show Me ")133 gen_btn.click(134 dataset_display,135 inputs=[digit, count_per_digit, binary_image],136 outputs=gr.Plot(),137 )138 139 with gr.Tab("Train Your Model"):140 gr.Markdown("# Play with the parameters to see how the model changes")141 142 gr.Markdown("## Solver and penalty")143 gr.Markdown(144 """145 Penalty | Solver146 -------|---------------147 l1 | saga 148 l2 | saga 149 """150 )151 152 with gr.Row():153 train_sample = gr.Slider(154 1000, 60000, label="Train Sample", value=5000, step=1155 )156 157 c = gr.Slider(0.1, 1, label="C", value=0.1, step=0.1)158 tol = gr.Slider(159 0.1, 1, label="Tolerance for stopping criteria.", value=0.1, step=0.1160 )161 max_iter = gr.Slider(100, 1000, label="Max Iter", value=100, step=1)162 163 penalty = gr.Dropdown(164 ["l1", "l2",], label="Penalty", value="l1"165 )166 solver = gr.Dropdown(167 ["saga"],168 label="Solver",169 value="saga",170 )171 172 train_btn = gr.Button("Train")173 train_btn.click(174 train_model,175 inputs=[train_sample, c, tol, solver, penalty],176 outputs=[177 gr.Textbox(label="Score"),178 gr.Textbox(label="Sparsity"),179 gr.Plot(),180 ],181 )182 183 with gr.Tab("Predict the Digit"):184 gr.Markdown("## Draw a digit and see the model's prediction")185 inputs = gr.Sketchpad(brush_radius=1.0)186 outputs = gr.Textbox(label="Predicted Label", lines=1)187 skecth_btn = gr.Button("Classify the Sketch")188 skecth_btn.click(predict, inputs, outputs)189 190 191demo.launch()192 