Nifdi01 commited on
Commit
9161b1a
·
1 Parent(s): 40f1e06

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ import pandas as pd
5
+ from sklearn.linear_model import LinearRegression
6
+ from sklearn.preprocessing import PolynomialFeatures
7
+
8
+ st.set_option('deprecation.showPyplotGlobalUse', False)
9
+
10
+ st.title('Polynomial Regression Prediction App')
11
+
12
+ default_X = "0, 1, 2, -1, -2"
13
+ default_Y = "1, 6, 33, 0, 9"
14
+
15
+ X_input = st.text_area('Enter the X values (comma-separated):', value=default_X)
16
+ Y_input = st.text_area('Enter the Y values (comma-separated):', value=default_Y)
17
+
18
+ X = np.array([float(x) for x in X_input.split(',')]).reshape(-1, 1)
19
+ Y = np.array([float(y) for y in Y_input.split(',')])
20
+
21
+ degree = len(X)-1
22
+
23
+ poly = PolynomialFeatures(degree=degree)
24
+ X_poly = poly.fit_transform(X)
25
+
26
+ regressor = LinearRegression()
27
+ regressor.fit(X_poly, Y)
28
+
29
+ x_values = np.linspace(min(X), max(X), 100).reshape(-1, 1)
30
+ x_values_poly = poly.transform(x_values)
31
+ y_predicted = regressor.predict(x_values_poly)
32
+
33
+ st.write("### Polynomial Regression Prediction Plot")
34
+ plt.scatter(X, Y, color='red', label='Data')
35
+ plt.plot(x_values, y_predicted, color='blue', label='Predicted')
36
+ plt.title(f'Polynomial Regression Prediction (Degree {degree})')
37
+ plt.xlabel('X')
38
+ plt.ylabel('Y')
39
+ plt.legend()
40
+ st.pyplot()
41
+
42
+ y_predicted = regressor.predict(X_poly)
43
+ data = {'X': X.ravel(), 'Y': Y, 'Y_pred': y_predicted}
44
+ df = pd.DataFrame(data)
45
+ st.write("### Dataframe with Predicted Values")
46
+ st.write(df)
47
+
48
+ coefficients = regressor.coef_
49
+ coeff_data = {'Feature': [f'X^{i}' for i in range(1, degree + 1)], 'Coefficient': coefficients[1:]}
50
+ coeff_df = pd.DataFrame(coeff_data)
51
+ st.write("### Coefficients of Polynomial Terms")
52
+ st.write(coeff_df)
53
+
54
+
55
+
56
+ coefficients = [i for i in regressor.coef_]
57
+ terms = [f'{coeff:.3f}X^{i}' for i, coeff in enumerate(coefficients) if coeff != 0]
58
+
59
+ latex_equation = r'''
60
+ Our Equation: {:.3f} + {}
61
+ '''.format(regressor.intercept_, ' + '.join(terms))
62
+
63
+ st.write("### Polynomial Equation")
64
+ st.latex(latex_equation)
65
+
66
+
67
+ def calculate_polynomial_value(coefficients, X, intercept):
68
+ result = sum(coeff * (X ** i) for i, coeff in enumerate(coefficients))
69
+ return result + intercept
70
+
71
+ X_to_calculate = st.number_input('Enter the X value for prediction:')
72
+ result = calculate_polynomial_value(coefficients, X_to_calculate, regressor.intercept_)
73
+ st.write(f"Predicted Y value at X = {X_to_calculate:.2f} is {result:.2f}")