Skip to content

Instantly share code, notes, and snippets.

View FelixChop's full-sized avatar

Félix Revert FelixChop

View GitHub Profile
from treeinterpreter import treeinterpreter as ti, utils
prediction, bias, contributions = ti.predict(rf_model, data_to_analyze[X_test.columns])
# Print the local interpretation tables
for i in range(10):
print("Bias: " +str(bias[i][1]))
data_to_analyze[X_test.columns].append(pd.DataFrame(
[[round(c[1],3) for c in contributions[i]]],
columns=X_test.columns,
index=['importance'])).T.sort_values('importance', ascending=False)
from treeinterpreter import treeinterpreter as ti
for i,row in X.iterrows():
data_point = pd.DataFrame([row])
data_point.set_axis(['value_variable']) # Once transposed, it will be the column name
prediction, bias, contributions = ti.predict(rf_model, data_point)
local_interpretation = data_point.append(
pd.DataFrame([[round(c[1],3) for c in contributions[0]]], columns=data_point.columns.tolist(), index=['contribution_variable'])
).T.sort_values('contribution_variable', ascending=False)
import pandas as pd
import networkx as nx
df = pd.DataFrame({'ID':[1,2,3],
'First Name':['Felix', 'Jean', 'James'],
'Family Name': ['Revert', 'Durand', 'Wright'],
'Phone number': ['+33 6 99 99 99 99', '+33 7 00 00 00 00', '+33 6 99 99 99 99'],
'Email': ['felix.revert@gmail.com', 'jean.durand@gmail.com', pd.np.nan],
'Zip Code': ['75001', '10350', pd.np.nan]})
import pandas as pd
df = pd.DataFrame({'ID':[1,2,3,4,5,6],
'First Name':['Felix', 'Jean', 'James', 'Daphne', 'James', 'Peter'],
'Family Name': ['Revert', 'Durand', 'Wright', 'Hull', 'Conrad', 'Donovan'],
'Phone number': ['+33 6 12 34 56 78', '+33 7 00 00 00 00', '+33 6 12 34 56 78', '+33 6 99 99 99 99', '+852 0123 4567', '+852 0123 4567'],
'Email': ['felix.revert@gmail.com', 'jean.durand@gmail.com', 'j.custom@gmail.com', pd.np.nan, 'j.custom@gmail.com', pd.np.nan]})
column_edge = 'Phone number'
column_ID = 'ID'
data_to_merge = df[[column_ID, column_edge]].dropna(subset=[column_edge]).drop_duplicates() # select columns, remove NaN
# To create connections between people who have the same number,
# join data with itself on the 'ID' column.
data_to_merge = data_to_merge.merge(
data_to_merge[[column_ID, column_edge]].rename(columns={column_ID:column_ID+"_2"}),
on=column_edge
# By joining the data with itself, people will have a connection with themselves.
# Remove self connections, to keep only connected people who are different.
d = data_to_merge[~(data_to_merge[column_ID]==data_to_merge[column_ID+"_2"])] \
.dropna()[[column_ID, column_ID+"_2", column_edge]]
# To avoid counting twice the connections (person 1 connected to person 2 and person 2 connected to person 1)
# we force the first ID to be "lower" then ID_2
d.drop(d.loc[d[column_ID+"_2"]<d[column_ID]].index.tolist(), inplace=True)
import networkx as nx
G = nx.from_pandas_edgelist(df=d, source=column_ID, target=column_ID+'_2', edge_attr=column_edge)
G.add_nodes_from(nodes_for_adding=d.ID.tolist())
import networkx as nx
G = nx.from_pandas_edgelist(df=d, source=column_ID, target=column_ID+'_2', edge_attr=column_edge)
G.add_nodes_from(nodes_for_adding=df.ID.tolist())
nx.draw(G)
eval_set = [(X_train, y_train), (X_test, y_test)]
eval_metric = ["auc","error"]
%time model.fit(X_train, y_train, eval_metric=eval_metric, eval_set=eval_set, verbose=True)