|
a |
|
b/preprocessing.py |
|
|
1 |
import numpy as np |
|
|
2 |
import pandas as pd |
|
|
3 |
from sklearn.model_selection import train_test_split |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
def load_csv(): |
|
|
7 |
""" |
|
|
8 |
:return: a dataframe containing our X and Y values |
|
|
9 |
""" |
|
|
10 |
aml_data = pd.read_csv('data.csv', index_col=0) |
|
|
11 |
del aml_data['DrawID'] |
|
|
12 |
aml_data['caseflag'].replace({'Yes': 1, 'No': -1}, inplace=True) |
|
|
13 |
return aml_data |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def fill_missing_values(aml_data): |
|
|
17 |
""" |
|
|
18 |
:param aml_data: a dataframe containing our X and Y values |
|
|
19 |
:return: an updated version of aml_data with no missing values (replaced by the mean of the column) |
|
|
20 |
""" |
|
|
21 |
for column in aml_data.columns: |
|
|
22 |
aml_data[column].fillna(aml_data[column].mean(), inplace=True) |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
def add_total_genes(aml_data): |
|
|
26 |
""" |
|
|
27 |
:param aml_data: a dataframe containing our X and Y values |
|
|
28 |
:return: an updated version of aml_data containing a new column with the sum of all allele frequencies |
|
|
29 |
""" |
|
|
30 |
aml_data['Total.Genes'] = 0 |
|
|
31 |
for column in aml_data.columns: |
|
|
32 |
if 'Gene.' in column: |
|
|
33 |
aml_data['Total.Genes'] += aml_data[column] |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
def add_extra_features(aml_data): |
|
|
37 |
""" |
|
|
38 |
:param aml_data: a dataframe containing our X and Y values |
|
|
39 |
:return: an updated version of aml_data containing new interaction terms |
|
|
40 |
""" |
|
|
41 |
columns = ['Total.Genes', 'HEMATOCR', 'PLATELET', 'WBC', 'HEMOGLBN', 'Age'] |
|
|
42 |
for i in range(len(columns)): |
|
|
43 |
for j in range(i, len(columns)): |
|
|
44 |
aml_data[columns[i] + columns[j]] = aml_data[columns[i]] * aml_data[columns[j]] |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
def preprocessing(add_features=False): |
|
|
48 |
""" |
|
|
49 |
:param add_features: if set to true, we add some "interaction features" |
|
|
50 |
:return: our featurized x and y values |
|
|
51 |
""" |
|
|
52 |
aml_data = load_csv() |
|
|
53 |
fill_missing_values(aml_data) |
|
|
54 |
add_total_genes(aml_data) |
|
|
55 |
|
|
|
56 |
if add_features: |
|
|
57 |
add_extra_features(aml_data) |
|
|
58 |
|
|
|
59 |
y_values = aml_data['caseflag'].values |
|
|
60 |
del aml_data['caseflag'] |
|
|
61 |
return aml_data.values, y_values |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
def get_train_and_test(x_values, y_values): |
|
|
65 |
""" |
|
|
66 |
:param x_values: the featurized values of x |
|
|
67 |
:param y_values: the featurized values of y |
|
|
68 |
:return: a tuple containing x_train, x_test, y_train, and y_test |
|
|
69 |
""" |
|
|
70 |
test_set_size = int(len(y_values) * 0.35) |
|
|
71 |
return train_test_split(x_values, y_values, test_size=test_set_size, random_state=8) |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
def standardize_features(x_train, x_test): |
|
|
75 |
""" |
|
|
76 |
:param x_train: 2D numpy array of size (num_instances, num_features) |
|
|
77 |
:param x_test: 2D numpy array of size (num_instances, num_features) |
|
|
78 |
:return: a tuple containing the newly standardized values for train and test |
|
|
79 |
""" |
|
|
80 |
i = 0 |
|
|
81 |
while i < x_train.shape[1]: |
|
|
82 |
std = np.std(x_train[:, i]) |
|
|
83 |
mean = np.mean(x_train[:, i]) |
|
|
84 |
if not std: |
|
|
85 |
x_train = np.delete(x_train, i, 1) |
|
|
86 |
x_test = np.delete(x_test, i, 1) |
|
|
87 |
else: |
|
|
88 |
x_train[:, i] = (x_train[:, i] - mean)/std |
|
|
89 |
x_test[:, i] = (x_test[:, i] - mean)/std |
|
|
90 |
i += 1 |
|
|
91 |
num_features = x_train.shape[1] |
|
|
92 |
return np.insert(x_train, num_features, 1, axis=1), np.insert(x_test, num_features, 1, axis=1) |