-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_default_data.py
More file actions
133 lines (104 loc) · 5.62 KB
/
load_default_data.py
File metadata and controls
133 lines (104 loc) · 5.62 KB
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
import torch
import pandas as pd
import numpy as np
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.neighbors import NearestNeighbors
def load_default(url):
data = pd.read_csv(url)
#data = shuffle(data)
# Encode categorical columns
categorical_columns = ['LIMIT_BAL','SEX','EDUCATION','MARRIAGE','PAY_0','PAY_2','PAY_3','PAY_4','PAY_5','PAY_6']
for col in categorical_columns:
encoder = LabelEncoder()
data[col] = encoder.fit_transform(data[col])
# Normalize numerical columns
numerical_columns = ['AGE','BILL_AMT1','BILL_AMT2','BILL_AMT3','BILL_AMT4','BILL_AMT5','BILL_AMT6','PAY_AMT1','PAY_AMT2','PAY_AMT3','PAY_AMT4','PAY_AMT5','PAY_AMT6']
scaler = StandardScaler()
data[numerical_columns] = scaler.fit_transform(data[numerical_columns])
# Split the data into features and labels
X = data.drop('y', axis=1)
y = LabelEncoder().fit_transform(data['y'])
#print("bismillah")
return X, y
def load_default_age(url, sensitive_feature):
data = pd.read_csv(url)
#data = shuffle(data)
# Encode categorical columns
categorical_columns = ['LIMIT_BAL','SEX','EDUCATION','MARRIAGE','PAY_0','PAY_2','PAY_3','PAY_4','PAY_5','PAY_6']
for col in categorical_columns:
encoder = LabelEncoder()
data[col] = encoder.fit_transform(data[col])
# Normalize numerical columns
numerical_columns = ['BILL_AMT1','BILL_AMT2','BILL_AMT3','BILL_AMT4','BILL_AMT5','BILL_AMT6','PAY_AMT1','PAY_AMT2','PAY_AMT3','PAY_AMT4','PAY_AMT5','PAY_AMT6']
scaler = StandardScaler()
data[numerical_columns] = scaler.fit_transform(data[numerical_columns])
data = shuffle(data)
#train_df, test_df = train_test_split(data, test_size=0.2, random_state=42)
#data = train_df
mask = (data['AGE'] >=0) & (data['AGE'] <=29)
df1 = data[mask]
mask = (data['AGE'] >=30) & (data['AGE'] <=39)
df2 = data[mask]
mask = (data['AGE'] >=40)
df3 = data[mask]
df1 = df1.dropna()
df2 = df2.dropna()
df3 = df3.dropna()
#scalrising the 'age' column as well
numerical_columns = ['AGE']
scaler = StandardScaler()
df1[numerical_columns] = scaler.fit_transform(df1[numerical_columns])
df2[numerical_columns] = scaler.fit_transform(df2[numerical_columns])
df3[numerical_columns] = scaler.fit_transform(df3[numerical_columns])
df1, test_df1 = train_test_split(df1, test_size=0.1, random_state=42)
df2, test_df2 = train_test_split(df2, test_size=0.1, random_state=42)
df3, test_df3 = train_test_split(df3, test_size=0.1, random_state=42)
# Split the data into features and labels
test_df = result = pd.concat([test_df1, test_df2, test_df3], ignore_index=True)
test_df[numerical_columns] = scaler.fit_transform(test_df[numerical_columns])
X_client1 = df1.drop('y', axis=1)
y_client1 = LabelEncoder().fit_transform(df1['y'])
X_client2 = df2.drop('y', axis=1)
y_client2 = LabelEncoder().fit_transform(df2['y'])
X_client3 = df3.drop('y', axis=1)
y_client3 = LabelEncoder().fit_transform(df3['y'])
s_client1 = X_client1[sensitive_feature]
#y_potential_client1 = find_potential_outcomes(X_client1,y_client1, sensitive_feature)
y_potential_client1 =y_client1
X_client1 = torch.tensor(X_client1.values, dtype=torch.float32)
y_client1 = torch.tensor(y_client1, dtype=torch.float32)
y_potential_client1 =y_client1
s_client1 = torch.from_numpy(s_client1.values).float()
y_potential_client1 = torch.tensor(y_potential_client1, dtype=torch.float32)
s_client2 = X_client2[sensitive_feature]
#y_potential_client2 = find_potential_outcomes(X_client2,y_client2, sensitive_feature)
y_potential_client2 =y_client2
X_client2 = torch.tensor(X_client2.values, dtype=torch.float32)
y_client2 = torch.tensor(y_client2, dtype=torch.float32)
s_client2 = torch.from_numpy(s_client2.values).float()
y_potential_client2 = torch.tensor(y_potential_client2, dtype=torch.float32)
s_client3 = X_client3[sensitive_feature]
#y_potential_client3 = find_potential_outcomes(X_client3,y_client3, sensitive_feature)
y_potential_client3 =y_client3
X_client3 = torch.tensor(X_client3.values, dtype=torch.float32)
y_client3 = torch.tensor(y_client3, dtype=torch.float32)
s_client3 = torch.from_numpy(s_client3.values).float()
y_potential_client3 = torch.tensor(y_potential_client3, dtype=torch.float32)
X_test = test_df.drop('y', axis=1)
y_test = LabelEncoder().fit_transform(test_df['y'])
sex_column = X_test[sensitive_feature]
column_names_list = X_test.columns.tolist()
#ytest_potential = find_potential_outcomes(X_test,y_test, sensitive_feature)
ytest_potential =y_test
ytest_potential = torch.tensor(ytest_potential, dtype=torch.float32)
X_test = torch.tensor(X_test.values, dtype=torch.float32)
y_test = torch.tensor(y_test, dtype=torch.float32)
sex_list = sex_column.tolist()
data_dict = {}
data_dict["client_1"] = {"X": X_client1, "y": y_client1, "s": s_client1, "y_pot": y_potential_client1}
data_dict["client_2"] = {"X": X_client2, "y": y_client2, "s": s_client2, "y_pot": y_potential_client2}
data_dict["client_3"] = {"X": X_client3, "y": y_client3, "s": s_client3, "y_pot": y_potential_client3}
#print("bismillah")
return data_dict, X_test, y_test, sex_list, column_names_list,ytest_potential