|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +# In[2]: |
| 7 | + |
| 8 | + |
| 9 | +# this definition exposes all python module imports that should be available in all subsequent commands |
| 10 | +import json |
| 11 | +import numpy as np |
| 12 | +import pandas as pd |
| 13 | +import seaborn as sns |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +# ... |
| 16 | +# global constants |
| 17 | +MODEL_DIRECTORY = "/srv/app/model/data/" |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +# In[4]: |
| 27 | + |
| 28 | + |
| 29 | +# this cell is not executed from MLTK and should only be used for staging data into the notebook environment |
| 30 | +def stage(name): |
| 31 | + with open("data/"+name+".csv", 'r') as f: |
| 32 | + df = pd.read_csv(f) |
| 33 | + with open("data/"+name+".json", 'r') as f: |
| 34 | + param = json.load(f) |
| 35 | + return df, param |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +# In[6]: |
| 45 | + |
| 46 | + |
| 47 | +# initialize your model |
| 48 | +# available inputs: data and parameters |
| 49 | +# returns the model object which will be used as a reference to call fit, apply and summary subsequently |
| 50 | +def init(df,param): |
| 51 | + model = {} |
| 52 | + return model |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +# In[8]: |
| 62 | + |
| 63 | + |
| 64 | +# train your model |
| 65 | +# returns a fit info json object and may modify the model object |
| 66 | +def fit(model,df,param): |
| 67 | + # model.fit() |
| 68 | + info = {"message": "no fit needed"} |
| 69 | + return info |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +# In[10]: |
| 79 | + |
| 80 | + |
| 81 | +# apply your model |
| 82 | +# returns the calculated results |
| 83 | +def plot_to_base64(plot): |
| 84 | + import base64 |
| 85 | + import io |
| 86 | + pic_IObytes = io.BytesIO() |
| 87 | + if hasattr(plot,'fig'): |
| 88 | + plot.fig.savefig(pic_IObytes, format='png') |
| 89 | + elif hasattr(plot,'figure'): |
| 90 | + plot.figure.savefig(pic_IObytes, format='png') |
| 91 | + pic_IObytes.seek(0) |
| 92 | + pic_hash = base64.b64encode(pic_IObytes.read()) |
| 93 | + return pic_hash |
| 94 | + |
| 95 | + |
| 96 | +def plot_pairplot_as_base64(df,param): |
| 97 | + hue=None |
| 98 | + if 'options' in param: |
| 99 | + if 'target_variable' in param['options']: |
| 100 | + hue=str(param['options']['target_variable'][0]) |
| 101 | + plot = sns.pairplot(df,hue=hue, palette="husl") |
| 102 | + return str(plot_to_base64(plot)) |
| 103 | + |
| 104 | + |
| 105 | +def plot_correlationmatrix_as_base64(corr): |
| 106 | + # Set up the matplotlib figure |
| 107 | + f, ax = plt.subplots(figsize=(15, 15)) |
| 108 | + # Generate a mask for the upper triangle |
| 109 | + mask = np.triu(np.ones_like(corr, dtype=np.bool)) |
| 110 | + # Generate a custom diverging colormap |
| 111 | + cmap = sns.diverging_palette(250, 10, as_cmap=True) |
| 112 | + # Draw the heatmap with the mask and correct aspect ratio |
| 113 | + #plot = sns.heatmap(corr, mask=mask, cmap="Spectral", vmax=1.0, center=0, square=True, linewidths=.5, cbar_kws={"shrink": .5}) |
| 114 | + plot = sns.heatmap(corr, cmap="Spectral", vmax=1.0, center=0, square=True, linewidths=.5, cbar_kws={"shrink": .5}) |
| 115 | + #plot.figure.savefig("plot.png", format='png') |
| 116 | + return str(plot_to_base64(plot)) |
| 117 | + |
| 118 | +def apply(model,df,param): |
| 119 | + # param['options']['model_name'] |
| 120 | + dfeatures = df[param['feature_variables']] |
| 121 | + result = dfeatures.corr() #.reset_index() |
| 122 | + if 'plot' in param['options']['params']: |
| 123 | + plots = param['options']['params']['plot'].lstrip("\"").rstrip("\"").lower().split(',') |
| 124 | + for plot in plots: |
| 125 | + if plot=='matrix': |
| 126 | + model["plot_matrix"] = plot_correlationmatrix_as_base64(result) |
| 127 | + elif plot=='pairplot': |
| 128 | + model["plot_pairplot"] = plot_pairplot_as_base64(df,param) |
| 129 | + else: |
| 130 | + continue |
| 131 | + |
| 132 | + return result |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +# In[12]: |
| 142 | + |
| 143 | + |
| 144 | +# save model to name in expected convention "<algo_name>_<model_name>" |
| 145 | +def save(model,name): |
| 146 | + with open(MODEL_DIRECTORY + name + ".json", 'w') as file: |
| 147 | + json.dump(model, file) |
| 148 | + return model |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +# In[14]: |
| 158 | + |
| 159 | + |
| 160 | +# load model from name in expected convention "<algo_name>_<model_name>" |
| 161 | +def load(name): |
| 162 | + model = {} |
| 163 | + with open(MODEL_DIRECTORY + name + ".json", 'r') as file: |
| 164 | + model = json.load(file) |
| 165 | + return model |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +# In[19]: |
| 175 | + |
| 176 | + |
| 177 | +# return a model summary |
| 178 | +def summary(model=None): |
| 179 | + returns = {"version": {"numpy": np.__version__, "pandas": pd.__version__} } |
| 180 | + return returns |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
0 commit comments