charting / app.py
JUNGU's picture
Update app.py
3deb8b3
raw
history blame
2.53 kB
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import matplotlib.pyplot as plt
def load_data(file):
return pd.read_csv(file)
def plot_data(data, x_values, y_values, model=None, prediction=None):
plt.scatter(x_values, y_values, label='Data')
if model is not None and prediction is not None:
plt.plot(x_values, prediction, color='red', label='Model')
plt.xlabel('Index')
plt.ylabel('Value')
plt.legend()
plt.show()
def fit_model(data, model_type, x_values, y_values):
if model_type == 'Linear Regression':
model = LinearRegression()
x_values = x_values.reshape(-1, 1)
model.fit(x_values, y_values)
prediction = model.predict(x_values)
equation = f'y = {{model.coef_[0]:.4f}}x + {{model.intercept_:.4f}}'
elif model_type == 'Polynomial Regression':
polynomial_features = PolynomialFeatures(degree=2)
x_values_poly = polynomial_features.fit_transform(x_values.reshape(-1, 1))
model = LinearRegression()
model.fit(x_values_poly, y_values)
prediction = model.predict(x_values_poly)
equation = 'Polynomial equation (degree 2)'
else:
model = None
prediction = None
equation = "No model selected"
return model, prediction, equation
def app():
st.title('RGB and HSV Analysis and Prediction')
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file is not None:
data = load_data(uploaded_file)
st.dataframe(data.head())
# Selecting R, G, B, H, S, V
color_component = st.selectbox("Select color component", ['R', 'G', 'B', 'H', 'S', 'V'])
st.write(f"Selected component: {{color_component}}")
selected_data = data[color_component].values
# Selecting regression model
regression_model = st.selectbox("Select a regression model", ["None", "Linear Regression", "Polynomial Regression"])
x_values = np.arange(len(selected_data))
y_values = selected_data
# Fitting the selected model
model, prediction, equation = fit_model(data, regression_model, x_values, y_values)
st.write(f"Equation: {{equation}}")
# Plotting the data and model
plot_data(data, x_values, y_values, model, prediction)
# Running the app (uncomment this line to run the app locally)
# app()