jiehou's picture
Update app.py
01e1cfa
### CSCI 4750/5750: regression models
import gradio as gr
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
def cal_mse(X,y,b,w):
thetas = np.array([[b], [w]])
X_b = np.c_[np.ones((len(X), 1)), X] # add x0 = 1 to each instance
y_predict = X_b.dot(thetas)
mse = np.mean((y_predict-y)**2)
return mse
def plot_regression(intercept=4, slope=3, intercept_random=4, slope_random=3, multi_line=False):
### (1) generate simulated data points
X = 2 * np.random.rand(100, 1)
y = intercept + slope * X + np.random.randn(100, 1)
### (2) fit regression model
lin_reg = LinearRegression()
lin_reg.fit(X, y)
### (3) make a prediction on training data
y_predict = lin_reg.predict(X)
y_predict
### (4) Draw baseline linear Line
fig = plt.figure(figsize=(12,12))
plt.subplot(2,1,1)
plt.plot(X, y_predict, "r-", linewidth=2, label = "Line of best fit")
plt.plot(X, y, "b.")
### (4.2) Draw random line
if intercept_random != intercept or slope_random != slope: #avoid overlap
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new] # add x0 = 1 to each instance
y_predict = X_new_b.dot(np.array([intercept_random, slope_random]))
plt.plot(X_new, y_predict, "g-", linewidth=2, label = "Random line")
if multi_line:
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new] # add x0 = 1 to each instance
### (4.3) generate grid of parameters and plot multiple lines
b = np.arange(-8,8,0.1) #bias
w = np.arange(-8,8,0.1) #weight
### select random weights/bias for visualization
w_random = w[np.random.choice(range(len(w)), 10)]
b_random = b[np.random.choice(range(len(b)), 10)]
for i in range(len(w_random)):
y_predict = X_new_b.dot(np.array([b_random[i], w_random[i]]))
plt.plot(X_new, y_predict, "g-", linewidth=2)
plt.xlabel("$x_1$", fontsize=22)
plt.ylabel("$y$", rotation=0, fontsize=22)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.axis([np.min(X)*0.1, np.max(X)*1.1, np.min(y)*0.1, np.max(y)*1.1])
plt.title("Linear Regression model predictions", fontsize=22)
plt.legend(fontsize=18)
### (5) Visualize loss function
plt.subplot(2,1,2)
### (5.1) generate grid of parameters
b = np.arange(-10,10,0.1) #bias
w = np.arange(-10,10,0.1) #weight
### (5.2) Calculate MSE over parameters
Z = np.zeros((len(w), len(b)))
for i in range(len(w)):
for j in range(len(b)):
w0 = w[i]
b0 = b[j]
Z[i][j] = cal_mse(X, y, b0, w0)
### (5.3) Get optimal parameters
theta0_best = lin_reg.intercept_[0]
theta1_best = lin_reg.coef_[0][0]
### (5.4) Draw the contour graph
plt.contourf(b,w,Z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))
### (5.5) Add optimal loss
plt.plot(theta0_best, theta1_best, 'x', ms=12, markeredgewidth=3, color='orange')
plt.text(theta0_best, theta1_best,'MSE:'+str(np.round(cal_mse(X,y,theta0_best, theta1_best),2)), color='red', fontsize=22)
### (5.6) Add loss of random lines
if intercept_random != intercept or slope_random != slope: #avoid overlap
plt.plot(intercept_random, slope_random, 'o', ms=5, markeredgewidth=3, color='orange')
plt.text(intercept_random, slope_random,'MSE:'+str(np.round(cal_mse(X,y,intercept_random, slope_random),2)), fontsize=22)
if multi_line:
for i in range(len(w_random)):
plt.plot(b_random[i], w_random[i], 'o', ms=5, markeredgewidth=3, color='orange')
plt.text(b_random[i], w_random[i],'MSE:'+str(np.round(cal_mse(X,y,b_random[i], w_random[i]),2)), fontsize=22)
plt.title("Visualization of Loss function over parameters", fontsize=22)
plt.xlabel("$Intercept$", fontsize=22)
plt.ylabel("$Slope$", rotation=0, fontsize=22)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
#plt.show()
fig.tight_layout()
plt.savefig('plot_line.png', dpi=300)
return 'plot_line.png'
#### Define input component
input_intercept = gr.inputs.Slider(1, 8, step=0.5, default=4, label='(Baseline) Intercept')
input_slope = gr.inputs.Slider(-8, 8, step=0.5, default=2.8, label='(Baseline) Slope')
input_intercept_random = gr.inputs.Slider(-8, 8, step=0.5, default=4, label='(Random) Intercept')
input_slope_random = gr.inputs.Slider(-8, 8, step=0.5, default=2.8, label='(Random) Slope')
input_random_plots = gr.inputs.Checkbox(label="Draw multiple lines")
#### Define output component
output_plot1 = gr.outputs.Image(label="Regression plot")
### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
interface = gr.Interface(fn=plot_regression,
inputs=[input_intercept, input_slope, input_intercept_random, input_slope_random, input_random_plots],
outputs=[output_plot1],
examples_per_page = 2,
examples = [[4, 3, 2, 1, True], [1, 2, 3, 4, False]],
title="CSCI4750/5750: Regression models",
description= "Click examples to generate random dataset",
theme = 'huggingface',
layout = 'vertical'
)
interface.launch(debug=True)