learn/optimize_hyperparameters.py
def opt_knn(grid, trainX, trainY, arg):
    """
    Optimizes the k nearest neighbor algorithm, prints the top score and 
    hyperparameter in a text file.

    Parameters
    ----------
    grid : a dict of hyperparameters and range of them to optimize over
    trainX : feature set of nuclide measurements
    trainY : parameter being predicted (e.g., burnup, cooling time)
    arg : a dict of various parameters needed for GridSearchCV and output file

    """
    knn_init = KNeighborsRegressor(weights='distance', p=1, metric='minkowski')
    if arg['pred'] == 'reactor': 
        knn_init = KNeighborsClassifier(weights='distance', p=1, metric='minkowski')
    knn_opt = GridSearchCV(estimator=knn_init, param_grid=grid, 
                           scoring=arg['score'], n_jobs=arg['jobs'], 
                           cv=arg['kfold'], return_train_score=True)
    knn_opt.fit(trainX, trainY) 
    
    tmpl = Template(
'''
score: $score
n_neighbors: $k
''')
    txt = tmpl.substitute(score = knn_opt.best_score_,
                          k = knn_opt.best_params_['n_neighbors'])                  
    with open(arg['file'], 'a') as f:
        f.write(txt)
    return

    Optimizes the k nearest neighbor algorithm, prints the top score and 
    hyperparameter in a text file.

    Parameters
    ----------
    grid : a dict of hyperparameters and range of them to optimize over
    trainX : feature set of nuclide measurements
    trainY : parameter being predicted (e.g., burnup, cooling time)
    arg : a dict of various parameters needed for GridSearchCV and output file

    
Optimizes the k nearest neighbor algorithm, prints the top score and
hyperparameter in a text file.

Parameters

grid : a dict of hyperparameters and range of them to optimize over
trainX : feature set of nuclide measurements
trainY : parameter being predicted
arg : a dict of various parameters needed for GridSearchCV and output file
Optimizes the k nearest neighbor algorithm prints the top score and hyperparameter in a text file . Parameters grid : a dict of hyperparameters and range of them to optimize over trainX : feature set of nuclide measurements trainY : parameter being predicted arg : a dict of various parameters needed for GridSearchCV and output file
def opt_knn(grid, trainX, trainY, arg):
    knn_init = KNeighborsRegressor(weights='distance', p=1, metric='minkowski')
    if arg['pred'] == 'reactor': 
        knn_init = KNeighborsClassifier(weights='distance', p=1, metric='minkowski')
    knn_opt = GridSearchCV(estimator=knn_init, param_grid=grid, 
                           scoring=arg['score'], n_jobs=arg['jobs'], 
                           cv=arg['kfold'], return_train_score=True)
    knn_opt.fit(trainX, trainY) 
    tmpl = Template(
'''
score: $score
n_neighbors: $k
''')
    txt = tmpl.substitute(score = knn_opt.best_score_,
                          k = knn_opt.best_params_['n_neighbors'])                  
    with open(arg['file'], 'a') as f:
        f.write(txt)
    return
def opt_knn ( grid , trainX , trainY , arg ) : knn_init = KNeighborsRegressor ( weights = 'distance' , p = 1 , metric = 'minkowski' ) if arg [ 'pred' ] == 'reactor' : knn_init = KNeighborsClassifier ( weights = 'distance' , p = 1 , metric = 'minkowski' ) knn_opt = GridSearchCV ( estimator = knn_init , param_grid = grid , scoring = arg [ 'score' ] , n_jobs = arg [ 'jobs' ] , cv = arg [ 'kfold' ] , return_train_score = True ) knn_opt . fit ( trainX , trainY ) tmpl = Template ( '''
score: $score
n_neighbors: $k
''' ) txt = tmpl . substitute ( score = knn_opt . best_score_ , k = knn_opt . best_params_ [ 'n_neighbors' ] ) with open ( arg [ 'file' ] , 'a' ) as f : f . write ( txt ) return
Optimizes the k nearest neighbor algorithm, prints the top score and
hyperparameter in a text file.
Optimizes the k nearest neighbor algorithm prints the top score and hyperparameter in a text file .
"""
    Optimizes the k nearest neighbor algorithm, prints the top score and 
    hyperparameter in a text file.

    Parameters
    ----------
    grid : a dict of hyperparameters and range of them to optimize over
    trainX : feature set of nuclide measurements
    trainY : parameter being predicted (e.g., burnup, cooling time)
    arg : a dict of various parameters needed for GridSearchCV and output file

    """
grid
trainX
trainY
arg