-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_preprocessing.py
158 lines (130 loc) · 5.46 KB
/
data_preprocessing.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
This script defines data reader
"""
import numpy as np
import pandas as pd
import numpy as np
import sklearn
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from sklearn.cluster import KMeans
from sklearn.compose import ColumnTransformer
from collections import Counter
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
def funcname(parameter_list):
pass
def data_reader(data_name = "adult"):
if(data_name == "adult"):
#load data
file_path = "./data/adult/"
data1 = pd.read_csv(file_path + 'adult.data', header=None)
data2 = pd.read_csv(file_path + 'adult.test', header=None)
data2 = data2.replace(' <=50K.', ' <=50K')
data2 = data2.replace(' >50K.', ' >50K')
data = pd.concat([data1,data2])
#data transform: str->int
data = np.array(data, dtype=str)
labels = data[:,14]
le= LabelEncoder()
le.fit(labels)
labels = le.transform(labels)
data = data[:,:-1]
categorical_features = [1,3,5,6,7,8,9,13]
# categorical_names = {}
for feature in categorical_features:
le = LabelEncoder()
le.fit(data[:, feature])
data[:, feature] = le.transform(data[:, feature])
# categorical_names[feature] = le.classes_
data = data.astype(float)
n_features = data.shape[1]
numerical_features = list(set(range(n_features)).difference(set(categorical_features)))
for feature in numerical_features:
scaler = MinMaxScaler()
sacled_data = scaler.fit_transform(data[:,feature].reshape(-1,1))
data[:,feature] = sacled_data.reshape(-1)
#OneHotLabel
oh_encoder = ColumnTransformer(
[('oh_enc', OneHotEncoder(sparse=False), categorical_features),],
remainder='passthrough' )
oh_data = oh_encoder.fit_transform(data)
elif(data_name == "bank"):
#load data
file_path = "./data/bank/"
data = pd.read_csv(file_path + 'bank-full.csv',sep=';')
#data transform
data = np.array(data, dtype=str)
labels = data[:,-1]
le= LabelEncoder()
le.fit(labels)
labels = le.transform(labels)
data = data[:,:-1]
categorical_features = [1,2,3,4,6,7,8,10,15]
# categorical_names = {}
for feature in categorical_features:
le = LabelEncoder()
le.fit(data[:, feature])
data[:, feature] = le.transform(data[:, feature])
# categorical_names[feature] = le.classes_
data = data.astype(float)
n_features = data.shape[1]
numerical_features = list(set(range(n_features)).difference(set(categorical_features)))
for feature in numerical_features:
scaler = MinMaxScaler()
sacled_data = scaler.fit_transform(data[:,feature].reshape(-1,1))
data[:,feature] = sacled_data.reshape(-1)
#OneHotLabel
oh_encoder = ColumnTransformer(
[('oh_enc', OneHotEncoder(sparse=False), categorical_features),],
remainder='passthrough' )
oh_data = oh_encoder.fit_transform(data)
elif(data_name == "mnist"):
file_path = "./data/mnist/"
data = pd.read_csv(file_path + 'mnist_train.csv', header=None)
data = np.array(data)
labels = data[:,0]
data = data[:,1:]
categorical_features = []
data = data/data.max()
oh_encoder = ColumnTransformer(
[('oh_enc', OneHotEncoder(sparse=False), categorical_features),],
remainder='passthrough' )
oh_data = oh_encoder.fit_transform(data)
else:
str_list = data_name.split('_')
file_path = "./data/purchase/"
data = pd.read_csv(file_path+'dataset_purchase')
data = np.array(data)
data = data[:,1:]
label_file = './data/purchase/label'+ str_list[1] + '.npy'
labels = np.load(label_file)
categorical_features = []
oh_encoder = ColumnTransformer(
[('oh_enc', OneHotEncoder(sparse=False), categorical_features),],
remainder='passthrough' )
oh_data = oh_encoder.fit_transform(data)
X_train, _, y_train, _ = train_test_split(oh_data, labels,test_size = 0.75)
oh_data = X_train
labels = y_train
#randomly select 10000 records as training data
train_idx = np.random.choice(len(labels), 10000, replace = False)
idx = range(len(labels))
idx = np.array(idx)
test_idx = list(set(idx).difference(set(train_idx)))
test_idx = np.array(test_idx)
assert test_idx.sum() + train_idx.sum() == idx.sum()
X_train = data[train_idx,:]
Y_train = labels[train_idx]
X_test = data[test_idx,:]
Y_test = labels[test_idx]
orig_dataset = {"X_train":X_train,
"Y_train":Y_train,
"X_test":X_test,
"Y_test":Y_test}
X_train = oh_data[train_idx,:]
X_test = oh_data[test_idx,:]
oh_dataset = {"X_train":X_train,
"Y_train":Y_train,
"X_test":X_test,
"Y_test":Y_test}
return orig_dataset, oh_dataset, oh_encoder