-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathbank.py
78 lines (58 loc) · 2.32 KB
/
bank.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
# https://github.com/zhengzangw/Fed-SINGA/blob/main/src/client/data/bank.py
import pandas as pd
import numpy as np
import sys
from pandas.api.types import is_numeric_dtype
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
def encode(df):
res = pd.DataFrame()
for col in df.columns.values:
if not is_numeric_dtype(df[col]):
tmp = pd.get_dummies(df[col], prefix=col)
else:
tmp = df[col]
res = pd.concat([res, tmp], axis=1)
return res
def load(device_id):
fn_train = "data/bank_train_" + str(device_id) + ".csv"
fn_test = "data/bank_test_" + str(device_id) + ".csv"
train = pd.read_csv(fn_train, sep=',')
test = pd.read_csv(fn_test, sep=',')
train_x = train.drop(['y'], axis=1)
train_y = train['y']
val_x = test.drop(['y'], axis=1)
val_y = test['y']
train_x = np.array((train_x), dtype=np.float32)
val_x = np.array((val_x), dtype=np.float32)
train_y = np.array((train_y), dtype=np.int32)
val_y = np.array((val_y), dtype=np.int32)
train_x, val_x = normalize(train_x, val_x)
num_classes = 2
return train_x, train_y, val_x, val_y, num_classes
def normalize(X_train, X_test):
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
return X_train_scaled, X_test_scaled
def split(num):
filepath = "../data/bank-additional-full.csv"
df = pd.read_csv(filepath, sep=';')
df['y'] = (df['y'] == 'yes').astype(int)
data = encode(df)
data = shuffle(data)
train, test = train_test_split(data, test_size=0.2)
train.to_csv("data/bank_train_.csv", index=False)
test.to_csv("data/bank_test_.csv", index=False)
train_per_client = len(train) // num
test_per_client = len(test) // num
print("train_per_client:", train_per_client)
print("test_per_client:", test_per_client)
for i in range(num):
sub_train = train[i * train_per_client:(i + 1) * train_per_client]
sub_test = test[i * test_per_client:(i + 1) * test_per_client]
sub_train.to_csv("data/bank_train_" + str(i) + ".csv", index=False)
sub_test.to_csv("data/bank_test_" + str(i) + ".csv", index=False)
if __name__ == "__main__":
split(int(sys.argv[1]))