Created
January 29, 2021 10:09
-
-
Save vinczebalazs/07039aa148d8100a5ec402720326daaf to your computer and use it in GitHub Desktop.
This file contains 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 joblib | |
import pickle | |
import sklearn.inspection | |
import numpy as np | |
import pandas as pd | |
import os.path | |
rows = ["Pers Clubs A","Pers Clubs 10","Pers Clubs K","Pers Clubs Q","Pers Clubs J", | |
"Pers Diamonds A","Pers Diamonds 10","Pers Diamonds K","Pers Diamonds Q","Pers Diamonds J", | |
"Pers Hearts A","Pers Hearts 10","Pers Hearts K","Pers Hearts Q","Pers Hearts J", | |
"Pers Spades A","Pers Spades 10","Pers Spades K","Pers Spades Q","Pers Spades J", | |
"P1 points","P2 points","P1 pending points","P2 pending points","Trump suit","Phase", | |
"Stock size","Leader","Whose turn","Opponents played card"] | |
feature_df = pd.DataFrame(rows,columns=["Feature"]) | |
df_list = [] | |
model_names = ["BNB","LR","MLPC"] | |
model_dataset = "_rand_g50000_f1" | |
for model_name in model_names: | |
#feature permutation importance file name: | |
fpi_file = "./fpi/fpi%s_%s.csv" %(model_dataset, model_name) | |
if not os.path.isfile(fpi_file): | |
evaluate_dataset = "_rand_g10000_f1" | |
with open("./datasets/dset%s.pkl" %evaluate_dataset, 'rb') as output: | |
data, target = pickle.load(output) | |
model = joblib.load("./models/m%s_%s.pkl" %(model_dataset, model_name)) | |
result = sklearn.inspection.permutation_importance(model, data, target) | |
np.savetxt("./fpi/fpi%s_%s.csv" %(model_dataset, model_name), result.importances_mean, delimiter=",") | |
index = 0 | |
feature_importance = np.array([]) | |
data = np.genfromtxt(fpi_file, delimiter=',') | |
#Perspective for 20 cards, each can be in of 6 states: Unknown, in Stock, P1 Hand, P2 Hand, P1 Won, P2 Won | |
for i in range(20): | |
feature_importance = np.append(feature_importance, np.average( data[index:index+6] )) | |
index += 6 | |
# P1_points,P2_points,P1_pending_points,P2_pending_points | |
for i in range(4): | |
feature_importance = np.append(feature_importance, data[index]) | |
index += 1 | |
# Trump_suit | |
feature_importance = np.append(feature_importance, np.average( data[index:index+4] )) | |
index += 4 | |
# Phase | |
feature_importance = np.append(feature_importance, np.average( data[index:index+2] )) | |
index += 2 | |
# Stock_size | |
feature_importance = np.append(feature_importance, data[index]) | |
index += 1 | |
# Leader | |
feature_importance = np.append(feature_importance, np.average( data[index:index+2] )) | |
index += 2 | |
# Whose_turn | |
feature_importance = np.append(feature_importance, np.average( data[index:index+2] )) | |
index += 2 | |
# 21 Opponents_played_cards (21 because opponent can play 20 cards or No card if we start the trick) | |
feature_importance = np.append(feature_importance, np.average( data[index:index+21] )) | |
index += 21 | |
# Normalize and round | |
feature_importance = np.round(feature_importance/np.sum(feature_importance),4) | |
df = pd.concat( [feature_df, pd.DataFrame(feature_importance,columns=[model_name]) ], axis=1) | |
df_sorted = (df.sort_values(model_name, ascending=False)).reset_index(drop=True) | |
df_list.append(df_sorted) | |
final_df = df_list[0] | |
for i in range(1,len(df_list)): | |
final_df = pd.concat([final_df, df_list[i]], axis=1) | |
print(final_df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment