In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
# color dict (use: colors['XColor'])

colors = {'DBrwn' : '#8c510a', 'MBrwn' : '#d8b365', 'LBrwn' : '#f6e8c3',
          'DTeal' : '#01665e', 'MTeal' : '#5ab4ac', 'LTeal' : '#c7eae5',
          'DPurp' : '#762a83', 'MPurp' : '#af8dc3', 'LPurp' : '#e7d4e8'
         }

# colors for seaborn
dark = ["#8c510a", "#01665e", "#762a83"]
med = ["#d8b365", "#5ab4ac", "#af8dc3"]

DEFAULT VALUES

for 2-param algoritms

19Jan2020

Reactor Prediction

gamma is 1/n_features, so 0.067 and C is 1000 for this run

(updates to be different default C values since this run are in learn-me script)

max depth/max features are defaulted to be None

Burnup Prediction

gamma is 1/n_features, so 0.067 and C is 42000 for this run

(updates to be different default C values since this run are in learn-me script)

max depth/max features are defaulted to be None


In [3]:
burn_vc = pd.read_csv('../results/19jan2020_15nuc/15nuc_m60_burnup_validation_curve.csv')
#enri_vc = pd.read_csv('../results/19jan2020_15nuc/15nuc_m60_enrichment_validation_curve.csv')
#cool_vc = pd.read_csv('../results/19jan2020_15nuc/15nuc_m60_cooling_validation_curve.csv')
rxtr_vc = pd.read_csv('../results/19jan2020_15nuc/15nuc_m60_reactor_validation_curve.csv')

In [4]:
burn_vc.loc[:, 'Parameter'] = 'Burnup [MWd/MTU]'
#enri_vc.loc[:, 'Parameter'] = 'Enrichment [% U235]'
#cool_vc.loc[:, 'Parameter'] = 'Cooling Time [Days]'
rxtr_vc.loc[:, 'Parameter'] = 'Reactor Type'

In [37]:
burn_vc


Out[37]:
Unnamed: 0 ParamList TrainScore TrainStd CV-Score CV-Std Algorithm Parameter
0 0 1.000000 1.000000 0.000000e+00 0.998526 8.036042e-05 knn Burnup [MWd/MTU]
1 1 2.000000 1.000000 0.000000e+00 0.998946 6.884247e-05 knn Burnup [MWd/MTU]
2 2 3.000000 1.000000 0.000000e+00 0.999083 6.951710e-05 knn Burnup [MWd/MTU]
3 3 5.000000 1.000000 0.000000e+00 0.999180 6.693686e-05 knn Burnup [MWd/MTU]
4 4 6.000000 1.000000 0.000000e+00 0.999190 6.895660e-05 knn Burnup [MWd/MTU]
5 5 7.000000 1.000000 0.000000e+00 0.999186 8.301489e-05 knn Burnup [MWd/MTU]
6 6 9.000000 1.000000 0.000000e+00 0.999154 9.287476e-05 knn Burnup [MWd/MTU]
7 7 10.000000 1.000000 0.000000e+00 0.999146 1.029428e-04 knn Burnup [MWd/MTU]
8 8 11.000000 1.000000 0.000000e+00 0.999136 1.021932e-04 knn Burnup [MWd/MTU]
9 9 13.000000 1.000000 0.000000e+00 0.999123 1.247079e-04 knn Burnup [MWd/MTU]
10 10 14.000000 1.000000 0.000000e+00 0.999108 1.367949e-04 knn Burnup [MWd/MTU]
11 11 15.000000 1.000000 0.000000e+00 0.999100 1.456989e-04 knn Burnup [MWd/MTU]
12 12 17.000000 1.000000 0.000000e+00 0.999059 1.504893e-04 knn Burnup [MWd/MTU]
13 13 18.000000 1.000000 0.000000e+00 0.999048 1.592439e-04 knn Burnup [MWd/MTU]
14 14 20.000000 1.000000 0.000000e+00 0.999018 1.656141e-04 knn Burnup [MWd/MTU]
15 0 1.000000 0.763486 1.114747e-03 0.763307 4.468055e-03 dtree Burnup [MWd/MTU]
16 1 2.000000 0.938716 1.923808e-04 0.938648 7.909660e-04 dtree Burnup [MWd/MTU]
17 2 3.000000 0.985681 5.971596e-05 0.985611 2.357110e-04 dtree Burnup [MWd/MTU]
18 3 4.000000 0.996479 2.825737e-05 0.996425 1.315134e-04 dtree Burnup [MWd/MTU]
19 4 5.000000 0.999144 2.227230e-06 0.999107 2.823922e-05 dtree Burnup [MWd/MTU]
20 5 6.000000 0.999772 4.997120e-06 0.999740 1.834206e-05 dtree Burnup [MWd/MTU]
21 6 7.000000 0.999927 1.251868e-06 0.999902 6.017753e-06 dtree Burnup [MWd/MTU]
22 7 8.000000 0.999971 1.103917e-06 0.999945 3.530578e-06 dtree Burnup [MWd/MTU]
23 8 9.000000 0.999989 4.774609e-07 0.999961 6.429390e-06 dtree Burnup [MWd/MTU]
24 9 10.000000 0.999996 3.150047e-07 0.999970 5.392140e-06 dtree Burnup [MWd/MTU]
25 10 11.000000 0.999998 3.302107e-07 0.999969 6.764799e-06 dtree Burnup [MWd/MTU]
26 11 12.000000 0.999999 1.552622e-07 0.999972 3.207441e-06 dtree Burnup [MWd/MTU]
27 12 13.000000 1.000000 1.226764e-07 0.999973 3.755583e-06 dtree Burnup [MWd/MTU]
28 13 14.000000 1.000000 8.747542e-08 0.999971 6.062111e-06 dtree Burnup [MWd/MTU]
29 14 15.000000 1.000000 6.517686e-08 0.999973 3.582564e-06 dtree Burnup [MWd/MTU]
... ... ... ... ... ... ... ... ...
45 0 0.000100 0.999710 3.839227e-06 0.999708 2.745303e-05 svr Burnup [MWd/MTU]
46 1 0.000164 0.999816 2.538378e-06 0.999815 1.687493e-05 svr Burnup [MWd/MTU]
47 2 0.000268 0.999888 1.478616e-06 0.999887 1.152726e-05 svr Burnup [MWd/MTU]
48 3 0.000439 0.999938 1.115369e-06 0.999937 7.833554e-06 svr Burnup [MWd/MTU]
49 4 0.000720 0.999966 7.618783e-07 0.999966 5.028623e-06 svr Burnup [MWd/MTU]
50 5 0.001179 0.999978 6.593989e-07 0.999978 3.226065e-06 svr Burnup [MWd/MTU]
51 6 0.001931 0.999985 5.356198e-07 0.999984 1.914242e-06 svr Burnup [MWd/MTU]
52 7 0.003162 0.999990 2.787581e-07 0.999990 1.058534e-06 svr Burnup [MWd/MTU]
53 8 0.005179 0.999994 7.143048e-08 0.999994 6.504381e-07 svr Burnup [MWd/MTU]
54 9 0.008483 0.999996 9.382602e-08 0.999996 4.497553e-07 svr Burnup [MWd/MTU]
55 10 0.013895 0.999997 3.467707e-08 0.999997 2.914131e-07 svr Burnup [MWd/MTU]
56 11 0.022758 0.999998 4.830448e-08 0.999998 3.126634e-07 svr Burnup [MWd/MTU]
57 12 0.037276 0.999998 7.048672e-08 0.999998 3.152614e-07 svr Burnup [MWd/MTU]
58 13 0.061054 0.999998 7.390164e-08 0.999997 5.484298e-07 svr Burnup [MWd/MTU]
59 14 0.100000 0.999998 1.308654e-07 0.999996 7.550822e-07 svr Burnup [MWd/MTU]
60 0 1.000000 0.092593 3.610874e-04 0.092594 1.176757e-03 svr Burnup [MWd/MTU]
61 1 2.275846 0.202143 7.904848e-04 0.202146 2.520156e-03 svr Burnup [MWd/MTU]
62 2 5.179475 0.415716 1.485485e-03 0.415724 4.787493e-03 svr Burnup [MWd/MTU]
63 3 11.787686 0.694426 1.458385e-03 0.694451 6.995133e-03 svr Burnup [MWd/MTU]
64 4 26.826958 0.883252 1.017029e-03 0.883274 6.279906e-03 svr Burnup [MWd/MTU]
65 5 61.054023 0.959578 4.463929e-04 0.959513 3.999494e-03 svr Burnup [MWd/MTU]
66 6 138.949549 0.987613 1.161466e-04 0.987518 1.955816e-03 svr Burnup [MWd/MTU]
67 7 316.227766 0.996171 4.593976e-05 0.996105 8.065147e-04 svr Burnup [MWd/MTU]
68 8 719.685673 0.998810 2.480132e-05 0.998766 3.018349e-04 svr Burnup [MWd/MTU]
69 9 1637.893707 0.999635 1.315600e-05 0.999606 1.183077e-04 svr Burnup [MWd/MTU]
70 10 3727.593720 0.999920 5.176052e-06 0.999906 3.926274e-05 svr Burnup [MWd/MTU]
71 11 8483.428982 0.999981 5.157233e-07 0.999975 1.093687e-05 svr Burnup [MWd/MTU]
72 12 19306.977289 0.999995 2.758192e-07 0.999992 4.015296e-06 svr Burnup [MWd/MTU]
73 13 43939.705608 0.999999 6.591936e-08 0.999997 1.361506e-06 svr Burnup [MWd/MTU]
74 14 100000.000000 0.999999 2.736590e-08 0.999999 5.288684e-07 svr Burnup [MWd/MTU]

75 rows × 8 columns


In [5]:
for vc in [rxtr_vc, burn_vc]:#, cool_vc, enri_vc]:
    vc.loc[:, 'Hyperparameter'] = ''
    vc.iloc[0:15, -1] = 'n_neighbors'
    vc.iloc[15:30, -1] = 'max_depth'
    vc.iloc[30:45, -1] = 'max_features'
    vc.iloc[45:60, -1] = 'gamma'
    vc.iloc[60:75, -1] = 'C'

In [6]:
rxtr_vc.loc[rxtr_vc['Algorithm']=='knn', :]


Out[6]:
Unnamed: 0 ParamList TrainScore TrainStd CV-Score CV-Std Algorithm Parameter Hyperparameter
0 0 1.0 0.997311 0.000332 0.988707 0.001643 knn Reactor Type n_neighbors
1 1 3.0 0.997328 0.000316 0.985279 0.002476 knn Reactor Type n_neighbors
2 2 5.0 0.997294 0.000298 0.983195 0.003025 knn Reactor Type n_neighbors
3 3 7.0 0.997311 0.000296 0.981582 0.003167 knn Reactor Type n_neighbors
4 4 9.0 0.997328 0.000316 0.979901 0.002827 knn Reactor Type n_neighbors
5 5 11.0 0.997328 0.000316 0.978355 0.002924 knn Reactor Type n_neighbors
6 6 13.0 0.997294 0.000298 0.977280 0.003639 knn Reactor Type n_neighbors
7 7 15.0 0.997328 0.000316 0.975667 0.003750 knn Reactor Type n_neighbors
8 8 17.0 0.997311 0.000296 0.974389 0.002638 knn Reactor Type n_neighbors
9 9 19.0 0.997345 0.000312 0.973852 0.001825 knn Reactor Type n_neighbors
10 10 21.0 0.997328 0.000316 0.971836 0.002023 knn Reactor Type n_neighbors
11 11 23.0 0.997328 0.000316 0.971163 0.002058 knn Reactor Type n_neighbors
12 12 25.0 0.997328 0.000316 0.970222 0.002462 knn Reactor Type n_neighbors
13 13 27.0 0.997311 0.000332 0.969483 0.001699 knn Reactor Type n_neighbors
14 14 30.0 0.997311 0.000332 0.967870 0.001826 knn Reactor Type n_neighbors

Validation Curves by Prediction Type


In [7]:
def plot_validation_curve(df, title, xname, fig, ax):
    
    params = df['ParamList']
    train_scores_mean = df['TrainScore']
    train_scores_std = df['TrainStd']
    test_scores_mean = df['CV-Score']
    test_scores_std = df['CV-Std']
    
    #ax.set_ylabel('Score', fontsize=16)
    ax.set_title(title, fontsize=20)
    ax.set_xlabel(xname, fontsize=14)
    # for burnup
    #ax.set_xlim(250, 5000)
    # for everything else
    #ax.set_xlim(1000, 12100)
    ax.tick_params(labelsize=14)
    
    ax.fill_between(params, train_scores_mean - train_scores_std,
                    train_scores_mean + train_scores_std, color = med[0], 
                    alpha=0.2)
    ax.fill_between(params, test_scores_mean - test_scores_std,
                    test_scores_mean + test_scores_std, color = med[1], 
                    alpha=0.2)
    ax.plot(params, train_scores_mean, 'o-', color=med[0], mew=0, ms=5, 
            label="Training score")
    ax.plot(params, test_scores_mean, 'o-', color=med[1], mew=0, ms=5,
            label="Cross-validation score")
    
    return fig

In [25]:
def plot_by_prediction(df):
    #fig, alg_ax = plt.subplots(nrows=3, ncols=2, sharex=False, sharey=False, figsize=(15, 15))
    fig, alg_ax = plt.subplots(nrows=1, ncols=3, sharex=False, sharey=True, figsize=(17, 5))
    knn = ['knn', 'k-Nearest Neighbors', 'Neighbors (k)', 'n_neighbors', alg_ax[0]]
    dtree_d = ['dtree', 'Decision Trees', 'Max Depth', 'max_depth', alg_ax[1]]
    #dtree_f = ['dtree', 'Decision Trees', 'Max Features', 'max_features', alg_ax[1]]
    svr_g = ['svr', 'Support Vectors', 'Gamma', 'gamma', alg_ax[2]]
    #svr_c = ['svr', 'Support Vectors', 'C (misclassification penalty)', 'C', alg_ax[2]]
    for alg, title, xname, hyperp, ax in [knn, dtree_d, svr_g]:
        vc = df.loc[(df['Algorithm']==alg) & (df['Hyperparameter']==hyperp)] 
        plot_validation_curve(vc, title, xname, fig, ax)
    
    # invert some axes to all follow increasing model complexity along x axis
    alg_ax[0].invert_xaxis()
    #alg_ax[2].invert_xaxis()
    # set svr to log
    alg_ax[2].set_xscale("log")
    
    alg_ax[0].set_ylabel('Explained Variance', fontsize=16)
    ax.set_ylim(0.98, 1.001)
    parameter = df.loc[0, 'Parameter']
    title = 'Validation Curves using Three Algorithms: ' + parameter + ' Prediction'
    fig.suptitle(title, fontsize=24)
    fig.subplots_adjust(top=.8)
    alg_ax[1].legend(loc='center', bbox_to_anchor=(0.5, -0.23), ncol=2)
    fig.show()
    return fig

Reactor Type


In [16]:
plt.close(fig)
fig = plot_by_prediction(rxtr_vc)
fig.savefig('../results/19jan2020_15nuc/graphs/rxtr_validation.png', bbox_inches='tight', transparent=True)


/home/opotowsky/.local/lib/python3.6/site-packages/ipykernel_launcher.py:26: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.

Burnup


In [27]:
plt.close(fig)
fig = plot_by_prediction(burn_vc)
fig.savefig('../results/19jan2020_15nuc/graphs/burn_validation.png', bbox_inches='tight', transparent=True)


/home/opotowsky/.local/lib/python3.6/site-packages/ipykernel_launcher.py:26: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.

Enrichment


In [157]:
plt.close(fig)
fig = plot_by_prediction(enri_vc)
fig.savefig('../results/4oct2018/graphs/enri_validation.png', bbox_inches='tight', transparent=True)


/home/opy/.local/lib/python3.6/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

Cooling Time


In [158]:
plt.close(fig)
fig = plot_by_prediction(cool_vc)
fig.savefig('../results/4oct2018/graphs/cool_validation.png', bbox_inches='tight', transparent=True)


/home/opy/.local/lib/python3.6/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

Old stuff for reference


In [6]:
knn_b = burn_vc.loc[(burn_vc['Algorithm']=='knn')]# & (burn_vc['ScoringMetric']=='explained_variance')]
knn_e = enri_vc.loc[(enri_vc['Algorithm']=='knn')]# & (enri_vc['ScoringMetric']=='explained_variance')]
knn_c = cool_vc.loc[(cool_vc['Algorithm']=='knn')]# & (cool_vc['ScoringMetric']=='explained_variance')]
knn_r = rxtr_vc.loc[(rxtr_vc['Algorithm']=='knn')]# & (rxtr_vc['ScoringMetric']=='accuracy')]

In [10]:
plt.clf()
sns.set(style="ticks", font_scale=4)

df = pd.DataFrame()
df = pd.concat([knn_b, knn_e, knn_c, knn_r])
df = df.melt(id_vars = ['ParamList', 'Parameter'], 
             value_vars = ['TrainScore', 'CV-Score'], 
             value_name='Explained Variance/Accuracy', var_name='Curve Type')

vc = sns.FacetGrid(data=df, col="Parameter", hue="Curve Type", 
                   sharex=True, sharey=True, size=9, 
                   palette=sns.color_palette(med))
vc.map(plt.plot, 'ParamList', 'Explained Variance/Accuracy', linewidth=8)

(vc.set_axis_labels('k (n_neighbors)', 'Variance/Accuracy')
   .set(ylim=(0.7, 1.01))
   .set_titles('{col_name}')
   .add_legend()
   .fig.suptitle('kNN Validation Curves for Four Prediction Categories', fontsize=50))
plt.subplots_adjust(top=.8)

#vc.savefig('../results/22jul2018/graphs/3knn_fissact_validation.png', bbox_inches='tight', transparent=True)


<matplotlib.figure.Figure at 0x7fc9ceabcf60>

Mean Absolute Error


In [83]:
knn_b = burn_vc.loc[(burn_vc['Algorithm']=='knn')] (burn_vc['ScoringMetric']=='neg_mean_absolute_error')]
knn_e = enri_vc.loc[(enri_vc['Algorithm']=='knn')] (enri_vc['ScoringMetric']=='neg_mean_absolute_error')]
knn_c = cool_vc.loc[(cool_vc['Algorithm']=='knn')] (cool_vc['ScoringMetric']=='neg_mean_absolute_error')]

In [88]:
plt.clf()
sns.set(style="ticks", font_scale=4)

df = pd.DataFrame()
df = pd.concat([knn_b, knn_e, knn_c])
df = df.melt(id_vars = ['ParamList', 'Parameter'], 
             value_vars = ['TrainScore', 'CV-Score'], 
             value_name='MAE', var_name='Curve Type')

vc = sns.FacetGrid(data=df, col="Parameter", hue="Curve Type", 
                   sharex=True, sharey=False, size=9, 
                   palette=sns.color_palette(med))
vc.map(plt.plot, 'ParamList', 'MAE', linewidth=8)

(vc.set_axis_labels('k (n_neighbors)', 'Negative MAE')
   .set_titles('{col_name}')
   .add_legend()
   .fig.suptitle('kNN Validation Curves for Three Prediction Categories', fontsize=50))
plt.subplots_adjust(top=.8)
#vc.axes[0,0].set_ylim(-25, 1)
#vc.axes[0,1].set_ylim(-0.1, 0.006)
#vc.axes[0,2].set_ylim(-8, 0.5)

vc.savefig('../results/22jul2018/graphs/3knn_fissact_validation_mae.png', bbox_inches='tight', transparent=True)


<matplotlib.figure.Figure at 0x7efd3caae358>

In [117]: