File size: 2,659 Bytes
f4e300e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
702419b
f4e300e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#create the explainer and shap values 

import pandas as pd
import streamlit as st
from st_aggrid import AgGrid, GridOptionsBuilder
from st_aggrid.shared import GridUpdateMode
import shap
import streamlit as st
import streamlit.components.v1 as components
import pickle
import xgboost
import joblib




def st_shap(plot, height=None):
    shap_html = f"<head>{shap.getjs()}</head><body>{plot.html()}</body>"
    components.html(shap_html, height=height)


def scale_and_standardize(df):
    from sklearn.preprocessing import StandardScaler    
    scaler = StandardScaler()
    scaler.fit(df)
    scaled_df = scaler.transform(df)
    return scaled_df

def aggrid_interactive_table(df: pd.DataFrame):
    """Creates an st-aggrid interactive table based on a dataframe.

    Args:
        df (pd.DataFrame]): Source dataframe

    Returns:
        dict: The selected row
    """
    options = GridOptionsBuilder.from_dataframe(
        df, enableRowGroup=True, enableValue=True, enablePivot=True
    )

    options.configure_side_bar()

    options.configure_selection("single")
    selection = AgGrid(
        df,
        enable_enterprise_modules=True,
        gridOptions=options.build(),
        theme="light",
        update_mode=GridUpdateMode.MODEL_CHANGED,
        allow_unsafe_jscode=True,
    )

    return selection

st.title("Pay With Moon Leaderboard")

#load joblib
df_M_character = joblib.load('df_M_character.joblib') 
df_M_character_scale = scale_and_standardize(df_M_character)
author_idx_lookup = dict([(name, idx) for idx, name in enumerate(df_M_character.index)])
ppl_labels = joblib.load('ppl_labels.joblib') 
# train XGBoost model
X, y = pd.DataFrame(
    df_M_character_scale, 
    columns=df_M_character.columns
), ppl_labels

bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)

# explain the model's predictions using SHAP values
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)
# shap.summary_plot(shap_values, X)


X = pd.DataFrame(
    df_M_character_scale, 
    columns=df_M_character.columns)


df_leaderboard = pd.read_csv("leaderboard.csv")
df_leaderboard.rename(columns = {'Unnamed: 0':'Rank', 'author':'member'}, inplace=True)
selection = aggrid_interactive_table(df=df_leaderboard)


if selection:
    try: 
        auth = selection["selected_rows"][0]['member']
    except:
        auth = df_leaderboard['member'].iloc[0]

    st.write(auth)
    author_idx = author_idx_lookup.get(auth)
    # visualize the first prediction's explaination with default colors
    st_shap(shap.force_plot(explainer.expected_value, shap_values[author_idx,:], X.iloc[author_idx,:]))