Skip to content

Instantly share code, notes, and snippets.

@ikanez
Created July 13, 2019 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikanez/7a11cb83636443a7906d6ca4efd6f6d4 to your computer and use it in GitHub Desktop.
Save ikanez/7a11cb83636443a7906d6ca4efd6f6d4 to your computer and use it in GitHub Desktop.
Find best parameter for sarimax model and log useful stuff into MLflow
# Initial approximation of parameters
Qs = range(0, 2)
qs = range(0, 3)
Ps = range(0, 3)
ps = range(0, 3)
D=1
d=1
parameters = product(ps, qs, Ps, Qs)
parameters_list = list(parameters)
len(parameters_list)
# Model Selection
results = []
best_aic = float("inf")
warnings.filterwarnings('ignore')
for param in parameters_list:
# start mlflow run
with mlflow.start_run(run_name='sarimax_param'):
# log parameters
mlflow.log_param('order-Qs', param[0])
mlflow.log_param('order-qs', param[1])
mlflow.log_param('seasonal-order-Ps', param[2])
mlflow.log_param('seasonal-order-ps', param[3])
try:
# model = SARIMAX(btc_month.close_box, order=(param[0], d, param[1]), seasonal_order=(param[2], D, param[3], 12)).fit(disp=-1)
model = SARIMAX(btc_month.close_box, order=(param[0], d, param[1]), seasonal_order=(param[2], D, param[3], 4)).fit(disp=-1)
except ValueError:
print('bad parameter combination:', param)
continue
aic = model.aic
if aic < best_aic:
best_model = model
best_aic = aic
best_param = param
results.append([param, model.aic])
# log metric
mlflow.log_metric('aic',aic)
mlflow.log_metric('dickey-fuller-test',adfuller(model.resid[13:])[1])
# log artifact: model summary
plt.rc('figure', figsize=(12, 7))
plt.text(0.01, 0.05, str(model.summary()), {'fontsize': 10}, fontproperties = 'monospace')
plt.axis('off')
plt.tight_layout()
summary_fn = 'model_sarimax_summary_{}_{}_{}_{}.png'.format(param[0],param[1],param[2],param[3])
plt.savefig(summary_fn)
mlflow.log_artifact(summary_fn) # logging to mlflow
plt.close()
# log artifact: diagnostics plot
model.plot_diagnostics(figsize=(15, 12))
fig1_fn = 'figure_diagnostics_{}_{}_{}_{}.png'.format(param[0],param[1],param[2],param[3])
plt.savefig(fig1_fn)
mlflow.log_artifact(fig1_fn) # logging to mlflow
plt.close()
# log artifact: residuals and pacf plot
plt.subplot(211)
model.resid[13:].plot()
plt.ylabel(u'Residuals')
ax = plt.subplot(212)
plot_acf(model.resid[13:].values.squeeze(), lags=12, ax=ax)
plt.tight_layout()
fig2_fn = 'figure_res_pacf_{}_{}_{}_{}.png'.format(param[0],param[1],param[2],param[3])
plt.savefig(fig2_fn)
mlflow.log_artifact(fig2_fn) # logging to mlflow
plt.close()
@theagut27
Copy link

product isn't defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment