Ejemplo n.º 1
0
def generate_assets():
    X_train, y_train, X_test, y_test = titanic_survive()

    model = RandomForestClassifier(n_estimators=5, max_depth=2)
    model.fit(X_train, y_train)

    explainer = ClassifierExplainer(
                        model, X_test, y_test, 
                        cats=[{'Gender': ['Sex_female', 'Sex_male', 'Sex_nan']}, 
                                            'Deck', 'Embarked'],
                        labels=['Not survived', 'Survived'])

    dashboard = ExplainerDashboard(explainer, 
        [
            ShapDependenceComposite(explainer, title="Test Tab!"),
            ShapDependenceComposite, 
            "importances"
        ], title="Test Title!")

    pkl_dir = Path.cwd() / "tests" / "cli_assets" 
    explainer.dump(pkl_dir / "explainer.joblib")
    explainer.to_yaml(pkl_dir / "explainer.yaml")
    dashboard.to_yaml(pkl_dir / "dashboard.yaml", 
                explainerfile=str(pkl_dir / "explainer.joblib"))
    return None
Ejemplo n.º 2
0
class DashboardTests(unittest.TestCase):
    def setUp(self):
        X_train, y_train, X_test, y_test = titanic_survive()
        train_names, test_names = titanic_names()

        self.model = RandomForestClassifier(n_estimators=5, max_depth=2)
        self.model.fit(X_train, y_train)

        self.explainer = ClassifierExplainer(
                            self.model, X_test, y_test, 
                            cats=[{'Gender': ['Sex_female', 'Sex_male', 'Sex_nan']}, 
                                                'Deck', 'Embarked'],
                            labels=['Not survived', 'Survived'])

        self.dashboard = ExplainerDashboard(self.explainer, 
            [
                ShapDependenceTab(self.explainer, title="Test Tab!"),
                ShapDependenceTab, 
                "importances"
            ], title="Test Title!")

        self.pkl_dir = Path.cwd() / "tests" / "cli_assets" 
        self.explainer.dump(self.pkl_dir / "explainer.joblib")
        self.explainer.to_yaml(self.pkl_dir / "explainer.yaml")
        self.dashboard.to_yaml(self.pkl_dir / "dashboard.yaml", 
                    explainerfile=str(self.pkl_dir / "explainer.joblib"))

    def test_yaml(self):
        yaml = self.dashboard.to_yaml()
        self.assertIsInstance(yaml, str)

    def test_yaml_dict(self):
        yaml_dict = self.dashboard.to_yaml(return_dict=True)
        self.assertIsInstance(yaml_dict, dict)
        self.assertIn("dashboard", yaml_dict)

    def test_load_config_joblib(self):
        db = ExplainerDashboard.from_config(
            self.pkl_dir / "explainer.joblib",
            self.pkl_dir / "dashboard.yaml")
        self.assertIsInstance(db, ExplainerDashboard)

    def test_load_config_yaml(self):
        db = ExplainerDashboard.from_config(
            self.pkl_dir / "dashboard.yaml")
        self.assertIsInstance(db, ExplainerDashboard)

    def test_load_config_explainer(self):
        db = ExplainerDashboard.from_config(
            self.explainer, self.pkl_dir / "dashboard.yaml")
        self.assertIsInstance(db, ExplainerDashboard)
        
Ejemplo n.º 3
0
    model = pickle.load(open(MODELS_DIR / 'general_model.pkl', 'rb'))
    y = pd.read_csv(DATA_DIR / 'general_target.csv',
                    index_col=['Ticker']).drop(columns=['Date'])
    X = pd.read_csv(DATA_DIR / f'general_features.csv',
                    index_col=['Ticker']).drop(columns=['Date'])

    # Dashboard Explainer is fussy about Column Names
    X.columns = X.columns.str.replace('.', '')
    feature_names = model.get_booster().feature_names
    feature_names = [x.replace('.', '') for x in feature_names]
    model.get_booster().feature_names = feature_names

    explainer = RegressionExplainer(model, X, y)

    db = ExplainerDashboard(
        explainer,
        title="Stock Valuation Explainer",
        description=
        "Visit https://share.streamlit.io/gardnmi/fundamental-stock-prediction to see the model in use,",
        shap_interaction=False,
        precision='float32',
        decision_trees=False)

    db.to_yaml("dashboard.yaml",
               explainerfile="explainer.joblib",
               dump_explainer=True)

db = ExplainerDashboard.from_config("dashboard.yaml")
app = db.flask_server()
Ejemplo n.º 4
0
from explainerdashboard import RegressionExplainer, ExplainerDashboard
from explainerdashboard.custom import *
from joblib import load
import pandas as pd
from sklearn.model_selection import train_test_split

# Load model & data
dec_tree = load('dec_tree_v3.joblib')
df_data = pd.read_csv('data_v3_enc.csv')

# Prepare & split data
X = df_data.drop(['Duration', 'Timestamp'], axis=1)
y = df_data.Duration
Xt, X_small, yt, y_small = train_test_split(X, y, test_size=0.01, random_state=0)

exp = RegressionExplainer(dec_tree, X_small, y_small, cats=['Day_of_week', 'Hour', 'Vehicle', 'Position'])

# Build
db = ExplainerDashboard(exp, [ShapDependenceComposite, WhatIfComposite], hide_whatifpdp=True)

# Save
exp.dump("explainer.joblib")
db.to_yaml("dashboard.yaml")