File size: 17,863 Bytes
da8e446
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, LabelEncoder, MinMaxScaler
from sklearn.impute import SimpleImputer, KNNImputer
from sklearn.ensemble import IsolationForest
import plotly.express as px
import plotly.graph_objects as go

def preprocess_data(df):
    
    st.markdown("""

    <div style='text-align: center; margin-bottom: 2rem;'>

        <h2>βš™οΈ Data Preprocessing Pipeline</h2>

        <p style='color: gray;'>Clean, transform, and prepare your data for analysis</p>

    </div>

    """, unsafe_allow_html=True)
    
    # Create tabs for different preprocessing steps
    tab1, tab2, tab3, tab4, tab5 = st.tabs([
        "πŸ“Š Overview", "🧹 Clean Data", "πŸ”„ Transform", 
        "πŸ“ Scale & Encode", "πŸ“ˆ Feature Engineering"
    ])
    
    with tab1:
        st.markdown('<div class="custom-card">', unsafe_allow_html=True)
        
        col1, col2, col3 = st.columns(3)
        
        with col1:
            st.metric("Original Rows", df.shape[0])
        with col2:
            st.metric("Original Columns", df.shape[1])
        with col3:
            missing_pct = (df.isnull().sum().sum() / (df.shape[0] * df.shape[1])) * 100
            st.metric("Missing Data", f"{missing_pct:.1f}%")
        
        # Data quality before preprocessing
        st.subheader("Data Quality Check")
        
        quality_df = pd.DataFrame({
            'Column': df.columns,
            'Data Type': df.dtypes,
            'Missing Values': df.isnull().sum(),
            'Missing %': (df.isnull().sum() / len(df) * 100).round(2),
            'Unique Values': [df[col].nunique() for col in df.columns]
        })
        
        st.dataframe(quality_df, use_container_width=True)
        
        # Visualize missing values
        if df.isnull().sum().sum() > 0:
            st.subheader("Missing Value Heatmap")
            missing_df = df.isnull().astype(int)
            fig = px.imshow(missing_df.T, 
                          color_continuous_scale='reds',
                          aspect="auto",
                          title="Missing Values Pattern")
            st.plotly_chart(fig, use_container_width=True)
        
        st.markdown('</div>', unsafe_allow_html=True)
    
    with tab2:
        st.markdown('<div class="custom-card">', unsafe_allow_html=True)
        st.subheader("🧹 Data Cleaning Options")
        
        # Create a copy for processing
        processed_df = df.copy()
        
        # Remove duplicates
        st.markdown("### Duplicate Removal")
        duplicates = processed_df.duplicated().sum()
        st.write(f"Duplicate rows found: **{duplicates}**")
        
        if duplicates > 0:
            if st.button("Remove Duplicates", use_container_width=True):
                processed_df = processed_df.drop_duplicates()
                st.success(f"βœ… Removed {duplicates} duplicate rows")
        
        # Handle missing values
        st.markdown("### Missing Value Handling")
        
        missing_cols = processed_df.columns[processed_df.isnull().any()].tolist()
        
        if missing_cols:
            selected_col = st.selectbox("Select column to handle missing values", missing_cols)
            
            col_type = processed_df[selected_col].dtype
            
            if pd.api.types.is_numeric_dtype(processed_df[selected_col]):
                method = st.radio(
                    "Choose imputation method",
                    ["Mean", "Median", "Mode", "KNN Imputer", "Drop rows", "Fill with value"]
                )
                
                if method == "Mean":
                    processed_df[selected_col].fillna(processed_df[selected_col].mean(), inplace=True)
                elif method == "Median":
                    processed_df[selected_col].fillna(processed_df[selected_col].median(), inplace=True)
                elif method == "Mode":
                    processed_df[selected_col].fillna(processed_df[selected_col].mode()[0], inplace=True)
                elif method == "KNN Imputer":
                    st.info("KNN Imputer will be applied to all numeric columns")
                    if st.button("Apply KNN Imputer"):
                        numeric_cols = processed_df.select_dtypes(include=[np.number]).columns
                        imputer = KNNImputer(n_neighbors=5)
                        processed_df[numeric_cols] = imputer.fit_transform(processed_df[numeric_cols])
                elif method == "Drop rows":
                    if st.button(f"Drop rows with missing values in {selected_col}"):
                        processed_df = processed_df.dropna(subset=[selected_col])
                else:
                    fill_value = st.text_input("Enter fill value")
                    if fill_value:
                        if pd.api.types.is_numeric_dtype(processed_df[selected_col]):
                            processed_df[selected_col].fillna(float(fill_value), inplace=True)
                        else:
                            processed_df[selected_col].fillna(fill_value, inplace=True)
            
            else:  # Categorical column
                method = st.radio(
                    "Choose imputation method",
                    ["Mode", "Drop rows", "Fill with value"]
                )
                
                if method == "Mode":
                    processed_df[selected_col].fillna(processed_df[selected_col].mode()[0], inplace=True)
                elif method == "Drop rows":
                    if st.button(f"Drop rows with missing values in {selected_col}"):
                        processed_df = processed_df.dropna(subset=[selected_col])
                else:
                    fill_value = st.text_input("Enter fill value")
                    if fill_value:
                        processed_df[selected_col].fillna(fill_value, inplace=True)
        else:
            st.success("βœ… No missing values found!")
        
        # Outlier detection
        st.markdown("### Outlier Detection")
        numeric_cols = processed_df.select_dtypes(include=[np.number]).columns
        
        if len(numeric_cols) > 0:
            selected_num = st.selectbox("Select numeric column for outlier detection", numeric_cols)
            
            # Calculate IQR
            Q1 = processed_df[selected_num].quantile(0.25)
            Q3 = processed_df[selected_num].quantile(0.75)
            IQR = Q3 - Q1
            
            outliers = processed_df[
                (processed_df[selected_num] < Q1 - 1.5 * IQR) | 
                (processed_df[selected_num] > Q3 + 1.5 * IQR)
            ]
            
            st.write(f"Outliers detected: **{len(outliers)}** rows")
            
            if len(outliers) > 0:
                if st.button(f"Remove outliers from {selected_num}"):
                    processed_df = processed_df[
                        (processed_df[selected_num] >= Q1 - 1.5 * IQR) & 
                        (processed_df[selected_num] <= Q3 + 1.5 * IQR)
                    ]
                    st.success(f"βœ… Removed {len(outliers)} outliers")
        
        st.markdown('</div>', unsafe_allow_html=True)
        
        # Update session state
        st.session_state.data = processed_df
        
    with tab3:
        st.markdown('<div class="custom-card">', unsafe_allow_html=True)
        st.subheader("πŸ”„ Data Transformations")
        
        processed_df = st.session_state.data.copy() if 'processed_df' not in locals() else processed_df
        
        # Column operations
        st.markdown("### Column Operations")
        
        operation = st.selectbox(
            "Choose operation",
            ["Create new column", "Rename column", "Drop column", "Change data type"]
        )
        
        if operation == "Create new column":
            col1, col2, col3 = st.columns(3)
            with col1:
                new_col_name = st.text_input("New column name")
            with col2:
                col_to_use = st.selectbox("Based on column", processed_df.columns)
            with col3:
                operation_type = st.selectbox(
                    "Operation",
                    ["Square", "Square Root", "Log", "Absolute", "Round", "Binary encode"]
                )
            
            if st.button("Create column") and new_col_name:
                if operation_type == "Square":
                    processed_df[new_col_name] = processed_df[col_to_use] ** 2
                elif operation_type == "Square Root":
                    processed_df[new_col_name] = np.sqrt(processed_df[col_to_use])
                elif operation_type == "Log":
                    processed_df[new_col_name] = np.log1p(processed_df[col_to_use])
                elif operation_type == "Absolute":
                    processed_df[new_col_name] = np.abs(processed_df[col_to_use])
                elif operation_type == "Round":
                    processed_df[new_col_name] = np.round(processed_df[col_to_use])
                elif operation_type == "Binary encode":
                    threshold = st.number_input("Threshold for binary encoding")
                    processed_df[new_col_name] = (processed_df[col_to_use] > threshold).astype(int)
                
                st.success(f"βœ… Created column: {new_col_name}")
        
        elif operation == "Rename column":
            col_to_rename = st.selectbox("Select column to rename", processed_df.columns)
            new_name = st.text_input("New column name")
            
            if st.button("Rename") and new_name:
                processed_df.rename(columns={col_to_rename: new_name}, inplace=True)
                st.success(f"βœ… Renamed {col_to_rename} to {new_name}")
        
        elif operation == "Drop column":
            cols_to_drop = st.multiselect("Select columns to drop", processed_df.columns)
            
            if st.button("Drop columns") and cols_to_drop:
                processed_df = processed_df.drop(columns=cols_to_drop)
                st.success(f"βœ… Dropped columns: {', '.join(cols_to_drop)}")
        
        elif operation == "Change data type":
            col_to_change = st.selectbox("Select column", processed_df.columns)
            new_type = st.selectbox(
                "New data type",
                ["int", "float", "str", "datetime", "category"]
            )
            
            if st.button("Change type"):
                try:
                    if new_type == "int":
                        processed_df[col_to_change] = processed_df[col_to_change].astype(int)
                    elif new_type == "float":
                        processed_df[col_to_change] = processed_df[col_to_change].astype(float)
                    elif new_type == "str":
                        processed_df[col_to_change] = processed_df[col_to_change].astype(str)
                    elif new_type == "datetime":
                        processed_df[col_to_change] = pd.to_datetime(processed_df[col_to_change])
                    elif new_type == "category":
                        processed_df[col_to_change] = processed_df[col_to_change].astype('category')
                    
                    st.success(f"βœ… Changed {col_to_change} to {new_type}")
                except Exception as e:
                    st.error(f"Error: {str(e)}")
        
        st.markdown('</div>', unsafe_allow_html=True)
        
        # Update session state
        st.session_state.data = processed_df
    
    with tab4:
        st.markdown('<div class="custom-card">', unsafe_allow_html=True)
        st.subheader("πŸ“ Feature Scaling & Encoding")
        
        processed_df = st.session_state.data.copy() if 'processed_df' not in locals() else processed_df
        
        col1, col2 = st.columns(2)
        
        with col1:
            st.markdown("### Feature Scaling")
            numeric_cols = processed_df.select_dtypes(include=[np.number]).columns.tolist()
            
            if numeric_cols:
                scale_cols = st.multiselect("Select columns to scale", numeric_cols)
                scale_method = st.radio("Scaling method", ["StandardScaler", "MinMaxScaler"])
                
                if st.button("Apply Scaling") and scale_cols:
                    if scale_method == "StandardScaler":
                        scaler = StandardScaler()
                    else:
                        scaler = MinMaxScaler()
                    
                    processed_df[scale_cols] = scaler.fit_transform(processed_df[scale_cols])
                    st.success(f"βœ… Applied {scale_method} to {len(scale_cols)} columns")
        
        with col2:
            st.markdown("### Categorical Encoding")
            cat_cols = processed_df.select_dtypes(include=['object', 'category']).columns.tolist()
            
            if cat_cols:
                encode_cols = st.multiselect("Select columns to encode", cat_cols)
                encode_method = st.radio("Encoding method", ["Label Encoding", "One-Hot Encoding"])
                
                if st.button("Apply Encoding") and encode_cols:
                    if encode_method == "Label Encoding":
                        for col in encode_cols:
                            le = LabelEncoder()
                            processed_df[col + '_encoded'] = le.fit_transform(processed_df[col])
                        st.success(f"βœ… Applied Label Encoding to {len(encode_cols)} columns")
                    else:
                        processed_df = pd.get_dummies(processed_df, columns=encode_cols)
                        st.success(f"βœ… Applied One-Hot Encoding to {len(encode_cols)} columns")
        
        st.markdown('</div>', unsafe_allow_html=True)
        
        # Update session state
        st.session_state.data = processed_df
    
    with tab5:
        st.markdown('<div class="custom-card">', unsafe_allow_html=True)
        st.subheader("πŸ“ˆ Feature Engineering")
        
        processed_df = st.session_state.data.copy() if 'processed_df' not in locals() else processed_df
        
        # Feature interactions
        st.markdown("### Feature Interactions")
        numeric_cols = processed_df.select_dtypes(include=[np.number]).columns.tolist()
        
        if len(numeric_cols) >= 2:
            col1, col2 = st.columns(2)
            with col1:
                feat1 = st.selectbox("First feature", numeric_cols)
            with col2:
                feat2 = st.selectbox("Second feature", [c for c in numeric_cols if c != feat1])
            
            interaction_type = st.selectbox(
                "Interaction type",
                ["Multiplication", "Addition", "Subtraction", "Division", "Ratio"]
            )
            
            new_col_name = st.text_input("New column name", f"{feat1}_{interaction_type}_{feat2}")
            
            if st.button("Create Interaction Feature"):
                if interaction_type == "Multiplication":
                    processed_df[new_col_name] = processed_df[feat1] * processed_df[feat2]
                elif interaction_type == "Addition":
                    processed_df[new_col_name] = processed_df[feat1] + processed_df[feat2]
                elif interaction_type == "Subtraction":
                    processed_df[new_col_name] = processed_df[feat1] - processed_df[feat2]
                elif interaction_type == "Division":
                    processed_df[new_col_name] = processed_df[feat1] / (processed_df[feat2] + 1e-8)
                elif interaction_type == "Ratio":
                    processed_df[new_col_name] = processed_df[feat1] / (processed_df[feat2].sum() + 1e-8)
                
                st.success(f"βœ… Created feature: {new_col_name}")
        
        # Binning
        st.markdown("### Feature Binning")
        if numeric_cols:
            bin_col = st.selectbox("Select column for binning", numeric_cols)
            n_bins = st.slider("Number of bins", 2, 20, 5)
            bin_labels = [f"Bin_{i}" for i in range(n_bins)]
            
            if st.button("Create Binned Feature"):
                processed_df[bin_col + '_binned'] = pd.cut(processed_df[bin_col], 
                                                          bins=n_bins, 
                                                          labels=bin_labels)
                st.success(f"βœ… Created binned feature: {bin_col}_binned")
        
        st.markdown('</div>', unsafe_allow_html=True)
        
        # Update session state
        st.session_state.data = processed_df
    
    # Preview processed data
    st.markdown("---")
    st.subheader("πŸ“‹ Processed Data Preview")
    
    data_to_show = st.session_state.data
    
    col1, col2, col3 = st.columns(3)
    with col1:
        st.metric("Final Rows", data_to_show.shape[0])
    with col2:
        st.metric("Final Columns", data_to_show.shape[1])
    with col3:
        final_missing = data_to_show.isnull().sum().sum()
        st.metric("Remaining Missing", final_missing)
    
    st.dataframe(data_to_show.head(10), use_container_width=True)
    
    # Download processed data
    csv = data_to_show.to_csv(index=False)
    st.download_button(
        label="πŸ“₯ Download Processed Data",
        data=csv,
        file_name="processed_data.csv",
        mime="text/csv",
        use_container_width=True
    )
    
    return data_to_show