-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategorical_features.py
60 lines (55 loc) · 2.1 KB
/
categorical_features.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
############################### CATEGORICAL FEATURES ENCODING ##################
### The features "player1", "player2" and "Tournament" are treated differently
### from the other features.
def categorical_features_encoding(cat_features):
"""
Categorical features encoding.
Simple one-hot encoding.
"""
cat_features=cat_features.apply(preprocessing.LabelEncoder().fit_transform)
ohe=OneHotEncoder()
cat_features=ohe.fit_transform(cat_features)
cat_features=pd.DataFrame(cat_features.todense())
cat_features.columns=["cat_feature_"+str(i) for i in range(len(cat_features.columns))]
cat_features=cat_features.astype(int)
return cat_features
def features_players_encoding(data):
"""
Encoding of the players .
The players are not encoded like the other categorical features because for each
match we encode both players at the same time (we put a 1 in each row corresponding
to the players playing the match for each match).
"""
winners=data.Winner
losers=data.Loser
le = preprocessing.LabelEncoder()
le.fit(list(winners)+list(losers))
winners=le.transform(winners)
losers=le.transform(losers)
encod=np.zeros([len(winners),len(le.classes_)])
for i in range(len(winners)):
encod[i,winners[i]]+=1
for i in range(len(losers)):
encod[i,losers[i]]+=1
columns=["player_"+el for el in le.classes_]
players_encoded=pd.DataFrame(encod,columns=columns)
return players_encoded
def features_tournaments_encoding(data):
"""
Encoding of the tournaments .
"""
tournaments=data.Tournament
le = preprocessing.LabelEncoder()
tournaments=le.fit_transform(tournaments)
encod=np.zeros([len(tournaments),len(le.classes_)])
for i in range(len(tournaments)):
encod[i,tournaments[i]]+=1
columns=["tournament_"+el for el in le.classes_]
tournaments_encoded=pd.DataFrame(encod,columns=columns)
return tournaments_encoded