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