Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -35,6 +35,8 @@ def manage_session_state():
|
|
35 |
st.session_state.slicers = {}
|
36 |
if 'analysis_performed' not in st.session_state:
|
37 |
st.session_state.analysis_performed = False
|
|
|
|
|
38 |
|
39 |
# 데이터 로드
|
40 |
@st.cache_data
|
@@ -95,14 +97,18 @@ def preprocess_data(data):
|
|
95 |
|
96 |
return data
|
97 |
|
|
|
|
|
|
|
98 |
def create_slicers(data):
|
99 |
for col in st.session_state.categorical_columns:
|
100 |
-
if data[col].nunique() <= 10:
|
101 |
st.session_state.slicers[col] = st.multiselect(
|
102 |
f"{col} 선택",
|
103 |
options=sorted(data[col].unique()),
|
104 |
default=sorted(data[col].unique()),
|
105 |
-
key=f"slicer_{col}"
|
|
|
106 |
)
|
107 |
|
108 |
def apply_slicers(data):
|
@@ -151,61 +157,9 @@ def plot_scatter_with_regression(data, x_var, y_var):
|
|
151 |
st.write(f"상관계수: {r_value:.4f}")
|
152 |
st.write(f"p-value: {p_value:.4f}")
|
153 |
st.write(f"표준 오차: {std_err:.4f}")
|
154 |
-
|
155 |
-
def perform_analysis():
|
156 |
-
if st.session_state.processed_data is not None and not st.session_state.processed_data.empty:
|
157 |
-
st.header("탐색적 데이터 분석")
|
158 |
-
|
159 |
-
# 슬라이서 생성 및 적용
|
160 |
-
create_slicers(st.session_state.processed_data)
|
161 |
-
filtered_data = apply_slicers(st.session_state.processed_data)
|
162 |
-
|
163 |
-
# 요약 통계
|
164 |
-
st.write("요약 통계:")
|
165 |
-
st.write(filtered_data.describe())
|
166 |
-
|
167 |
-
# 상관관계 히트맵
|
168 |
-
st.subheader("상관관계 히트맵")
|
169 |
-
plot_correlation_heatmap(filtered_data)
|
170 |
-
|
171 |
-
# 사용자가 선택한 두 변수에 대한 산점도 및 회귀 분석
|
172 |
-
st.subheader("두 변수 간의 관계 분석")
|
173 |
-
st.session_state.x_var = st.selectbox("X축 변수 선택", options=st.session_state.numeric_columns, key='x_var')
|
174 |
-
st.session_state.y_var = st.selectbox("Y축 변수 선택", options=[col for col in st.session_state.numeric_columns if col != st.session_state.x_var], key='y_var')
|
175 |
-
|
176 |
-
if st.session_state.x_var and st.session_state.y_var:
|
177 |
-
plot_scatter_with_regression(filtered_data, st.session_state.x_var, st.session_state.y_var)
|
178 |
-
|
179 |
-
st.session_state.analysis_performed = True
|
180 |
-
else:
|
181 |
-
st.warning("분석할 데이터가 없습니다. 데이터를 먼저 로드하고 전처리해주세요.")
|
182 |
-
|
183 |
-
|
184 |
-
# state 유지하도록 추가
|
185 |
-
|
186 |
-
def update_filtered_data():
|
187 |
-
st.session_state.filtered_data = apply_slicers(st.session_state.processed_data)
|
188 |
-
|
189 |
-
def create_slicers(data):
|
190 |
-
for col in st.session_state.categorical_columns:
|
191 |
-
if data[col].nunique() <= 10:
|
192 |
-
st.session_state.slicers[col] = st.multiselect(
|
193 |
-
f"{col} 선택",
|
194 |
-
options=sorted(data[col].unique()),
|
195 |
-
default=sorted(data[col].unique()),
|
196 |
-
key=f"slicer_{col}",
|
197 |
-
on_change=update_filtered_data
|
198 |
-
)
|
199 |
-
|
200 |
-
def apply_slicers(data):
|
201 |
-
filtered_data = data.copy()
|
202 |
-
for col, selected_values in st.session_state.slicers.items():
|
203 |
-
if selected_values:
|
204 |
-
filtered_data = filtered_data[filtered_data[col].isin(selected_values)]
|
205 |
-
return filtered_data
|
206 |
|
207 |
def perform_analysis():
|
208 |
-
if
|
209 |
st.session_state.filtered_data = st.session_state.processed_data.copy()
|
210 |
|
211 |
st.header("탐색적 데이터 분석")
|
@@ -234,16 +188,23 @@ def main():
|
|
234 |
|
235 |
manage_session_state()
|
236 |
|
237 |
-
if
|
238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
if st.session_state.data is not None:
|
241 |
st.subheader("데이터 미리보기 및 수정")
|
242 |
st.write("데이터를 확인하고 필요한 경우 수정하세요:")
|
243 |
edited_data = st.data_editor(st.session_state.data, num_rows="dynamic", key="data_editor")
|
244 |
|
245 |
-
if st.button("데이터 분석 시작", key="start_analysis") or
|
246 |
-
if
|
247 |
st.session_state.processed_data = preprocess_data(edited_data)
|
248 |
st.session_state.analysis_performed = True
|
249 |
perform_analysis()
|
|
|
35 |
st.session_state.slicers = {}
|
36 |
if 'analysis_performed' not in st.session_state:
|
37 |
st.session_state.analysis_performed = False
|
38 |
+
if 'filtered_data' not in st.session_state:
|
39 |
+
st.session_state.filtered_data = None
|
40 |
|
41 |
# 데이터 로드
|
42 |
@st.cache_data
|
|
|
97 |
|
98 |
return data
|
99 |
|
100 |
+
def update_filtered_data():
|
101 |
+
st.session_state.filtered_data = apply_slicers(st.session_state.processed_data)
|
102 |
+
|
103 |
def create_slicers(data):
|
104 |
for col in st.session_state.categorical_columns:
|
105 |
+
if data[col].nunique() <= 10:
|
106 |
st.session_state.slicers[col] = st.multiselect(
|
107 |
f"{col} 선택",
|
108 |
options=sorted(data[col].unique()),
|
109 |
default=sorted(data[col].unique()),
|
110 |
+
key=f"slicer_{col}",
|
111 |
+
on_change=update_filtered_data
|
112 |
)
|
113 |
|
114 |
def apply_slicers(data):
|
|
|
157 |
st.write(f"상관계수: {r_value:.4f}")
|
158 |
st.write(f"p-value: {p_value:.4f}")
|
159 |
st.write(f"표준 오차: {std_err:.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
def perform_analysis():
|
162 |
+
if st.session_state.filtered_data is None:
|
163 |
st.session_state.filtered_data = st.session_state.processed_data.copy()
|
164 |
|
165 |
st.header("탐색적 데이터 분석")
|
|
|
188 |
|
189 |
manage_session_state()
|
190 |
|
191 |
+
if st.session_state.data is None:
|
192 |
+
data_input_method = st.radio("데이터 입력 방법 선택:", ("파일 업로드", "수동 입력"), key="data_input_method")
|
193 |
+
|
194 |
+
if data_input_method == "파일 업로드":
|
195 |
+
uploaded_file = st.file_uploader("CSV, XLS, 또는 XLSX 파일을 선택하세요", type=["csv", "xls", "xlsx"], key="file_uploader")
|
196 |
+
if uploaded_file is not None:
|
197 |
+
st.session_state.data = load_data(uploaded_file)
|
198 |
+
else:
|
199 |
+
st.session_state.data = manual_data_entry()
|
200 |
|
201 |
if st.session_state.data is not None:
|
202 |
st.subheader("데이터 미리보기 및 수정")
|
203 |
st.write("데이터를 확인하고 필요한 경우 수정하세요:")
|
204 |
edited_data = st.data_editor(st.session_state.data, num_rows="dynamic", key="data_editor")
|
205 |
|
206 |
+
if st.button("데이터 분석 시작", key="start_analysis") or st.session_state.analysis_performed:
|
207 |
+
if not st.session_state.analysis_performed:
|
208 |
st.session_state.processed_data = preprocess_data(edited_data)
|
209 |
st.session_state.analysis_performed = True
|
210 |
perform_analysis()
|