charting / app.py
JUNGU's picture
Update app.py
7501233 verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
st.title("๋ฐ์ดํ„ฐ(csv)ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”")
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
time_frame_options = [
"All",
"1 second",
"5 seconds",
"10 seconds",
"30 seconds",
"1 minute",
"5 minutes",
"10 minutes",
"30 minutes",
"60 minutes",
]
time_frame = st.selectbox("Data Time Frame", time_frame_options)
if uploaded_file is not None:
# Read CSV file
data = pd.read_csv(uploaded_file)
# Filter data according to the time frame
if time_frame != "All":
seconds = {
"1 second": 1,
"5 seconds": 5,
"10 seconds": 10,
"30 seconds": 30,
"1 minute": 60,
"5 minutes": 300,
"10 minutes": 600,
"30 minutes": 1800,
"60 minutes": 3600,
}
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms')
data.set_index('timestamp', inplace=True)
data = data.resample(f"{seconds[time_frame]}S").mean().dropna().reset_index()
# Let the user select the columns
selected_columns = st.multiselect("Select Columns", options=['R', 'G', 'B', 'H', 'S', 'V'])
# Create charts based on selected columns
fig, ax = plt.subplots(figsize=(10, 5))
for col in selected_columns:
ax.plot(data[col], label=col)
ax.legend(loc='upper left')
st.pyplot(fig)
# Selecting target and features
target_column = st.selectbox("Select Target Column", options=selected_columns)
feature_columns = st.multiselect("Select Feature Columns", options=[col for col in selected_columns if col != target_column])
# Defining models
models = {
"Linear Regression": LinearRegression(),
"Polynomial Regression": make_pipeline(PolynomialFeatures(degree=2), LinearRegression()),
"SVR (Support Vector Regression)": SVR(),
"Random Forest Regression": RandomForestRegressor()
}
# Selecting model
selected_model = st.selectbox("Select Regression Model", options=list(models.keys()))
# Fitting the model
if st.button("Fit Model"):
if feature_columns: # Check if feature columns are selected
X = data[feature_columns]
y = data[target_column]
model = models[selected_model]
model.fit(X, y)
# Predicting and plotting
predictions = model.predict(X)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(y, label="Actual")
ax.plot(predictions, label="Predicted")
ax.legend(loc='upper left')
st.pyplot(fig)
else:
st.error("Please select at least one feature column.")
else:
st.warning("Please upload a CSV file.")