Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import plotly.express as px | |
import plotly.graph_objects as go | |
from io import StringIO | |
import openpyxl | |
import matplotlib.font_manager as fm | |
from scipy import stats | |
# νκΈ ν°νΈ μ€μ | |
def set_font(): | |
font_path = "Pretendard-Bold.ttf" # μ€μ ν°νΈ νμΌ κ²½λ‘λ‘ λ³κ²½ν΄μ£ΌμΈμ | |
fm.fontManager.addfont(font_path) | |
return {'font.family': 'Pretendard-Bold', 'axes.unicode_minus': False} | |
# ν°νΈ μ€μ μ κ°μ Έμ΅λλ€ | |
font_settings = set_font() | |
def load_data(file): | |
file_extension = file.name.split('.')[-1].lower() | |
if file_extension == 'csv': | |
data = pd.read_csv(file) | |
elif file_extension in ['xls', 'xlsx']: | |
data = pd.read_excel(file) | |
else: | |
st.error("μ§μλμ§ μλ νμΌ νμμ λλ€. CSV, XLS, λλ XLSX νμΌμ μ λ‘λν΄μ£ΌμΈμ.") | |
return None | |
return data | |
def manual_data_entry(): | |
st.subheader("μλ λ°μ΄ν° μ λ ₯") | |
col_names = st.text_input("μ΄ μ΄λ¦μ μΌνλ‘ κ΅¬λΆνμ¬ μ λ ₯νμΈμ:").split(',') | |
col_names = [name.strip() for name in col_names if name.strip()] | |
if col_names: | |
num_rows = st.number_input("μ΄κΈ° νμ μλ₯Ό μ λ ₯νμΈμ:", min_value=1, value=5) | |
data = pd.DataFrame(columns=col_names, index=range(num_rows)) | |
edited_data = st.data_editor(data, num_rows="dynamic") | |
return edited_data | |
return None | |
def preprocess_data(data): | |
st.subheader("λ°μ΄ν° μ μ²λ¦¬") | |
# κ²°μΈ‘μΉ μ²λ¦¬ | |
if data.isnull().sum().sum() > 0: | |
st.write("κ²°μΈ‘μΉ μ²λ¦¬:") | |
for column in data.columns: | |
if data[column].isnull().sum() > 0: | |
method = st.selectbox(f"{column} μ΄μ μ²λ¦¬ λ°©λ² μ ν:", | |
["μ κ±°", "νκ· μΌλ‘ λ체", "μ€μκ°μΌλ‘ λ체", "μ΅λΉκ°μΌλ‘ λ체"]) | |
if method == "μ κ±°": | |
data = data.dropna(subset=[column]) | |
elif method == "νκ· μΌλ‘ λ체": | |
data[column].fillna(data[column].mean(), inplace=True) | |
elif method == "μ€μκ°μΌλ‘ λ체": | |
data[column].fillna(data[column].median(), inplace=True) | |
elif method == "μ΅λΉκ°μΌλ‘ λ체": | |
data[column].fillna(data[column].mode()[0], inplace=True) | |
# λ°μ΄ν° νμ λ³ν | |
for column in data.columns: | |
if data[column].dtype == 'object': | |
try: | |
data[column] = pd.to_numeric(data[column]) | |
st.write(f"{column} μ΄μ μ«μνμΌλ‘ λ³ννμ΅λλ€.") | |
except ValueError: | |
st.write(f"{column} μ΄μ λ²μ£ΌνμΌλ‘ μ μ§λ©λλ€.") | |
return data | |
def create_slicers(data): | |
slicers = {} | |
categorical_columns = data.select_dtypes(include=['object', 'category']).columns | |
for col in categorical_columns: | |
if data[col].nunique() <= 10: # κ³ μ κ°μ΄ 10κ° μ΄νμΈ κ²½μ°μλ§ μ¬λΌμ΄μ μμ± | |
slicers[col] = st.multiselect(f"{col} μ ν", options=sorted(data[col].unique()), default=sorted(data[col].unique())) | |
return slicers | |
def apply_slicers(data, slicers): | |
for col, selected_values in slicers.items(): | |
if selected_values: | |
data = data[data[col].isin(selected_values)] | |
return data | |
def perform_analysis(data): | |
st.header("νμμ λ°μ΄ν° λΆμ") | |
# μ¬λΌμ΄μ μμ± | |
slicers = create_slicers(data) | |
# μ¬λΌμ΄μ μ μ© | |
filtered_data = apply_slicers(data, slicers) | |
# μμ½ ν΅κ³ | |
st.write("μμ½ ν΅κ³:") | |
st.write(filtered_data.describe()) | |
# μκ΄κ΄κ³ ννΈλ§΅ | |
st.write("μκ΄κ΄κ³ ννΈλ§΅:") | |
numeric_data = filtered_data.select_dtypes(include=['float64', 'int64']) | |
if not numeric_data.empty: | |
fig = px.imshow(numeric_data.corr(), color_continuous_scale='RdBu_r', zmin=-1, zmax=1) | |
fig.update_layout(title='μκ΄κ΄κ³ ννΈλ§΅') | |
st.plotly_chart(fig) | |
else: | |
st.write("μκ΄κ΄κ³ ννΈλ§΅μ 그릴 μ μλ μ«μν μ΄μ΄ μμ΅λλ€.") | |
# μ¬μ©μκ° μ νν λ λ³μμ λν μ°μ λ λ° νκ· λΆμ | |
st.subheader("λ λ³μ κ°μ κ΄κ³ λΆμ") | |
numeric_columns = filtered_data.select_dtypes(include=['float64', 'int64']).columns | |
x_var = st.selectbox("XμΆ λ³μ μ ν", options=numeric_columns) | |
y_var = st.selectbox("YμΆ λ³μ μ ν", options=[col for col in numeric_columns if col != x_var]) | |
if x_var and y_var: | |
fig = px.scatter(filtered_data, x=x_var, y=y_var, color='λ°' if 'λ°' in filtered_data.columns else None) | |
# νκ·μ μΆκ° | |
x = filtered_data[x_var] | |
y = filtered_data[y_var] | |
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y) | |
line_x = np.array([x.min(), x.max()]) | |
line_y = slope * line_x + intercept | |
fig.add_trace(go.Scatter(x=line_x, y=line_y, mode='lines', name='νκ·μ ')) | |
r_squared = r_value ** 2 | |
fig.update_layout( | |
title=f'{x_var}μ {y_var}μ κ΄κ³ (R-squared: {r_squared:.4f})', | |
xaxis_title=x_var, | |
yaxis_title=y_var, | |
annotations=[ | |
dict( | |
x=0.5, | |
y=1.05, | |
xref='paper', | |
yref='paper', | |
text=f'R-squared: {r_squared:.4f}', | |
showarrow=False, | |
) | |
] | |
) | |
st.plotly_chart(fig) | |
# μΆκ° ν΅κ³ μ 보 | |
st.write(f"μκ΄κ³μ: {r_value:.4f}") | |
st.write(f"p-value: {p_value:.4f}") | |
st.write(f"νμ€ μ€μ°¨: {std_err:.4f}") | |
def main(): | |
st.title("μΈν°λν°λΈ EDA ν΄ν·") | |
data_input_method = st.radio("λ°μ΄ν° μ λ ₯ λ°©λ² μ ν:", ("νμΌ μ λ‘λ", "μλ μ λ ₯")) | |
if data_input_method == "νμΌ μ λ‘λ": | |
uploaded_file = st.file_uploader("CSV, XLS, λλ XLSX νμΌμ μ ννμΈμ", type=["csv", "xls", "xlsx"]) | |
if uploaded_file is not None: | |
data = load_data(uploaded_file) | |
else: | |
data = None | |
else: | |
data = manual_data_entry() | |
if data is not None: | |
st.subheader("λ°μ΄ν° 미리보기 λ° μμ ") | |
st.write("λ°μ΄ν°λ₯Ό νμΈνκ³ νμν κ²½μ° μμ νμΈμ:") | |
edited_data = st.data_editor(data, num_rows="dynamic") | |
if st.button("λ°μ΄ν° λΆμ μμ"): | |
processed_data = preprocess_data(edited_data) | |
perform_analysis(processed_data) | |
if __name__ == "__main__": | |
main() |