""" =========================================================================== Gradio Demo to Plot Ridge coefficients as a function of the regularization =========================================================================== Shows the effect of collinearity in the coefficients of an estimator. .. currentmodule:: sklearn.linear_model :class:`Ridge` Regression is the estimator used in this example. Each color represents a different feature of the coefficient vector, and this is displayed as a function of the regularization parameter. This example also shows the usefulness of applying Ridge regression to highly ill-conditioned matrices. For such matrices, a slight change in the target variable can cause huge variances in the calculated weights. In such cases, it is useful to set a certain regularization (alpha) to reduce this variation (noise). When alpha is very large, the regularization effect dominates the squared loss function and the coefficients tend to zero. At the end of the path, as alpha tends toward zero and the solution tends towards the ordinary least squares, coefficients exhibit big oscillations. In practise it is necessary to tune alpha in such a way that a balance is maintained between both. """ # Author: Fabian Pedregosa -- # License: BSD 3 clause # Demo Author: Syed Affan import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model import gradio as gr def make_plot(size_X,min_alpha,max_alpha): # X is the 10x10 Hilbert matrix X = 1.0 / (np.arange(1, size_X+1) + np.arange(0, size_X)[:, np.newaxis]) y = np.ones(size_X) # %% # Compute paths # ------------- fig = plt.figure() n_alphas = 200 alphas = np.logspace(min_alpha, max_alpha, n_alphas) coefs = [] for a in alphas: ridge = linear_model.Ridge(alpha=a, fit_intercept=False) ridge.fit(X, y) coefs.append(ridge.coef_) # %% # Display results # --------------- ax = plt.gca() ax.plot(alphas, coefs) ax.set_xscale("log") ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis plt.xlabel("alpha") plt.ylabel("weights") plt.title("Ridge coefficients as a function of the regularization") plt.axis("tight") return fig title='Plot Ridge coefficients as a function of the regularization' model_card=f""" ## Description This interactive demo is based on the [Plot Ridge coefficients as a function of the regularization](https://scikit-learn.org/stable/_downloads/9d5a4167bc60f250de65fe21497c1eb6/plot_ridge_path.py) example from the popular [scikit-learn](https://scikit-learn.org/stable/) library, which is a widely-used library for machine learning in Python. This demo demonstrates the effect of collinearity in the coefficients of an estimator by plotting the regularization selected against the coefficients that are learnt by the model. It also shows the usefulness of applying Ridge regression to highly ill-conditioned matrices. For such matrices, a slight change in the target variable can cause huge variances in the calculated weights. In such cases, it is useful to set a certain regularization (alpha) to reduce this variation (noise). You can play with the range of `Alpha` values and the `Training Size` When alpha is very large, the regularization effect dominates the squared loss function and the coefficients tend to zero. At the end of the path, as alpha tends toward zero and the solution tends towards the ordinary least squares, coefficients exhibit big oscillations. In practise it is necessary to tune alpha in such a way that a balance is maintained between both. ## Model currentmodule: [sklearn.linear_model](https://scikit-learn.org/stable/modules/classes.html#module-sklearn.linear_model) class:`Ridge` Regression is the estimator used in this example. Each color represents a different feature of the coefficient vector, and this is displayed as a function of the regularization parameter. """ with gr.Blocks(title=title) as demo: gr.Markdown('''

Plot Ridge coefficients as a function of the regularization

''') gr.Markdown(model_card) gr.Markdown("Author: sulpha") d0 = gr.Slider(1,101,value=10,step=10,label='Select Size of Training Set') with gr.Column(): with gr.Tab('Select Alpha Range'): d1 = gr.Slider(-20,20,value=-10,step=1,label='Creates an array of regularization values which are fed to the model and plotted against the returned weights') d2 = gr.Slider(-20,20,value=-2,step=1,label='') o1=gr.Plot() #btn = gr.Button(value = 'Submit') d0.change(fn=make_plot,inputs=[d0,d1,d2],outputs=[o1]) d1.change(fn=make_plot,inputs=[d0,d1,d2],outputs=[o1]) d2.change(fn=make_plot,inputs=[d0,d1,d2],outputs=[o1]) #btn.click(make_plot,inputs=[d0,d1,d2],outputs=[gr.Plot()]) demo.launch()