matplotlib.pyplot

2021. 9. 3. 17:02Python/Visualization

pd.options.display.float_format = '{:,.2f}'.format

pred_lasso = pd.DataFrame(lasso.predict(x_test), index=x_test.index, columns=['pred_lasso'])
pred_ridge = pd.DataFrame(ridge.predict(x_test), index=x_test.index, columns=['pred ridge'])


# ========================================================================================
# LASSO
df_plot = pd.concat([y_test, pred_lasso], axis=1).sort_index()

fig, ax = plt.subplots(figsize=(20, 8))
fig.set_facecolor("lightgrey")
ax.plot(df_plot.index, df_plot.iloc[:, 0], label='Net 3y', linewidth=1.5, color='r')
ax.plot(df_plot.index, df_plot.iloc[:, 1], label='Pred LASSO', linewidth=1.5, color='orange')
ax.set_title('LASSO Prediction', fontdict={'fontsize':25})
ax.legend(['Net 3y', 'Pred LASSO'], loc='best', fontsize=15)
ax.tick_params(axis='both', labelsize=15)
ax.set_xlabel('Date', fontsize=15)
ax.set_ylabel('Net 3y', fontsize=15)
ax.axhline(0, linestyle='--', color='grey')
ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
plt.show()

lasso_coef = pd.DataFrame(lasso.coef_, index=cols, columns=['LASSO Coef'])
display(lasso_coef)
# ========================================================================================

plt.clf()

# ========================================================================================
# Ridge
df_plot = pd.concat([y_test, pred_ridge], axis=1).sort_index()

fig, ax = plt.subplots(figsize=(20, 8))
fig.set_facecolor("lightgrey")
ax.plot(df_plot.index, df_plot.iloc[:, 0], label='Net 3y', linewidth=1.5, color='r')
ax.plot(df_plot.index, df_plot.iloc[:, 1], label='Pred Ridge', linewidth=1.5, color='orange')
ax.set_title('Ridge Prediction', fontdict={'fontsize':25})
ax.legend(['Net 3y', 'Pred Ridge'], loc='best', fontsize=15)
ax.tick_params(axis='both', labelsize=15)
ax.set_xlabel('Date', fontsize=15)
ax.set_ylabel('Net 3y', fontsize=15)
ax.axhline(0, linestyle='--', color='grey')
ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
plt.show()

ridge_coef = pd.DataFrame(ridge.coef_.reshape(-1, 1), index=cols, columns=['Ridge Coef'])
display(ridge_coef)
# ========================================================================================

coef = pd.concat([lasso_coef, ridge_coef], axis=1)
display(coef)

LASSO

 

Ridge