Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
### (1) Generate simulated data
|
| 3 |
+
|
| 4 |
+
import numpy.random as rnd
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import matplotlib
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import numpy as np
|
| 10 |
+
from sklearn.linear_model import LinearRegression
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
np.random.seed(42)
|
| 15 |
+
|
| 16 |
+
m = 100
|
| 17 |
+
X1 = 6 * np.random.rand(m,1) - 3
|
| 18 |
+
|
| 19 |
+
X2 = X1 ** 2
|
| 20 |
+
|
| 21 |
+
y = X1 + 0.5 * X2 + 2 + np.random.randn(m,1)
|
| 22 |
+
|
| 23 |
+
m = 20
|
| 24 |
+
X1_test = 6 * np.random.rand(m,1) - 3
|
| 25 |
+
X2_test = X1_test ** 2
|
| 26 |
+
y_test = X1_test + 0.5 * X2_test + 2 + np.random.randn(m,1)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
import pandas as pd
|
| 30 |
+
|
| 31 |
+
data = pd.DataFrame({'y': y.flatten(), 'X1': X1.flatten(), 'X2': X2.flatten()}, columns=['y', 'X1', 'X2'],)
|
| 32 |
+
data
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def draw_polynomial(degree=2):
|
| 38 |
+
fig = plt.figure(figsize=(12,20))
|
| 39 |
+
|
| 40 |
+
plt.subplot(2,1,1)
|
| 41 |
+
|
| 42 |
+
from sklearn.preprocessing import PolynomialFeatures
|
| 43 |
+
poly_features = PolynomialFeatures(degree=degree, include_bias=False)
|
| 44 |
+
X_poly = poly_features.fit_transform(X1)
|
| 45 |
+
|
| 46 |
+
from sklearn.linear_model import LinearRegression
|
| 47 |
+
lin_reg = LinearRegression()
|
| 48 |
+
lin_reg.fit(X_poly, y)
|
| 49 |
+
lin_reg.intercept_, lin_reg.coef_
|
| 50 |
+
|
| 51 |
+
## Visualized fitted quadratic line on training/testing data
|
| 52 |
+
X_new=np.linspace(-3, 3, 100).reshape(100, 1)
|
| 53 |
+
X_new_poly = poly_features.transform(X_new)
|
| 54 |
+
y_new = lin_reg.predict(X_new_poly)
|
| 55 |
+
plt.plot(X1, y, "b.")
|
| 56 |
+
plt.plot(X1_test, y_test, "g.", markersize=12)
|
| 57 |
+
plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions")
|
| 58 |
+
plt.xlabel("$x_1$", fontsize=18)
|
| 59 |
+
plt.ylabel("$y$", rotation=0, fontsize=18)
|
| 60 |
+
plt.legend(loc="upper left", fontsize=14)
|
| 61 |
+
plt.axis([-3, 3, 0, 10])
|
| 62 |
+
|
| 63 |
+
plt.subplot(2,1,2)
|
| 64 |
+
|
| 65 |
+
plt.plot(polynomial_regression['lin_reg'].coef_[0])
|
| 66 |
+
plt.xlabel("Parameters", fontsize=14)
|
| 67 |
+
plt.ylabel("Values", fontsize=14)
|
| 68 |
+
|
| 69 |
+
#plt.show()
|
| 70 |
+
fig.tight_layout()
|
| 71 |
+
plt.savefig('plot_line.png', dpi=300)
|
| 72 |
+
return 'plot_line.png'
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
#### Define input component
|
| 76 |
+
input_degree = gr.inputs.Slider(1, 50, step=1, default=2, label='Degree of Polynomial Regression')
|
| 77 |
+
|
| 78 |
+
#### Define output component
|
| 79 |
+
output_plot1 = gr.outputs.Image(label="Regression plot")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
|
| 84 |
+
interface = gr.Interface(fn=gradient_descent,
|
| 85 |
+
inputs=[input_degree],
|
| 86 |
+
outputs=[output_plot1],
|
| 87 |
+
title="CSCI4750/5750: Polynomial Regression models \n (Model Complexity)",
|
| 88 |
+
theme = 'huggingface',
|
| 89 |
+
layout = 'vertical'
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
interface.launch(debug=True)
|