MCPcopy Create free account
hub / github.com/Hrishikesh332/HCDS-EPD / detect_outliers

Function detect_outliers

utils.py:254–357  ·  view source on GitHub ↗
(df, method, threshold, contamination, analysis_type)

Source from the content-addressed store, hash-verified

252 }
253
254def detect_outliers(df, method, threshold, contamination, analysis_type):
255 if analysis_type == 'temporal':
256 monthly_data = df.groupby('YEAR_MONTH')['TOTAL_COST'].sum()
257 outlier_months = set()
258
259 if method == "IQR Method":
260 Q1 = monthly_data.quantile(0.25)
261 Q3 = monthly_data.quantile(0.75)
262 IQR = Q3 - Q1
263 if IQR > 0:
264 lower_bound = Q1 - threshold * IQR
265 upper_bound = Q3 + threshold * IQR
266 outlier_months = monthly_data[(monthly_data < lower_bound) | (monthly_data > upper_bound)].index
267
268 elif method == "Isolation Forest":
269 if len(monthly_data) > 1:
270 iso_forest = IsolationForest(contamination=contamination, random_state=42)
271 outliers = iso_forest.fit_predict(monthly_data.values.reshape(-1, 1))
272 outlier_months = monthly_data[outliers == -1].index
273 else:
274 methods_results = []
275
276 Q1 = monthly_data.quantile(0.25)
277 Q3 = monthly_data.quantile(0.75)
278 IQR = Q3 - Q1
279 if IQR > 0:
280 lower_bound = Q1 - threshold * IQR
281 upper_bound = Q3 + threshold * IQR
282 iqr_outliers = (monthly_data < lower_bound) | (monthly_data > upper_bound)
283 methods_results.append(iqr_outliers)
284
285 if monthly_data.std() > 0:
286 z_scores = np.abs((monthly_data - monthly_data.mean()) / monthly_data.std())
287 zscore_outliers = z_scores > threshold
288 methods_results.append(zscore_outliers)
289
290 if len(monthly_data) > 1:
291 iso_forest = IsolationForest(contamination=contamination, random_state=42)
292 iso_outliers = iso_forest.fit_predict(monthly_data.values.reshape(-1, 1)) == -1
293 methods_results.append(iso_outliers)
294
295 if methods_results:
296 combined_outliers = sum(methods_results) >= 2
297 outlier_months = monthly_data[combined_outliers].index
298 else:
299 outlier_months = []
300
301 return df[df['YEAR_MONTH'].isin(outlier_months)]
302
303 data = df['TOTAL_COST']
304
305 if method == "IQR Method":
306 Q1 = data.quantile(0.25)
307 Q3 = data.quantile(0.75)
308 IQR = Q3 - Q1
309 if IQR > 0:
310 lower_bound = Q1 - threshold * IQR
311 upper_bound = Q3 + threshold * IQR

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected