Spaces:
Build error
Build error
File size: 15,226 Bytes
7487a04 67c52d0 7487a04 5277f37 7487a04 67c52d0 7487a04 | 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | 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 |