sklearn-docs/Precision-Recall
1
1import numpy as np2import gradio as gr3from sklearn.svm import LinearSVC4from sklearn.datasets import load_iris5from sklearn.pipeline import make_pipeline6from sklearn.multiclass import OneVsRestClassifier7from sklearn.model_selection import train_test_split8from sklearn.preprocessing import label_binarize, StandardScaler9 10import utils11 12 13def app_fn(n_random_features: int, test_size: float, random_state_val: int):14 X, y = load_iris(return_X_y=True)15 16 # Add noisy features17 random_state = np.random.RandomState(random_state_val)18 n_samples, n_features = X.shape19 X = np.concatenate([X, random_state.randn(n_samples, n_random_features)], axis=1)20 21 # Solving Binary Problem22 X_train, X_test, y_train, y_test = train_test_split(23 X[y < 2], y[y < 2], test_size=test_size, random_state=random_state24 )25 26 clf_bin = make_pipeline(StandardScaler(), LinearSVC(random_state=random_state))27 clf_bin.fit(X_train, y_train)28 29 fig_bin = utils.plot_binary_pr_curve(clf_bin, X_test, y_test)30 31 # Solving Multi-Label Problem32 Y = label_binarize(y, classes=[0, 1, 2])33 X_train_multi, X_test_multi, Y_train, Y_test = train_test_split(34 X, Y, test_size=test_size, random_state=random_state35 )36 37 clf = OneVsRestClassifier(38 make_pipeline(StandardScaler(), LinearSVC(random_state=random_state))39 )40 clf.fit(X_train_multi, Y_train)41 42 fig_multi = utils.plot_multi_label_pr_curve(clf, X_test_multi, Y_test)43 44 return fig_bin, fig_multi45 46 47title = "Precision-Recall Curves"48with gr.Blocks(title=title) as demo:49 gr.Markdown(f"# {title}")50 gr.Markdown(51 """52 This demo shows the precision-recall curves on the Iris dataset \53 using a Linear SVM classifier + StandardScaler. \54 Noise is added to the dataset to make the problem more challenging. \55 The dataset is split into train and test sets. \56 The model is trained on the train set and evaluated on the test set. \57 Two separate problems are solved: 58 59 - Binary classification: class 0 vs class 160 - Multi-label classification: class 0 vs class 1 vs class 261 62 See the scikit-learn example [here](https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html#sphx-glr-auto-examples-model-selection-plot-precision-recall-py).63 """64 )65 66 with gr.Row():67 n_random_features = gr.inputs.Slider(0, 1000, 50, 800,label="Number of Random Features")68 test_size = gr.inputs.Slider(0.1, 0.9, 0.01, 0.5, label="Test Size")69 random_state_val = gr.inputs.Slider(0, 100, 5, 0,label="Random State")70 71 72 with gr.Row():73 fig_bin = gr.Plot(label="Binary PR Curve")74 fig_multi = gr.Plot(label="Multi-Label PR Curve")75 76 n_random_features.change(fn=app_fn, inputs=[n_random_features, test_size, random_state_val], outputs=[fig_bin, fig_multi])77 test_size.change(fn=app_fn, inputs=[n_random_features, test_size, random_state_val], outputs=[fig_bin, fig_multi])78 random_state_val.change(fn=app_fn, inputs=[n_random_features, test_size, random_state_val], outputs=[fig_bin, fig_multi])79 80 demo.load(fn=app_fn, inputs=[n_random_features, test_size, random_state_val], outputs=[fig_bin, fig_multi])81 82demo.launch()83 84 85 