File size: 2,637 Bytes
3ec88bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01e6c2b
 
 
 
 
3ec88bd
 
 
01e6c2b
 
 
 
 
3ec88bd
 
264e408
 
 
 
3ec88bd
 
 
 
264e408
3ec88bd
 
 
264e408
3ec88bd
 
264e408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ec88bd
 
 
 
264e408
3ec88bd
 
 
 
 
264e408
3ec88bd
 
 
 
 
 
264e408
3ec88bd
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_regression
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error


def get_plots(min_alpha, max_alpha):
    clf = Ridge()

    X, y, w = make_regression(
        n_samples=10, n_features=10, coef=True, random_state=1, bias=3.5
    )

    coefs = []
    errors = []

    alphas = np.logspace(min_alpha, max_alpha, 200)

    # Train the model with different regularisation strengths
    for a in alphas:
        clf.set_params(alpha=a)
        clf.fit(X, y)
        coefs.append(clf.coef_)
        errors.append(mean_squared_error(clf.coef_, w))

    # Display results
    fig, ax = plt.subplots(1, 2, figsize=(20, 6))

    ax[0].plot(alphas, coefs)
    ax[0].set_xscale("log")
    ax[0].set_xlabel("alpha", fontsize=16)
    ax[0].set_ylabel("weights", fontsize=16)
    ax[0].set_title(
        "Ridge coefficients as a function of the regularization", fontsize=20
    )

    ax[1].plot(alphas, errors)
    ax[1].set_xscale("log")
    ax[1].set_xlabel("alpha", fontsize=16)
    ax[1].set_ylabel("error", fontsize=16)
    ax[1].set_title(
        "Coefficient error as a function of the regularization", fontsize=20
    )
    fig.tight_layout()

    plotted_alphas_text = (
        f"**Plotted alphas between 10^({min_alpha}) and 10^({max_alpha})**"
    )
    return fig, plotted_alphas_text


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column(scale=2):
            gr.Markdown(
                "Choose the range of alpha values to plot."
                + " The models you input for alpha are for the exponents of 10,"
                + " so a value of -6 means 10^6."
            )

            plotted_alphas = gr.Markdown()

        with gr.Column(scale=3):
            with gr.Row():
                min_alpha = gr.Number(
                    step=1,
                    value=-6,
                    label="Minimum Alpha Exponent",
                )
                max_alpha = gr.Number(
                    step=1,
                    value=6,
                    label="Maximum Alpha Exponent",
                )

    plots = gr.Plot()

    min_alpha.change(
        get_plots,
        [min_alpha, max_alpha],
        [plots, plotted_alphas],
        queue=False,
    )
    max_alpha.change(
        get_plots,
        [min_alpha, max_alpha],
        [plots, plotted_alphas],
        queue=False,
    )

    demo.load(
        get_plots,
        [min_alpha, max_alpha],
        [plots, plotted_alphas],
        queue=False,
    )

if __name__ == "__main__":
    demo.launch()