forked from interpretml/fastSparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarization_utils.py
35 lines (26 loc) · 1.06 KB
/
binarization_utils.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
import numpy as np
import pandas as pd
def convert_continuous_df_to_binary_df(df):
colnames = df.columns
n = len(df)
print("Make sure your first column corresponds to the y label")
print("Converting continuous features to binary features in the dataframe......")
percentile_ticks = range(1, 101)
binarized_dict = {}
for i in range(0, len(colnames)):
uni = df[colnames[i]].unique()
if len(uni) == 2:
binarized_dict[colnames[i]] = np.asarray(df[colnames[i]], dtype=int)
continue
uni.sort()
if len(uni) >= 100:
uni = np.percentile(uni, percentile_ticks)
for j in range(len(uni)-1):
tmp_feature = np.ones(n, dtype=int)
tmp_name = colnames[i] + "<=" + str(uni[j])
zero_indices = df[colnames[i]] > uni[j]
tmp_feature[zero_indices] = 0
binarized_dict[tmp_name] = tmp_feature
binarized_df = pd.DataFrame(binarized_dict)
print("Finish converting continuous features to binary features......")
return binarized_df