eswardivi's picture
Create app.py
d27e5fe
raw
history blame
3.29 kB
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from sklearn import linear_model
import gradio as gr
np.random.seed(0)
def plot_it(X_train_x, X_train_y, Y_train_x, Y_train_y, X_test_x, X_test_y, alpha):
X_train = np.array([[X_train_x, X_train_y]]).T
y_train = [Y_train_x, Y_train_y]
X_test = np.array([[X_test_x, X_test_y]]).T
classifiers = dict(
ols=linear_model.LinearRegression(), ridge=linear_model.Ridge(alpha=alpha)
)
fig, axs = plt.subplots(ncols=len(classifiers), figsize=(8, 4))
for i, (name, clf) in enumerate(classifiers.items()):
ax = axs[i]
for _ in range(6):
this_X = 0.1 * np.random.normal(size=(2, 1)) + X_train
clf.fit(this_X, y_train)
ax.plot(X_test, clf.predict(X_test), color="gray")
ax.scatter(this_X, y_train, s=3, c="gray", marker="o", zorder=10)
clf.fit(X_train, y_train)
ax.plot(X_test, clf.predict(X_test), linewidth=2, color="blue")
ax.scatter(X_train, y_train, s=30, c="red", marker="+", zorder=10)
ax.set_title(name)
ax.set_xlim(0, 2)
ax.set_ylim((0, 1.6))
ax.set_xlabel("X")
ax.set_ylabel("y")
return fig
with gr.Blocks() as demo:
gr.Markdown("# Ordinary Least Squares and Ridge Regression Variance")
gr.Markdown(
"This interactive demo is based on the [Ordinary Least Squares and Ridge Regression Variance](https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols_ridge_variance.html) shows how to use linear regression with OLS and ridge regression, and compare the variance of the coefficients. It generates a synthetic dataset with a small number of features and a large number of samples, fits both models to the data, and plots the variance of the coefficients for each model. It demonstrates that ridge regression can reduce the variance of coefficients when there is multicollinearity between the features, making it a useful tool in certain regression scenarios."
)
gr.Markdown("Select Training points For X_train and Y_train")
with gr.Row():
with gr.Column():
X_train_x = gr.Slider(
value=0.5, minimum=0, maximum=100, step=0.1, label="X_train_x"
)
X_train_y = gr.Slider(
value=1, minimum=0, maximum=100, step=0.1, label="X_train_y"
)
with gr.Column():
Y_train_x = gr.Slider(
value=0.5, minimum=0, maximum=100, step=0.1, label="Y_train_x"
)
Y_train_y = gr.Slider(
value=1, minimum=0, maximum=100, step=0.1, label="Y_train_y"
)
gr.Markdown("X_test")
with gr.Row():
X_test_x = gr.Slider(
value=0, minimum=0, maximum=100, step=0.1, label="X_test_x"
)
X_test_y = gr.Slider(
value=2, minimum=0, maximum=100, step=0.1, label="X_test_y"
)
gr.Markdown("Select Classifier parameters")
alpha = gr.Slider(value=0.5, minimum=0, maximum=100, step=0.1, label="alpha")
gr.Button("Plot").click(
plot_it,
inputs=[X_train_x, X_train_y, Y_train_x, Y_train_y, X_test_x, X_test_y, alpha],
outputs=gr.Plot(),
)
demo.launch()