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}")