Update Preprocessing2.py
Browse files- Preprocessing2.py +212 -217
Preprocessing2.py
CHANGED
|
@@ -1,217 +1,212 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import io
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
from sklearn.preprocessing import LabelEncoder
|
| 7 |
-
import seaborn as sns
|
| 8 |
-
import base64
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def handle_categorical_values():
|
| 12 |
-
if "data" in st.session_state:
|
| 13 |
-
data = st.session_state["data"]
|
| 14 |
-
|
| 15 |
-
st.subheader("Handle Categorical Values")
|
| 16 |
-
|
| 17 |
-
categorical_cols_features = list(
|
| 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 |
-
fill_method =
|
| 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 |
-
elif action == "
|
| 119 |
-
modified_data.drop_duplicates(
|
| 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 |
-
replace_method =
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
st.write("### After:")
|
| 214 |
-
st.dataframe(modified_data)
|
| 215 |
-
|
| 216 |
-
else:
|
| 217 |
-
st.warning("Please upload a dataset first.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from sklearn.preprocessing import LabelEncoder
|
| 7 |
+
import seaborn as sns
|
| 8 |
+
import base64
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def handle_categorical_values():
|
| 12 |
+
if "data" in st.session_state:
|
| 13 |
+
data = st.session_state["data"]
|
| 14 |
+
|
| 15 |
+
st.subheader("Handle Categorical Values")
|
| 16 |
+
|
| 17 |
+
categorical_cols_features = list(data.select_dtypes(include="object").columns)
|
| 18 |
+
|
| 19 |
+
one_hot_enc = st.multiselect("Select nominal categorical columns", categorical_cols_features)
|
| 20 |
+
|
| 21 |
+
if one_hot_enc:
|
| 22 |
+
for column in one_hot_enc:
|
| 23 |
+
if data[column].dtype == 'object':
|
| 24 |
+
data = pd.get_dummies(data, columns=[column])
|
| 25 |
+
st.session_state["data"] = data
|
| 26 |
+
st.write("### Data after One-Hot Encoding:")
|
| 27 |
+
st.write(data.head())
|
| 28 |
+
|
| 29 |
+
label_encoder = LabelEncoder()
|
| 30 |
+
label_enc = st.multiselect("Select ordinal categorical columns", categorical_cols_features)
|
| 31 |
+
|
| 32 |
+
if label_enc:
|
| 33 |
+
for column in label_enc:
|
| 34 |
+
if data[column].dtype == 'object':
|
| 35 |
+
data[column] = label_encoder.fit_transform(data[column])
|
| 36 |
+
st.session_state["data"] = data
|
| 37 |
+
st.write("### Data after Label Encoding:")
|
| 38 |
+
st.write(data.head())
|
| 39 |
+
|
| 40 |
+
else:
|
| 41 |
+
st.warning("Please upload a dataset to handle categorical values.")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def missing_values():
|
| 45 |
+
st.title("Handle Missing Values")
|
| 46 |
+
|
| 47 |
+
if "data" in st.session_state:
|
| 48 |
+
data = st.session_state["data"].copy()
|
| 49 |
+
|
| 50 |
+
action = st.selectbox(
|
| 51 |
+
"Select Action", ["Drop", "Dropna", "Fill missing val"])
|
| 52 |
+
|
| 53 |
+
column = st.selectbox("Select Column", data.columns)
|
| 54 |
+
|
| 55 |
+
st.write("### Before:")
|
| 56 |
+
st.dataframe(data)
|
| 57 |
+
|
| 58 |
+
modified_data = data.copy()
|
| 59 |
+
|
| 60 |
+
if action == "Drop":
|
| 61 |
+
modified_data.drop(columns=[column], inplace=True)
|
| 62 |
+
elif action == "Dropna":
|
| 63 |
+
modified_data.dropna(subset=[column], inplace=True)
|
| 64 |
+
elif action == "Fill missing val":
|
| 65 |
+
fill_method = st.selectbox(
|
| 66 |
+
"Select fill method", ["Mean", "Mode", "Median"])
|
| 67 |
+
|
| 68 |
+
if fill_method == "Mean":
|
| 69 |
+
fill_value = data[column].mean()
|
| 70 |
+
elif fill_method == "Mode":
|
| 71 |
+
fill_value = data[column].mode()[0]
|
| 72 |
+
elif fill_method == "Median":
|
| 73 |
+
fill_value = data[column].median()
|
| 74 |
+
|
| 75 |
+
modified_data[column].fillna(fill_value, inplace=True)
|
| 76 |
+
|
| 77 |
+
st.write("### After (Preview):")
|
| 78 |
+
st.dataframe(modified_data)
|
| 79 |
+
|
| 80 |
+
if st.button("OK"):
|
| 81 |
+
st.session_state["data"] = modified_data
|
| 82 |
+
st.success("Done! The action has been applied.")
|
| 83 |
+
st.write("### After:")
|
| 84 |
+
st.dataframe(modified_data)
|
| 85 |
+
|
| 86 |
+
else:
|
| 87 |
+
st.warning("Please upload a dataset first.")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def handle_duplicates():
|
| 91 |
+
st.title("Handle Duplicates")
|
| 92 |
+
|
| 93 |
+
if "data" in st.session_state:
|
| 94 |
+
data = st.session_state["data"].copy()
|
| 95 |
+
|
| 96 |
+
action = st.selectbox(
|
| 97 |
+
"Select Action", ["Drop Duplicates", "Drop Duplicates in Column", "Keep First", "Keep Last"])
|
| 98 |
+
|
| 99 |
+
if action in ["Drop Duplicates in Column", "Keep First", "Keep Last"]:
|
| 100 |
+
column = st.selectbox("Select Column", data.columns)
|
| 101 |
+
else:
|
| 102 |
+
column = None
|
| 103 |
+
|
| 104 |
+
st.write("### Before:")
|
| 105 |
+
st.dataframe(data)
|
| 106 |
+
|
| 107 |
+
after_placeholder = st.empty()
|
| 108 |
+
|
| 109 |
+
modified_data = data.copy()
|
| 110 |
+
|
| 111 |
+
if action == "Drop Duplicates":
|
| 112 |
+
modified_data.drop_duplicates(inplace=True)
|
| 113 |
+
elif action == "Drop Duplicates in Column":
|
| 114 |
+
modified_data.drop_duplicates(subset=[column], inplace=True)
|
| 115 |
+
elif action == "Keep First":
|
| 116 |
+
modified_data.drop_duplicates(
|
| 117 |
+
subset=[column], keep="first", inplace=True)
|
| 118 |
+
elif action == "Keep Last":
|
| 119 |
+
modified_data.drop_duplicates(
|
| 120 |
+
subset=[column], keep="last", inplace=True)
|
| 121 |
+
|
| 122 |
+
st.write("### After (Preview):")
|
| 123 |
+
st.dataframe(modified_data)
|
| 124 |
+
|
| 125 |
+
if st.button("OK"):
|
| 126 |
+
st.session_state["data"] = modified_data
|
| 127 |
+
st.success("Done! The action has been applied.")
|
| 128 |
+
st.write("### After:")
|
| 129 |
+
st.dataframe(modified_data)
|
| 130 |
+
|
| 131 |
+
else:
|
| 132 |
+
st.warning("Please upload a dataset first.")
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def handle_outliers():
|
| 136 |
+
st.title("Handle Outliers")
|
| 137 |
+
|
| 138 |
+
if "data" in st.session_state:
|
| 139 |
+
data = st.session_state["data"].copy()
|
| 140 |
+
|
| 141 |
+
column = st.selectbox("Select Column", data.select_dtypes(
|
| 142 |
+
include=[np.number]).columns)
|
| 143 |
+
|
| 144 |
+
action = st.selectbox(
|
| 145 |
+
"Select Action",
|
| 146 |
+
["Remove Outliers (IQR)", "Set Bounds Manually",
|
| 147 |
+
"Replace Outliers"]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
st.write("### Before:")
|
| 151 |
+
st.dataframe(data)
|
| 152 |
+
|
| 153 |
+
after_placeholder = st.empty()
|
| 154 |
+
|
| 155 |
+
modified_data = data.copy()
|
| 156 |
+
|
| 157 |
+
if action == "Remove Outliers (IQR)":
|
| 158 |
+
Q1 = data[column].quantile(0.25)
|
| 159 |
+
Q3 = data[column].quantile(0.75)
|
| 160 |
+
IQR = Q3 - Q1
|
| 161 |
+
lower_bound = Q1 - 1.5 * IQR
|
| 162 |
+
upper_bound = Q3 + 1.5 * IQR
|
| 163 |
+
|
| 164 |
+
# Remove outliers
|
| 165 |
+
modified_data = modified_data[(
|
| 166 |
+
modified_data[column] >= lower_bound) & (modified_data[column] <= upper_bound)]
|
| 167 |
+
|
| 168 |
+
elif action == "Set Bounds Manually":
|
| 169 |
+
# User inputs for bounds
|
| 170 |
+
lower_bound = st.number_input(
|
| 171 |
+
f"Set lower bound for {column}", value=float(data[column].min()))
|
| 172 |
+
upper_bound = st.number_input(
|
| 173 |
+
f"Set upper bound for {column}", value=float(data[column].max()))
|
| 174 |
+
|
| 175 |
+
modified_data = modified_data[(
|
| 176 |
+
modified_data[column] >= lower_bound) & (modified_data[column] <= upper_bound)]
|
| 177 |
+
|
| 178 |
+
elif action == "Replace Outliers":
|
| 179 |
+
|
| 180 |
+
Q1 = data[column].quantile(0.25)
|
| 181 |
+
Q3 = data[column].quantile(0.75)
|
| 182 |
+
IQR = Q3 - Q1
|
| 183 |
+
lower_bound = Q1 - 1.5 * IQR
|
| 184 |
+
upper_bound = Q3 + 1.5 * IQR
|
| 185 |
+
|
| 186 |
+
replace_method = st.radio(
|
| 187 |
+
"Select Replacement Method",
|
| 188 |
+
["Mean", "Median"]
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
if replace_method == "Mean":
|
| 192 |
+
replacement_value = data[column].mean()
|
| 193 |
+
else:
|
| 194 |
+
replacement_value = data[column].median()
|
| 195 |
+
|
| 196 |
+
# Replace outliers
|
| 197 |
+
modified_data[column] = modified_data[column].apply(
|
| 198 |
+
lambda x: replacement_value if x < lower_bound or x > upper_bound else x
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
# After Visualization
|
| 202 |
+
st.write("### After (Preview):")
|
| 203 |
+
st.dataframe(modified_data)
|
| 204 |
+
|
| 205 |
+
if st.button("OK"):
|
| 206 |
+
st.session_state["data"] = modified_data
|
| 207 |
+
st.success("Done! The action has been applied.")
|
| 208 |
+
st.write("### After:")
|
| 209 |
+
st.dataframe(modified_data)
|
| 210 |
+
|
| 211 |
+
else:
|
| 212 |
+
st.warning("Please upload a dataset first.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|