forked from archanags001/Insightful-Data-Explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutlierHandling.py
292 lines (227 loc) · 11 KB
/
outlierHandling.py
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
import streamlit as st
import numpy as np
from scipy.stats import zscore, iqr,boxcox
from sklearn.covariance import EllipticEnvelope
from statsmodels.robust import mad
from sklearn.cluster import DBSCAN
from sklearn.neighbors import LocalOutlierFactor
from sklearn.ensemble import IsolationForest
from streamlit_option_menu import option_menu
from scipy.stats.mstats import winsorize
def zscore_outliers(data):
try:
z_scores = np.abs(zscore(data))
return np.where(z_scores > 3)[0]
except ValueError as e:
msg = "An error occurred while applying zscore method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
def iqr_outliers(data):
try:
q1, q3 = np.percentile(data, [25, 75])
iqr_val = q3 - q1
lower_bound, upper_bound = q1 - 1.5 * iqr_val, q3 + 1.5 * iqr_val
return np.where((data < lower_bound) | (data > upper_bound))[0]
except ValueError as e:
msg = "An error occurred while applying IQR method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
def mahalanobis_outliers(data):
try:
clf = EllipticEnvelope(contamination=0.1)
outliers = clf.fit_predict(data)
return np.where(outliers == -1)[0]
except ValueError as e:
msg = "An error occurred while applying Mahalanobis method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
def mad_outliers(data):
try:
median = np.median(data)
mad_value = mad(data)
threshold = 3 * mad_value
return np.where(np.abs(data - median) > threshold)[0]
except ValueError as e:
msg = "An error occurred while applying MAD method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
def knn_outliers(data):
try :
clf = LocalOutlierFactor(n_neighbors=20, contamination=0.1)
outliers = clf.fit_predict(data)
return np.where(outliers == -1)[0]
except ValueError as e:
msg = "An error occurred while applying KNN method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
def isolation_forest_outliers(data):
try:
clf = IsolationForest(contamination=0.1)
outliers = clf.fit_predict(data)
return np.where(outliers == -1)[0]
except ValueError as e:
msg = "An error occurred while applying Isolation_forest method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
def dbscan_outliers(data):
try:
dbscan = DBSCAN(eps=0.5, min_samples=5)
data['cluster'] = dbscan.fit_predict(data.values)
return np.where(data['cluster'] == -1)[0]
except ValueError as e:
msg = "An error occurred while applying DBSCAN method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
def outlier_detection():
st.title("Outlier Detection")
if st.session_state.df is not None:
cols = st.columns(2)
outlier_method = cols[1].selectbox(
"Select Outlier Detection Method",
["Z-Score", "IQR", "Mahalanobis", "MAD", "DBSCAN", "KNN", "Isolation Forest"]
)
column_selector = cols[0].multiselect(
"Select Column for Outlier Detection",
st.session_state.df.select_dtypes(include=np.number).columns
)
st.session_state.column_selector = column_selector
# Check if at least one column is selected
if not column_selector:
st.info("Please ensure that at least one column is selected")
return
outliers = None
error_msg = None
if outlier_method == "Z-Score":
outliers = zscore_outliers(st.session_state.df[column_selector])
elif outlier_method == "IQR":
outliers = iqr_outliers(st.session_state.df[column_selector])
elif outlier_method == "Mahalanobis":
outliers = mahalanobis_outliers(st.session_state.df[column_selector])
elif outlier_method == "MAD":
outliers = mad_outliers(st.session_state.df[column_selector])
elif outlier_method == "DBSCAN":
outliers = dbscan_outliers(st.session_state.df[column_selector])
elif outlier_method == "KNN":
outliers = knn_outliers(st.session_state.df[column_selector])
elif outlier_method == "Isolation Forest":
outliers = isolation_forest_outliers(st.session_state.df[column_selector])
st.session_state.outliers =outliers
# Display outliers with highlighted columns
st.subheader(f"Outliers using {outlier_method} for {column_selector}:")
try:
st.write("Number of outliers: ", len(st.session_state.outliers))
st.dataframe(st.session_state.df.loc[outliers].style.set_properties(**{'background-color': '#F4CABC'},
subset=column_selector))
st.subheader("Lightcoral color highlights data points with outliers.")
st.dataframe(
st.session_state.df.style.applymap(
lambda _: "background-color: lightcoral;", subset=(outliers, slice(None))
)
)
except Exception as e:
msg = "An error occurred while applying "+ outlier_method + " method,\n ERROR: "+str(e)
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
except ValueError :
msg = "An error occurred while applying "+ outlier_method + " method,\n ERROR: "
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
except TypeError :
msg = "An error occurred while applying "+ outlier_method + " method,\n ERROR: "
st.error(msg)
st.info("Please check your data or try a different outlier detection method.")
except:
st.error("Unexpected error:")
def handle_outliers_tech(data, method, column_selector, outliers,min_max_list,bounds,imputation_value):
if method == "Log Transformation":
for column in column_selector:
data[column], _ = boxcox(data[column])
elif method == "Box-Cox Transformation":
for column in column_selector:
data[column], fitted_lambda = boxcox(data[column])
st.write(f"Lambda value used for {column} Transformation: ", fitted_lambda)
elif method == "Truncation or Capping":
i = 0
for column in column_selector:
data[column] = np.clip(data[column], a_min=min_max_list[i][0], a_max=min_max_list[i][1])
i+=1
elif method == "Winsorizing":
i = 0
for column in column_selector:
data[column] = winsorize(data[column], limits=(bounds[i][0], bounds[i][1]))
i+=1
elif method == "Imputation":
i =0
for col in column_selector:
data.loc[outliers, col] = np.nan
data[col] = data[col].fillna(imputation_value[i])
i+=1
elif method == "Remove Outliers":
data = data.drop(outliers, axis=0).reset_index(drop=True)
return data
def outlier_handling():
st.title("Outlier Handling")
if st.session_state.df is not None:
cols = st.columns(2)
handle_option = cols[0].selectbox("Select Outlier Handling Technique",
["None", "Log Transformation", "Box-Cox Transformation",
"Truncation or Capping", "Winsorizing", "Imputation", "Remove Outliers"])
if handle_option is None:
st.warning("Please ensure that at least one technique is selected")
if handle_option != "Remove Outliers":
column_selector_handle = cols[1].multiselect("Select Column to handle Outlier", st.session_state.column_selector)
else:
column_selector_handle = st.session_state.column_selector
imputation_value =[]
bounds = []
min_max_list = []
if handle_option =="Imputation":
for col in column_selector_handle:
msg = col+": Enter imputation value:"
imputation_value.append(st.number_input(msg, min_value=-1.7976931348623157e+308,
max_value=1.7976931348623157e+308))
elif handle_option== "Truncation or Capping":
for column in column_selector_handle:
min_max = []
min_max.append(st.number_input(f"Enter min value for {column}:", min_value=0.0))
min_max.append(st.number_input(f"Enter max valuefor {column}:", min_value=0.0))
min_max_list.append(min_max)
elif handle_option == "Winsorizing":
for column in column_selector_handle:
lower_upper = []
lower_upper.append(st.number_input(f"Enter lower bound value for {column}:", min_value=0.0, max_value=1.0,
key=f"Winsorizing_lower_{column}"))
lower_upper.append(st.number_input(f"Enter upper bound value for {column}:", min_value=0.0, max_value=1.0,
key=f"Winsorizing_upper_{column}"))
bounds.append(lower_upper)
if handle_option != "None":
if st.button("Handle Outlier"):
try:
st.session_state.df = handle_outliers_tech(st.session_state.df, handle_option,
column_selector_handle, st.session_state.outliers,
min_max_list,bounds,imputation_value)
outlierfix = list(set(st.session_state.outliers).intersection(st.session_state.df.index))
st.dataframe(
st.session_state.df.style.applymap(
lambda _: "background-color: green;", subset=(outlierfix, slice(None))
)
)
except Exception as e:
st.error(str(e))
def outlier_detection_handling():
if 'page' not in st.session_state:
st.session_state.page = "Home"
if 'df' not in st.session_state:
st.session_state.df = None
# Define page navigation
pages = ["Outlier Detection", "Handling Outliers"]
# st.session_state.page = st.sidebar.radio("Select a page", pages, index=pages.index(st.session_state.page))
nav_tab_op = option_menu(
menu_title="",
options=pages,
orientation='horizontal',
)
if nav_tab_op == "Outlier Detection":
outlier_detection()
elif nav_tab_op == "Handling Outliers":
outlier_handling()