AutoML_Model / app.py
Abdulabin's picture
Update app.py
5277f37 verified
Raw History Blame Contribute Delete
15.2 kB
import os
import evalml
import pandas as pd
import streamlit as st
import plotly.graph_objects as go
from evalml.automl import AutoMLSearch
from evalml.data_checks import DefaultDataChecks
from streamlit_option_menu import option_menu
from evalml.problem_types import detect_problem_type
from evalml.automl import get_default_primary_search_objective
from evalml.objectives import get_optimization_objectives
from time_series_splitter import split_time_series_data
from evalml.automl import get_default_primary_search_objective
from evalml.objectives import get_optimization_objectives
from evalml.model_understanding.metrics import confusion_matrix
from evalml.model_understanding.prediction_explanations import explain_predictions
from evalml.model_understanding import graph_prediction_vs_actual_over_time
st.set_page_config(None,layout="wide")
st.title("All_in_one_Model")
problem_types = ['BINARY', 'MULTICLASS', 'REGRESSION',]
time_problems = ['TIME SERIES REGRESSION','TIME SERIES BINARY', 'TIME SERIES MULTICLASS']
prob = option_menu(None, options=["General Problem","Time Series Problem"],
menu_icon="cast", default_index=0, orientation="horizontal",)
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
if prob =="General Problem":
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
with st.container():
col1 ,col2 ,col3 = st.columns(3)
with col1:
y_col = st.selectbox("Select Output Columns",options=df.columns,index= (df.shape[1])-1,)
X = df.drop(y_col,axis=1)
y= df[y_col]
with col2:
detect_prob_type = detect_problem_type(y).name
problem_type = st.selectbox("Select Problem Type",options=problem_types,index= problem_types.index(detect_prob_type),)
with col3:
objective_auto = get_default_primary_search_objective(problem_type).name
objectives = []
for objective in get_optimization_objectives(problem_type):
objectives.append(objective.name)
objective_type = st.selectbox("Select Objective Type",options=objectives,index=objectives.index(objective_auto))
data_checks = DefaultDataChecks(problem_type, objective_type)
check = data_checks.validate(X,y)
if uploaded_file is not None:
if len(check) == 0:
st.success("Data Check Successful")
else:
st.error("Data Check Unsuccessful")
X_train, X_test, y_train, y_test = evalml.preprocessing.split_data(X, y,
problem_type=problem_type,
test_size=0.2,)
automl = AutoMLSearch(X_train=X_train,
y_train=y_train,
X_holdout=X_test,
y_holdout=y_test,
problem_type=problem_type,
allow_long_running_models=False,
objective=objective_type,
max_batches=3,
ensembling=True,
verbose=True
)
selected = option_menu(None, options=["Home", "Predict","Model_info","Graphs","XAI"],
icons=['house', 'person',"card-text", "list-task", 'chat-left-text-fill'], menu_icon="cast", default_index=0, orientation="horizontal",)
if selected == "Home":
train = st.button("Train Model ")
if train:
st.write("Training Model ....")
automl.search()
best_pipeline = automl.best_pipeline
best_pipeline.save("best_model.pkl")
load_model=automl.load('best_model.pkl')
st.dataframe(automl.rankings)
y_pred = load_model.predict(X_test)
st.write(load_model.score(X_test, y_test, objectives=objectives))
try:
y_pred = load_model.predict(X_test)
st.dataframe(confusion_matrix(y_test, y_pred))
except:
pass
if selected =="Predict":
try:
load_model=automl.load('best_model.pkl')
df1= df.drop(y_col,axis=1)
column_names = df1.columns
column_data_types = df1.dtypes
st.subheader("Enter Data:")
col1, col2 = st.columns(2)
new_row_data = {}
for col_name, col_type in zip(column_names, column_data_types):
if col_type == 'object':
with col1:
input_data = st.text_input(f"Enter {col_name}")
elif col_type == 'int64' or col_type == 'float64':
with col2:
input_data = st.number_input(f"Enter {col_name}")
else:
input_data = None
st.write(f"Data type {col_type} not supported for column {col_name}")
new_row_data[col_name] = input_data
if st.button("Predict"):
load_model=automl.load('best_model.pkl')
new_df = pd.DataFrame([new_row_data],columns=column_names)
st.dataframe(new_df,hide_index=True,width=1200)
pred = load_model.predict(new_df)
st.success(f"Predicted Value : {pred.values[0]}")
except:
st.write("Please Train Model first ..")
if selected == "Model_info":
try:
load_model=automl.load('best_model.pkl')
st.write(load_model.describe(return_dict=True))
except:
st.write("Please Train Model first ..")
if selected == "Graphs":
try:
pass
except:
st.write("Please Train Model first ..")
if selected == "XAI":
try:
with st.container():
col1 ,col2 = st.columns(2)
with col1:
load_model=automl.load('best_model.pkl')
st.dataframe(load_model.feature_importance)
with col2:
table = explain_predictions(
pipeline=load_model,
input_features=X_test,
y=None,
output_format="dataframe",
indices_to_explain=[3],
top_k_features=8,
include_explainer_values=True,
algorithm='shap'
)
st.subheader("Positive and Negative contributing features")
st.dataframe(table)
except:
st.write("Please Train Model first ..")
try:
with open("best_model.pkl", "rb") as f:
file_contents = f.read()
st.download_button('Download Model', file_contents, file_name='best_model.pkl', mime='application/octet-stream')
except:
pass
if prob == "Time Series Problem":
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
with st.container():
col1 ,col4,col2 ,col3 = st.columns(4)
with col1:
y_col = st.selectbox("Select Output Column",options=df.columns,index= (df.shape[1])-1,)
X = df.drop(y_col,axis=1)
y= df[y_col]
with col4:
def date_column_name(X):
date_column = None
for column in X.columns:
if pd.api.types.is_datetime64_any_dtype(X[column]):
date_column = column
break
if date_column is None:
raise ValueError("No datetime column found in the DataFrame.")
return date_column
try:
pred_date = date_column_name(df)
except:
pred_date = df.columns[0]
date_col = st.selectbox("Select Date Column",options=X.columns,index=X.columns.get_loc(pred_date))
df[date_col] = pd.to_datetime(df[date_col])
with col2:
detect_prob_type = detect_problem_type(y).name
try:
problem_type = st.selectbox("Select Problem Type",options=time_problems,index= time_problems.index(detect_prob_type),)
except:
problem_type = st.selectbox("Select Problem Type",options=time_problems,index= 0,)
with col3:
objective_auto = get_default_primary_search_objective(problem_type).name
objectives = []
for objective in get_optimization_objectives(problem_type):
objectives.append(objective.name)
objective_type = st.selectbox("Select Objective Type",options=objectives,index=objectives.index(objective_auto))
X_train, X_test, y_train, y_test = split_time_series_data(date_col,X,y,test_size=0.1)
with st.container():
col1 ,col2 ,col3= st.columns(3)
with col1:
gap = st.number_input('Select Gap in Data ',value= 0,min_value=0,max_value=10)
with col2:
max_delay = st.number_input('Select Max Delay',value=7,min_value=3,)
with col3:
forecast_horizon = st.number_input('Select forecast horizon ',value=7)
if problem_type=='TIME SERIES REGRESSION':
algo_families =["All Algorithm",'RANDOM_FOREST','XGBOOST','LIGHTGBM','LINEAR_MODEL','EXPONENTIAL_SMOOTHING','ARIMA','PROPHET',]
elif problem_type=='TIME SERIES BINARY':
algo_families =["All Algorithm",'RANDOM_FOREST','XGBOOST','LIGHTGBM','LINEAR_MODEL',"EXTRA_TREES"]
elif problem_type=='TIME SERIES MULTICLASS':
algo_families =["All Algorithm",'RANDOM_FOREST','XGBOOST','LIGHTGBM','LINEAR_MODEL',"EXTRA_TREES"]
algo_select = st.multiselect(
'Select Algorithm',
algo_families,
default=["All Algorithm"],
)
if "All Algorithm" in algo_select or len(algo_select)==0:
algo_select=None
problem_config = {"gap": gap, "max_delay": max_delay, "forecast_horizon": forecast_horizon, "time_index":date_col }
automl_time = AutoMLSearch(
X_train,
y_train,
problem_type = problem_type,
objective=objective_type,
max_batches=1,
ensembling=True,
problem_configuration=problem_config,
automl_algorithm="iterative",
allowed_model_families=algo_select
)
with st.container():
col1 ,col2 ,col3= st.columns([3,3,6])
with col1:
train_time = st.button("Train Time Series Model....")
with col2:
load_time = st.button("Load Best Time Series Model....")
if train_time:
st.write("Model Training.....")
automl_time.search()
st.dataframe(automl_time.rankings)
best_pipeline_time = automl_time.best_pipeline
best_pipeline_time.save("best_model_time.pkl")
if load_time:
try:
load_model_time=automl_time.load('best_model_time.pkl')
st.write(load_model_time.describe(return_dict=True))
load_model_time.fit(X_train, y_train)
best_pipeline_score = load_model_time.score(X_test, y_test, [objective_type], X_train, y_train)[
objective_type]
st.write(f"{objective_type} : {best_pipeline_score}")
fig = graph_prediction_vs_actual_over_time(load_model_time, X_test, y_test, X_train, y_train, dates=X_test[date_col])
fig.update_layout(height=700)
st.plotly_chart(fig,use_container_width=True,height=200)
st.header("Predict Next 7 Days")
load_model_time.fit(X, y)
X_forecast_dates = load_model_time.get_forecast_period(X=X).to_frame()
y_forecast = load_model_time.get_forecast_predictions(X=X, y=y)
res = load_model_time.get_prediction_intervals(X=pd.DataFrame(X_forecast_dates), y=y_forecast, X_train=X, y_train=y)
y_lower = res["0.95_lower"]
y_upper = res["0.95_upper"]
X_before = X[-31:]
y_before = y[-31:]
fig2 = go.Figure(
[
go.Scatter(x=X_before[date_col], y=y_before, name="Training Data", mode="lines"),
go.Scatter(
x=X_forecast_dates[date_col], y=y_forecast, name="Forecast Data", mode="lines"
),
go.Scatter(
x=pd.concat([X_forecast_dates[date_col], X_forecast_dates[date_col][::-1]]),
y=pd.concat([y_upper, y_lower[::-1]]),
fill="toself",
fillcolor="rgba(255,0,0,0.2)",
line=dict(color="rgba(255,0,0,0.2)"),
name="Forecast Prediction Intervals",
showlegend=True,
),
],
layout={
"title": "Plot of Last Two Weeks of Data + Forecast Data With Prediction Intervals",
"xaxis": dict(title=date_col),
"yaxis": dict(title=y_col),
},
)
fig2.update_layout(height=600)
st.plotly_chart(fig2,use_container_width=True)
except:
st.write("Please Train Model First....")
try:
with open("best_model_time.pkl", "rb") as f:
file_contents = f.read()
st.download_button('Download Model', file_contents, file_name='best_model_time.pkl', mime='application/octet-stream')
except:
pass
file_path = os.path.join('best_model.pkl')
file_path2 = os.path.join('best_model_time.pkl')
if uploaded_file == None:
try:
if prob == "General Problem":
os.remove(file_path)
elif prob == "Time Series Problem":
os.remove(file_path2)
except:
pass