<jupyter_start><jupyter_text>Feature Importance<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from random import randint
starcraft_loc = '../data/interimStarcraft_cleaned.csv'
#using index_col = 0 to drop the uncessary number column added by saving the data from the previous notebook.
starcraft = pd.read_csv(starcraft_loc,index_col = 0)
starcraft.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 3337 entries, 0 to 3336
Data columns (total 20 columns):
 #   Column                Non-Null Count  Dtype  
---  ------                --------------  -----  
 0   GameID                3337 non-null   int64  
 1   LeagueIndex           3337 non-null   int64  
 2   Age                   3337 non-null   int64  
 3   HoursPerWeek          3337 non-null   int64  
 4   TotalHours            3337 non-null   int64  
 5   APM                   3337 non-null   float64
 6   SelectByHotkeys       3337 non-null   float64
 7   AssignToHotkeys       3337 non-null   float64
 8   UniqueHotkeys         3337 non-null   int64  
 9   MinimapAttacks        3337 non-null   float64
 10  MinimapRightClicks    3337 non-null   float64
 11  NumberOfPACs          3337 non-null   float64
 12  GapBetweenPACs        3337 non-null   float64
 13  ActionLatency         3337 non-null   float64
 14  ActionsInPAC          3337 non-null   float64
 15  TotalMapExplored     [...]<jupyter_text>Importance for the entire data set:<jupyter_code>#Split for testing feature importance on the whole set
SCfeatureNames = ['Age','HoursPerWeek','TotalHours','APM','AssignToHotkeys','UniqueHotkeys','MinimapAttacks','MinimapRightClicks','NumberOfPACs','GapBetweenPACs','ActionLatency','ActionsInPAC','TotalMapExplored','WorkersMade','UniqueUnitsMade','ComplexUnitsMade','ComplexAbilitiesUsed']
X = starcraft[SCfeatureNames]
y = starcraft['LeagueIndex']
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=.25,random_state=420)
rf = RandomForestRegressor(n_estimators=100)
rf.fit(X_train, y_train)
#create a feature name sorting function based on feature importance
def feat_sort(features,importances):
    paired = zip(features,importances)
    res = sorted(paired, key = lambda x: x[1])
    toRet = []
    for x,y in res:
        toRet.append(x)
    return toRet
starcraft.corr()
starcraft = starcraft.drop('SelectByHotkeys',axis=1)
sorted_idx = rf.feature_importances_.argsort()
SCfeatureNamesSorted = feat_sort(SCfeatureNames,rf.feature_importances_)
plt.barh(SCfeatureNamesSorted, rf.feature_importances_[sorted_idx])
plt.title('Feature Importance for entire data set')
plt.xlabel('Importance')
plt.ylabel('Features')
plt.show()<jupyter_output><empty_output><jupyter_text>Importance for each league transition.We will break the data set into sub sets containing each league transition, ie 1-2, 2-3, 3-4 so on and so forth, and check feature importance for each league transition<jupyter_code>leagueTrans = []
for i in range(1,7): 
    leagueTrans.append(pd.concat([starcraft[starcraft['LeagueIndex'] == i],starcraft[starcraft['LeagueIndex'] == i+1]],axis=0))

i=1
featImpPerTrans = []
featImpPairs = []
for transition in leagueTrans:
    i+=1
    X = transition[SCfeatureNames]
    y = transition['LeagueIndex']
    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=.25,random_state=420)
    rf = RandomForestRegressor(n_estimators=100)
    rf.fit(X_train, y_train)
    sorted_idx = rf.feature_importances_.argsort()
    SCfeatureNamesSorted = feat_sort(SCfeatureNames,rf.feature_importances_)
    featImpPerTrans.append(rf.feature_importances_.tolist())
    plt.barh(SCfeatureNamesSorted, rf.feature_importances_[sorted_idx])
    featImpPairs.append((SCfeatureNamesSorted,rf.feature_importances_[sorted_idx].tolist()))
    plt.title('Feature Importance for league change of '+str(i-1)+' to '+str(i))
    plt.xlabel('Importance')
    plt.ylabel('Features')
    plt.show()
colNames = ['Transition']
for name in SCfeatureNames:
    colNames.append(name)
importanceDF = pd.DataFrame(columns = colNames)
transNames = []
for i in range(1,7):
    transNames.append(str(i)+' - '+str(i+1))
i = 0
for importance in featImpPerTrans:
    importance.insert(0,transNames[i])
    importanceDF.loc[len(importanceDF.index)] = importance
    i+=1
importanceDF.set_index('Transition', inplace=True)
importanceDF.head()
sns.set(rc = {'figure.figsize':(14,5)})
colors = cm.get_cmap('tab20',18)
importanceDF.plot(kind='bar',stacked=True,colormap=colors)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()
colorsDict={}
handle_list = []
label_list=[]
for num in range(17):
    colorsDict[SCfeatureNames[num]] = '#%06X' % randint(0, 0xFFFFFF)
for i in range(1,7):
    tempDF = pd.DataFrame(columns=['feature','data'])
    for y in range(16,11,-1):
        tempDF.loc[len(tempDF.index)] = [featImpPairs[i-1][0][y],featImpPairs[i-1][1][y]]
    tempDF['data'] = tempDF['data']/tempDF['data'].sum()
    print(tempDF['data'].sum())
    bottom = 0
    for row in tempDF.index:
        plt.bar(x=i,height=tempDF.iloc[row,1],bottom=bottom,label=tempDF.iloc[row,0],color=colorsDict[tempDF.iloc[row,0]])
        bottom += tempDF.iloc[row,1]
        handles, labels = plt.gca().get_legend_handles_labels()
        for handle, label in zip(handles, labels):
            if label not in label_list:
                handle_list.append(handle)
                label_list.append(label)
    plt.legend(handle_list,label_list,bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('Single league transition')
plt.ylabel('Normalized importance of the top 5 features')
plt.xticks([1,2,3,4,5,6],['1-2','2-3','3-4','4-5','5-6','6-7'])
plt.show()<jupyter_output>1.0
1.0
1.0
1.0000000000000002
1.0
0.9999999999999999
{'fraction_non_alphanumeric': 0.12451295951211248, 'fraction_numerical': 0.04506183296628833, 'mean_word_length': 4.252669039145908, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '553020', 'license': 'MIT', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2025, 'n_words': 412, 'path': 'notebooks/03_Getz_Feature_Importance.ipynb', 'repo_name': 'theGetz1995/Capstone-2', 'size': '5903'}