-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_preprocessing.py
218 lines (166 loc) · 7.11 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
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 data_reader(data_name = "adult"):
if(data_name == "adult"):
#load data
file_path = "./dataset/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), 30000, 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
def fn_Feature_Range_Counter(dataset):
'''
Function: counting the value range for each feature of the dataset
'''
data = np.vstack([dataset['X_train'], dataset['X_test']])
feature_num = data.shape[1]
feature_range_dict = {}
for ii in range(feature_num):
cnt = Counter(data[:,ii])
values = list(cnt.keys())
values = np.array(values)
feature_range_dict[ii] = values
# labels = np.hstack([dataset['Y_train'], dataset['Y_test']])
# n_class = len(Counter(labels))
return feature_range_dict
if(__name__ == '__main__'):
file_path = "./data/purchase/"
data = pd.read_csv(file_path+'dataset_purchase')
data = np.array(data)
label_100 = data[:,0]
data = data[:,1:]
label_2 = KMeans(n_clusters=2, random_state=9).fit_predict(data)
np.save("./data/purchase/label2", label_2)
print("label_2 finished!")
label_10 = KMeans(n_clusters=10, random_state=9).fit_predict(data)
np.save("./data/purchase/label10", label_10)
print("label_10 finished!")
label_20 = KMeans(n_clusters=20, random_state=9).fit_predict(data)
np.save("./data/purchase/label20", label_20)
print("label_20 finished!")
label_50 = KMeans(n_clusters=50, random_state=9).fit_predict(data)
np.save("./data/purchase/label50", label_50)
print("label_50 finished!")
np.save("./data/purchase/label100", label_100)
print("label_100 finished!")
#OneHotLabel
# ct = ColumnTransformer(
# [('oh_enc', OneHotEncoder(sparse=False), categorical_features),],
# remainder='passthrough' )
# ct.fit(data)