This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df = X_test.copy()df['predictions'] | |
| rf_model.predict_proba(X_test) | |
| data_to_analyze = df.sort_values('predictions', ascending=False).head(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df = X_test.copy() | |
| df['predictions'] = rf_model.predict_proba(X_test) | |
| data_to_analyze = df.sort_values('predictions', ascending=False).head(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.ensemble import RandomForestClassifier # from xgboost import XGBClassifier | |
| model = RandomForestClassifier() # XGBClassifier() | |
| model.fit(X, y) | |
| pd.DataFrame({'Variable':X.columns, | |
| 'Importance':model.feature_importances_}).sort_values('Importance', ascending=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os, bs4, requests | |
| import pandas as pd | |
| PATH = os.path.join("C:\\","Users","xxx","Documents","py") # you need to change to your local path | |
| res = pd.DataFrame() | |
| url = "http://bank-code.net/country/FRANCE-%28FR%29/" | |
| counter = 0 | |
| def table_to_df(table): | |
| return pd.DataFrame([[td.text for td in row.findAll('td')] for row in table.tbody.findAll('tr')]) |