-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
projects/DAPIO/data-processing/preprocessing/data_cleaner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pandas as pd | ||
import numpy as np | ||
from sklearn.preprocessing import StandardScaler | ||
|
||
class DataCleaner: | ||
def __init__(self, data: pd.DataFrame): | ||
self.data = data | ||
|
||
def handle_missing_values(self) -> pd.DataFrame: | ||
self.data.fillna(self.data.mean(), inplace=True) | ||
return self.data | ||
|
||
def remove_outliers(self) -> pd.DataFrame: | ||
Q1 = self.data.quantile(0.25) | ||
Q3 = self.data.quantile(0.75) | ||
IQR = Q3 - Q1 | ||
self.data = self.data[~((self.data < (Q1 - 1.5 * IQR)) | (self.data > (Q3 + 1.5 * IQR)))] | ||
return self.data | ||
|
||
def scale_data(self) -> pd.DataFrame: | ||
scaler = StandardScaler() | ||
self.data[['feature1', 'feature2', 'feature3']] = scaler.fit_transform(self.data[['feature1', 'feature2', 'feature3']]) | ||
return self.data | ||
|
||
def preprocess_data(self) -> pd.DataFrame: | ||
self.data = self.handle_missing_values() | ||
self.data = self.remove_outliers() | ||
self.data = self.scale_data() | ||
return self.data |