-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_large_datasets.py
155 lines (126 loc) · 4.36 KB
/
prepare_large_datasets.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
import glob
import numpy as np
import os
import pandas as pd
from keras.datasets import cifar10
from sklearn.datasets import (
fetch_20newsgroups_vectorized,
fetch_kddcup99,
fetch_lfw_people,
fetch_olivetti_faces,
fetch_rcv1,
)
from sklearn.preprocessing import LabelEncoder, MinMaxScaler, RobustScaler
# load the datasets
# for f in list(glob.glob("../datasets/uci/*")):
for f in list(glob.glob("../datasets/large_datasets/emnist/emnist-byclass*.csv")):
print(f)
parsing_args = parsing_dict["emnist"]
df = pd.read_csv(
f, sep=parsing_args[0], header=parsing_args[1], index_col=parsing_args[2]
)
if os.path.basename(f) == "PLANNING_plrx.txt":
del df[13]
if os.path.basename(f) == "BREAST.csv":
del df["Unnamed: 32"]
df = df.rename(columns={parsing_args[3]: "LABEL"})
for column, dtype in df.dtypes.items():
if column == "LABEL":
continue
if dtype not in ["int64", "float64"]:
if len(df[column].unique()) > 2:
# print(pd.get_dummies(df[column], prefix=column))
df = pd.concat(
[
pd.get_dummies(df[column], prefix=column),
df.drop(column, axis=1),
],
axis=1,
)
else:
df.loc[:, column] = df.loc[:, column].astype("category").cat.codes
# print(df)
labelEncoder = LabelEncoder()
# df["LABEL"] = labelEncoder.fit_transform(df.LABEL.values)
df = df.dropna()
print(df)
# rf = RandomForestClassifier()
# rf.fit(df.drop("LABEL", axis=1), labelEncoder.fit_transform(df["LABEL"].values))
# print(os.path.basename(f))
df.to_csv(
"../datasets/large_cleaned/" + os.path.basename(f).split(".")[0] + ".csv",
index=False,
)
# code for cifar etc.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# print shape of each variable
print("train image shape:", x_train.shape) # output (50000, 32, 32, 3)
print("train label shape:", y_train.shape) # output (50000, 1)
print("test image shape:", x_test.shape) # output (10000, 32, 32, 3)
print("test label shape:", y_test.shape) # output (10000, 1)
x_test = np.reshape(x_test, (10000, 3072))
df = pd.DataFrame(x_test)
df["LABEL"] = y_test.flatten()
print(df)
labelEncoder = LabelEncoder()
df["LABEL"] = labelEncoder.fit_transform(df.LABEL.values)
feature_columns = df.columns.to_list()
feature_columns.remove("LABEL")
# feature normalization
scaler = RobustScaler()
df[feature_columns] = scaler.fit_transform(df[feature_columns])
# scale back to [0,1]
scaler = MinMaxScaler()
df[feature_columns] = scaler.fit_transform(df[feature_columns])
print(df)
df.to_csv("cifar10.csv", index=False)
exit(-1)
# code for olivetti
for fetch_func in [
# fetch_olivetti_faces,
# fetch_20newsgroups_vectorized,
# fetch_lfw_people,
fetch_rcv1,
# fetch_kddcup99,
]:
print(fetch_func.__name__)
data = fetch_func()
df = pd.DataFrame(data.data)
df["LABEL"] = data.target
print(df)
for column, dtype in df.dtypes.items():
if column == "LABEL":
continue
if dtype not in ["int64", "float64", "float32"]:
print(str(column) + ": \t \t " + str(dtype))
if len(df[column].unique()) > 2:
# print(pd.get_dummies(df[column], prefix=column))
df = pd.concat(
[
pd.get_dummies(df[column], prefix=column),
df.drop(column, axis=1),
],
axis=1,
)
else:
df.loc[:, column] = df.loc[:, column].astype("category").cat.codes
labelEncoder = LabelEncoder()
df["LABEL"] = labelEncoder.fit_transform(df.LABEL.values)
df = df.dropna()
feature_columns = df.columns.to_list()
feature_columns.remove("LABEL")
# feature normalization
scaler = RobustScaler()
df[feature_columns] = scaler.fit_transform(df[feature_columns])
# scale back to [0,1]
scaler = MinMaxScaler()
df[feature_columns] = scaler.fit_transform(df[feature_columns])
print(df)
df.to_csv(fetch_func.__name__ + "_.csv", index=False)
# df.to_csv(
# "../datasets/large_cleaned/" + os.path.basename(f).split(".")[0] + ".csv",
# index=False,
# )
#
exit(-1)
exit(-1)