Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import matplotlib.pyplot as plt
|
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
from sklearn.linear_model import LinearRegression
|
| 12 |
-
|
| 13 |
|
| 14 |
|
| 15 |
np.random.seed(42)
|
|
@@ -47,6 +47,21 @@ def draw_polynomial(degree=2):
|
|
| 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)
|
|
@@ -96,13 +111,14 @@ def draw_polynomial(degree=2):
|
|
| 96 |
#plt.show()
|
| 97 |
fig.tight_layout()
|
| 98 |
plt.savefig('plot_line.png', dpi=300)
|
| 99 |
-
return results, 'plot_line.png'
|
| 100 |
|
| 101 |
|
| 102 |
#### Define input component
|
| 103 |
input_degree = gr.inputs.Slider(1, 35, step=1, default=2, label='Degree of Polynomial Regression')
|
| 104 |
|
| 105 |
#### Define output component
|
|
|
|
| 106 |
set_output = gr.outputs.Dataframe(type='pandas', label ='Evaluation Results')
|
| 107 |
output_plot1 = gr.outputs.Image(label="Regression plot", type='pil')
|
| 108 |
|
|
@@ -111,7 +127,7 @@ output_plot1 = gr.outputs.Image(label="Regression plot", type='pil')
|
|
| 111 |
### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
|
| 112 |
interface = gr.Interface(fn=draw_polynomial,
|
| 113 |
inputs=[input_degree],
|
| 114 |
-
outputs=[set_output, output_plot1],
|
| 115 |
title="CSCI4750/5750: Polynomial Regression models \n (Model Complexity)",
|
| 116 |
theme = 'huggingface',
|
| 117 |
layout = 'vertical'
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
from sklearn.linear_model import LinearRegression
|
| 12 |
+
import sympy as sp
|
| 13 |
|
| 14 |
|
| 15 |
np.random.seed(42)
|
|
|
|
| 47 |
lin_reg = LinearRegression()
|
| 48 |
lin_reg.fit(X_poly, y)
|
| 49 |
lin_reg.intercept_, lin_reg.coef_
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Sample polynomial coefficients
|
| 53 |
+
coefficients = lin_reg.coef_[0]
|
| 54 |
+
|
| 55 |
+
# Create a symbolic variable for x
|
| 56 |
+
x = sp.symbols('x')
|
| 57 |
+
|
| 58 |
+
# Create the symbolic polynomial expression
|
| 59 |
+
polynomial_expr = lin_reg.intercept_[0]+ sum(coeff * x**power for power, coeff in enumerate(coefficients))
|
| 60 |
+
|
| 61 |
+
# Convert the symbolic expression to a LaTeX string
|
| 62 |
+
latex_expression = sp.latex(polynomial_expr, mode='inline')
|
| 63 |
+
|
| 64 |
+
|
| 65 |
|
| 66 |
## Visualized fitted quadratic line on training/testing data
|
| 67 |
X_new=np.linspace(-3, 3, 100).reshape(100, 1)
|
|
|
|
| 111 |
#plt.show()
|
| 112 |
fig.tight_layout()
|
| 113 |
plt.savefig('plot_line.png', dpi=300)
|
| 114 |
+
return latex_expression, results, 'plot_line.png'
|
| 115 |
|
| 116 |
|
| 117 |
#### Define input component
|
| 118 |
input_degree = gr.inputs.Slider(1, 35, step=1, default=2, label='Degree of Polynomial Regression')
|
| 119 |
|
| 120 |
#### Define output component
|
| 121 |
+
set_formula = gr.outputs.Textbox(label ='Polynomial Model')
|
| 122 |
set_output = gr.outputs.Dataframe(type='pandas', label ='Evaluation Results')
|
| 123 |
output_plot1 = gr.outputs.Image(label="Regression plot", type='pil')
|
| 124 |
|
|
|
|
| 127 |
### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
|
| 128 |
interface = gr.Interface(fn=draw_polynomial,
|
| 129 |
inputs=[input_degree],
|
| 130 |
+
outputs=[set_formula, set_output, output_plot1],
|
| 131 |
title="CSCI4750/5750: Polynomial Regression models \n (Model Complexity)",
|
| 132 |
theme = 'huggingface',
|
| 133 |
layout = 'vertical'
|