File size: 5,364 Bytes
f0d411f e390497 f0d411f cac478c f0d411f cac478c f0d411f cac478c f0d411f cac478c f0d411f cac478c f0d411f cac478c f0d411f e390497 f0d411f cac478c f0d411f e390497 f0d411f e390497 f0d411f |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import streamlit as st
import pandas as pd
import numpy as np
from flaml import AutoML
from flaml.automl.data import get_output_from_log
import pickle
import plotly.express as px
import base64
import time
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.inspection import permutation_importance
def autoML(csv, task, budget, label, metric_to_minimize_class, metric_to_minimize_reg):
progress_text="Training in progress. Please wait."
my_bar = st.progress(0, text=progress_text)
time.sleep(0.5)
df = pd.read_csv(csv)
df_features = df[df.columns.difference([label])]
y = df[label]
my_bar.progress(50, text=progress_text)
if task == 'Classification':
metric = metric_to_minimize_class
log = 'classlog.log'
automl_settings = {
"time_budget": int(budget),
"metric": metric,
"task": 'classification',
"log_file_name": log,
"early_stop": True,
"eval_method": "holdout"
}
if task == 'Regression':
metric = metric_to_minimize_reg
log = 'reglog.log'
automl_settings = {
"time_budget": int(budget),
"metric": metric,
"task": 'regression',
"log_file_name": log,
"early_stop": True,
"eval_method": "holdout"
}
num_cols = df_features.select_dtypes(include=['float64', 'int64']).columns
cat_cols = df_features.select_dtypes(include=['object']).columns
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='mean')),
('scaler', StandardScaler())
])
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, num_cols),
('cat', categorical_transformer, cat_cols)
])
automl = AutoML(**automl_settings)
pipeline = Pipeline(steps=[('preprocessor', preprocessor),
('classifier', automl)])
pipeline.fit(df_features, y)
my_bar.progress(100, text=progress_text)
time.sleep(0.5)
my_bar.empty()
tab1, tab2 = st.tabs(["AutoML", "Best Model"])
with tab1:
time_history, best_valid_loss_history, valid_loss_history, config_history, metric_history = get_output_from_log(filename=log, time_budget=120)
def model(s):
mod = s.get('Current Learner')
return mod
def hp(s):
hparams = s.get('Current Hyper-parameters')
return hparams
df_res = pd.DataFrame({'time': time_history,
metric: 1 - np.array(best_valid_loss_history),
'model': list(map(model, config_history)),
})
fig = px.line(df_res,
title='evolution of best models found by AutoML',
x='time',
y=metric,
hover_name='model',
line_shape='hv',
range_y=[0,1])
st.plotly_chart(fig, theme="streamlit")
models = pd.DataFrame({'learner': list(map(model, config_history))})
hps = list(map(hp, config_history))
df_hp = pd.DataFrame(hps)
df_models = pd.concat((models, df_hp), axis=1)
def highlight_last_row(s):
return ['background-color: yellow' if i == len(s) - 1 else '' for i in range(len(s))]
st.dataframe(df_models.style.apply(highlight_last_row, axis=0))
st.write('Estimator tested')
st.table(automl.estimator_list)
with tab2:
st.header('Best Model')
st.text(automl.model.estimator)
col1, col2, col3 = st.columns((1,1,1))
with col1:
st.metric(label=metric, value=round(1 - automl.best_loss, 2))
with col2:
st.metric(label="Time to find", value=str(round(automl.time_to_find_best_model, 2))+' sec')
with col3:
st.metric(label="Time to train", value=str(round(automl.best_config_train_time, 2))+' sec')
perm_importance = permutation_importance(
pipeline, df_features, y, n_repeats=8
)
df_features_importance = pd.DataFrame({'features name': df_features.columns,
'features importance': perm_importance["importances_mean"],
'std error': perm_importance["importances_std"]})
fig_features = px.bar(df_features_importance, x='features importance', y='features name', error_x='std error')
st.divider()
st.plotly_chart(fig_features, theme="streamlit")
def download_model(model):
output_model = pickle.dumps(model)
b64 = base64.b64encode(output_model).decode()
href = f'<a href="data:file/output_model;base64,{b64}" download="automl.pkl">Download Trained Model File (.pkl)</a>'
st.markdown(href, unsafe_allow_html=True)
download_model(automl) |