File size: 2,527 Bytes
87d5e36
bd820cc
 
e741394
 
 
3deb8b3
e741394
3deb8b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

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()