-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_under_sampling.py
69 lines (51 loc) · 2.26 KB
/
random_under_sampling.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
from imblearn.under_sampling import RandomUnderSampler
import pandas as pd
def generate_csv(dataframe, filepath):
print("Generating csv file: " + filepath + "\n")
dataframe.to_csv(filepath, index=False)
def perform_random_under_sampling(df, out_path, sampling_strategy='auto'):
"""
Perform random under-sampling on the given dataset.
Parameters:
- df: pd.DataFrame - The entire dataset including features and target.
- target_column: str - The name of the target column.
- sampling_strategy: str or dict (default='auto') - The sampling strategy to use for under-sampling.
- random_state: int or None (default=None) - Random state for reproducibility.
Returns:
- df_resampled: pd.DataFrame - The resampled DataFrame including both features and the target column.
"""
target_column = 'repres30days'
# Separate features (X) and target (y)
X = df.drop(columns=[target_column])
y = df[target_column]
print(f"[{out_path}]")
print(f"Training set size before applying random under-sampling: {X.shape}\n")
print("Class distribution in training set before random under-sampling:")
print(y.value_counts())
# Initialize the RandomUnderSampler
rus = RandomUnderSampler(sampling_strategy=sampling_strategy, random_state=42)
# Perform the random under-sampling
X_resampled, y_resampled = rus.fit_resample(X, y)
# Convert the resampled data back to pandas DataFrame and Series
X_resampled = pd.DataFrame(X_resampled, columns=X.columns)
y_resampled = pd.Series(y_resampled, name=target_column)
print("\nClass distribution in trainning set after random under-sampling:")
print(y_resampled.value_counts())
# Combine the resampled features and target into one DataFrame
df_resampled = pd.concat([X_resampled, y_resampled], axis=1)
generate_csv(df_resampled, out_path + 'train_resampled.csv')
return df_resampled
def main():
path = 'TrainTestData/NO_FS/'
df = pd.read_csv(path + "train.csv")
perform_random_under_sampling(df, path)
path = 'TrainTestData/CFS/'
df = pd.read_csv(path + "train.csv")
perform_random_under_sampling(df, path)
path = 'TrainTestData/InfoGain/'
df = pd.read_csv(path + "train.csv")
perform_random_under_sampling(df, path)
path = 'TrainTestData/Manual_FS/'
df = pd.read_csv(path + "train.csv")
perform_random_under_sampling(df, path)
main()