cadcad_machine_search/visualizations/sensitivity.py
def f(X: pd.DataFrame,
      y: pd.Series,
      target: str,
      ax_dt: object,
      ax_rf: object,
      label: str = 'target'):
    """
    Fit DT and RF classifiers for summarizing the sensivity.
    """
    model = DecisionTreeClassifier(class_weight='balanced',
                                   max_depth=5,
                                   )
    rf = RandomForestClassifier()
    model.fit(X, y)
    rf.fit(X, y)

    df = (pd.DataFrame(list(zip(X.columns, rf.feature_importances_)),
                       columns=['features', 'importance'])
          .sort_values(by='importance', ascending=False)
          )

    plot_tree(model,
              rounded=True,
              proportion=True,
              fontsize=8,
              feature_names=X.columns,
              class_names=['threshold not met', 'threshold met'],
              filled=True,
              ax=ax_dt)
    ax_dt.set_title(
        f'Decision tree for {label}, score: {model.score(X, y) :.0%}. N: {len(X) :.2e}')
    sns.barplot(data=df,
                x=df.features,
                y=df.importance,
                ax=ax_rf,
                label='small')
    plt.setp(ax_rf.xaxis.get_majorticklabels(), rotation=45)
    ax_rf.set_title(f'Feature importance for the {label}')

    return df.assign(target=target)

    Fit DT and RF classifiers for summarizing the sensivity.
    
Fit DT and RF classifiers for summarizing the sensivity.
Fit DT and RF classifiers for summarizing the sensivity .
def f(X: pd.DataFrame,
      y: pd.Series,
      target: str,
      ax_dt: object,
      ax_rf: object,
      label: str = 'target'):
    model = DecisionTreeClassifier(class_weight='balanced',
                                   max_depth=5,
                                   )
    rf = RandomForestClassifier()
    model.fit(X, y)
    rf.fit(X, y)
    df = (pd.DataFrame(list(zip(X.columns, rf.feature_importances_)),
                       columns=['features', 'importance'])
          .sort_values(by='importance', ascending=False)
          )
    plot_tree(model,
              rounded=True,
              proportion=True,
              fontsize=8,
              feature_names=X.columns,
              class_names=['threshold not met', 'threshold met'],
              filled=True,
              ax=ax_dt)
    ax_dt.set_title(
        f'Decision tree for {label}, score: {model.score(X, y) :.0%}. N: {len(X) :.2e}')
    sns.barplot(data=df,
                x=df.features,
                y=df.importance,
                ax=ax_rf,
                label='small')
    plt.setp(ax_rf.xaxis.get_majorticklabels(), rotation=45)
    ax_rf.set_title(f'Feature importance for the {label}')
    return df.assign(target=target)
def f ( X : pd . DataFrame , y : pd . Series , target : str , ax_dt : object , ax_rf : object , label : str = 'target' ) : model = DecisionTreeClassifier ( class_weight = 'balanced' , max_depth = 5 , ) rf = RandomForestClassifier ( ) model . fit ( X , y ) rf . fit ( X , y ) df = ( pd . DataFrame ( list ( zip ( X . columns , rf . feature_importances_ ) ) , columns = [ 'features' , 'importance' ] ) . sort_values ( by = 'importance' , ascending = False ) ) plot_tree ( model , rounded = True , proportion = True , fontsize = 8 , feature_names = X . columns , class_names = [ 'threshold not met' , 'threshold met' ] , filled = True , ax = ax_dt ) ax_dt . set_title ( f'Decision tree for {label}, score: {model.score(X, y) :.0%}. N: {len(X) :.2e}' ) sns . barplot ( data = df , x = df . features , y = df . importance , ax = ax_rf , label = 'small' ) plt . setp ( ax_rf . xaxis . get_majorticklabels ( ) , rotation = 45 ) ax_rf . set_title ( f'Feature importance for the {label}' ) return df . assign ( target = target )
Fit DT and RF classifiers for summarizing the sensivity.
Fit DT and RF classifiers for summarizing the sensivity .
"""
    Fit DT and RF classifiers for summarizing the sensivity.
    """
X
pd.DataFrame
y
pd.Series
target
str
ax_dt
object
ax_rf
object
label
str