learn/learnme.py
def main():
    """
    Given training data, this script performs a user-defined number of ML 
    tasks for three algorithms (kNN, decision trees, SVR), saving the results
    as .csv files:
    1. cv_predict provides the prediction of each training instance from when
       it was in the CV fold for testing
    2. errors_and_scores provides the prediction accuracy for each algorithm
       for each CV fold
    3. learning_curves provides the prediction accuracy with respect to
       training set size
    4. validation_curves provides the prediction accuracy with respect to
       algorithm hyperparameter variations
    5. ext_test_compare provides the predictions of each algorithm of an 
       external test set
    6. int_test_compare provides the predictions of each algorithm of an 
       from a single split-off-from-DB test set
    7. random_error calculates prediction performance with respect to 
       increasing error

    """
    
    args = parse_args(sys.argv[1:])
    
    CV = args.cv
    alg = args.alg
    tset_frac = args.tset_frac
    csv_name =  args.outfile
    lbls = ['ReactorType', 'CoolingTime', 'Enrichment', 'Burnup', 'OrigenReactor']
    nonlbls = ['AvgPowerDensity', 'ModDensity', 'UiWeight']
    
    trainXY = pd.read_pickle(args.train_db)
    #trainXY.reset_index(inplace=True, drop=True) 
    if tset_frac < 1.0:
        trainXY = trainXY.sample(frac=tset_frac)
    trainX_unscaled, rY, cY, eY, bY = splitXY(trainXY)
    if ((args.learn_curves == True) or (args.valid_curves == True)):
        trainX = scale(trainX_unscaled)
    
    # set ground truth 
    trainY = pd.Series()
    if args.rxtr_param == 'cooling':
        trainY = cY
    elif args.rxtr_param == 'enrichment': 
        trainY = eY
    elif args.rxtr_param == 'burnup':
        trainY = bY
    else:
        trainY = rY

    # get hyperparams
    #if args.ext_test_compare == True:
    #    # hacking the use of these arguments, sorry future me
    #    k, depth, feats, g, c = get_sfco_hyperparam(tset_frac, CV)
    #else:
    k, depth, feats, g, c = get_hyperparam(args.rxtr_param, args.train_db)
        
    ## initialize learners
    score = 'neg_mean_absolute_error'
    kfold = KFold(n_splits=CV, shuffle=True)
    knn_init = KNeighborsRegressor(n_neighbors=k, weights='distance', p=1, metric='minkowski')
    dtr_init = DecisionTreeRegressor(max_depth=depth, max_features=feats)
    svr_init = SVR(gamma=g, C=c)
    if args.rxtr_param == 'reactor':
        score = make_scorer(balanced_accuracy_score, adjusted=True)
        kfold = StratifiedKFold(n_splits=CV, shuffle=True)
        knn_init = KNeighborsClassifier(n_neighbors=k, weights='distance', p=1, metric='minkowski')
        dtr_init = DecisionTreeClassifier(max_depth=depth, max_features=feats, class_weight='balanced')
        svr_init = SVC(gamma=g, C=c, class_weight='balanced')
    
    if alg == 'knn':
        init = knn_init
    elif alg == 'dtree':
        init = dtr_init
    else:
        init = svr_init

    ## create test set from train set (no CV)
    if args.int_test_compare == True:
        int_test_compare(trainX_unscaled, trainY, alg, init, csv_name, tset_frac, args.train_db, args.rxtr_param)

    ## calculate errors and scores
    if args.err_n_scores == True:
        errors_and_scores(trainX_unscaled, trainY, alg, init, score, kfold, csv_name, args.train_db)

    ## return all CV predictions
    if args.cv_pred == True:
        cv_predict(trainX_unscaled, trainY, alg, init, kfold, csv_name, args.train_db, args.rxtr_param)

    # learning curves
    if args.learn_curves == True:
        learning_curves(trainX, trainY, alg, init, kfold, score, csv_name)
    
    # compare against external test set
    if args.ext_test_compare == True:
        # convert trainset to be same units as testset
        trainX_unscaled = convert_g_to_mgUi(trainX_unscaled)
        xy_cols = trainXY.columns.tolist()
        for col in nonlbls: xy_cols.remove(col)
        testX_unscaled, testY = get_testsetXY(args.testing_set, xy_cols, args.rxtr_param)
        # scale testset using scale fit from trainset
        scaler = StandardScaler().fit(trainX_unscaled)
        trainX = scaler.transform(trainX_unscaled)
        testX = scaler.transform(testX_unscaled)
        ext_test_compare(trainX, trainY, testX, testY, alg, init, csv_name, args.rxtr_param)

    # pred results wrt random error
    if args.random_error == True:
        random_error(trainX_unscaled, trainY, alg, init, kfold, csv_name, args.rxtr_param)

    # validation curves 
    if args.valid_curves == True:
        # VC needs different inits
        knn_init = KNeighborsRegressor(weights='distance', p=1, metric='minkowski')
        dtr_init = DecisionTreeRegressor()
        svr_init = SVR(gamma='scale', C=c)
        if args.rxtr_param == 'reactor':
            knn_init = KNeighborsClassifier(weights='distance', p=1, metric='minkowski')
            dtr_init = DecisionTreeClassifier(class_weight='balanced')
            svr_init = SVC(gamma='scale', C=c, class_weight='balanced')
        if alg == 'knn':
            init = knn_init
        elif alg == 'dtree':
            init = dtr_init
        else:
            init = svr_init
        validation_curves(trainX, trainY, alg, init, kfold, score, csv_name)
    
    return

    Given training data, this script performs a user-defined number of ML 
    tasks for three algorithms (kNN, decision trees, SVR), saving the results
    as .csv files:
    1. cv_predict provides the prediction of each training instance from when
       it was in the CV fold for testing
    2. errors_and_scores provides the prediction accuracy for each algorithm
       for each CV fold
    3. learning_curves provides the prediction accuracy with respect to
       training set size
    4. validation_curves provides the prediction accuracy with respect to
       algorithm hyperparameter variations
    5. ext_test_compare provides the predictions of each algorithm of an 
       external test set
    6. int_test_compare provides the predictions of each algorithm of an 
       from a single split-off-from-DB test set
    7. random_error calculates prediction performance with respect to 
       increasing error

    
Given training data, this script performs a user-defined number of ML
tasks for three algorithms (kNN, decision trees, SVR), saving the results
as .csv files:
1. cv_predict provides the prediction of each training instance from when
it was in the CV fold for testing
2. errors_and_scores provides the prediction accuracy for each algorithm
for each CV fold
3. learning_curves provides the prediction accuracy with respect to
training set size
4. validation_curves provides the prediction accuracy with respect to
algorithm hyperparameter variations
5. ext_test_compare provides the predictions of each algorithm of an
external test set
6. int_test_compare provides the predictions of each algorithm of an
from a single split-off-from-DB test set
7. random_error calculates prediction performance with respect to
increasing error
Given training data this script performs a user - defined number of ML tasks for three algorithms ( kNN decision trees SVR ) saving the results as . csv files : 1 . cv_predict provides the prediction of each training instance from when it was in the CV fold for testing 2 . errors_and_scores provides the prediction accuracy for each algorithm for each CV fold 3 . learning_curves provides the prediction accuracy with respect to training set size 4 . validation_curves provides the prediction accuracy with respect to algorithm hyperparameter variations 5 . ext_test_compare provides the predictions of each algorithm of an external test set 6 . int_test_compare provides the predictions of each algorithm of an from a single split - off - from - DB test set 7 . random_error calculates prediction performance with respect to increasing error
def main():
    args = parse_args(sys.argv[1:])
    CV = args.cv
    alg = args.alg
    tset_frac = args.tset_frac
    csv_name =  args.outfile
    lbls = ['ReactorType', 'CoolingTime', 'Enrichment', 'Burnup', 'OrigenReactor']
    nonlbls = ['AvgPowerDensity', 'ModDensity', 'UiWeight']
    trainXY = pd.read_pickle(args.train_db)
    if tset_frac < 1.0:
        trainXY = trainXY.sample(frac=tset_frac)
    trainX_unscaled, rY, cY, eY, bY = splitXY(trainXY)
    if ((args.learn_curves == True) or (args.valid_curves == True)):
        trainX = scale(trainX_unscaled)
    trainY = pd.Series()
    if args.rxtr_param == 'cooling':
        trainY = cY
    elif args.rxtr_param == 'enrichment': 
        trainY = eY
    elif args.rxtr_param == 'burnup':
        trainY = bY
    else:
        trainY = rY
    k, depth, feats, g, c = get_hyperparam(args.rxtr_param, args.train_db)
    score = 'neg_mean_absolute_error'
    kfold = KFold(n_splits=CV, shuffle=True)
    knn_init = KNeighborsRegressor(n_neighbors=k, weights='distance', p=1, metric='minkowski')
    dtr_init = DecisionTreeRegressor(max_depth=depth, max_features=feats)
    svr_init = SVR(gamma=g, C=c)
    if args.rxtr_param == 'reactor':
        score = make_scorer(balanced_accuracy_score, adjusted=True)
        kfold = StratifiedKFold(n_splits=CV, shuffle=True)
        knn_init = KNeighborsClassifier(n_neighbors=k, weights='distance', p=1, metric='minkowski')
        dtr_init = DecisionTreeClassifier(max_depth=depth, max_features=feats, class_weight='balanced')
        svr_init = SVC(gamma=g, C=c, class_weight='balanced')
    if alg == 'knn':
        init = knn_init
    elif alg == 'dtree':
        init = dtr_init
    else:
        init = svr_init
    if args.int_test_compare == True:
        int_test_compare(trainX_unscaled, trainY, alg, init, csv_name, tset_frac, args.train_db, args.rxtr_param)
    if args.err_n_scores == True:
        errors_and_scores(trainX_unscaled, trainY, alg, init, score, kfold, csv_name, args.train_db)
    if args.cv_pred == True:
        cv_predict(trainX_unscaled, trainY, alg, init, kfold, csv_name, args.train_db, args.rxtr_param)
    if args.learn_curves == True:
        learning_curves(trainX, trainY, alg, init, kfold, score, csv_name)
    if args.ext_test_compare == True:
        trainX_unscaled = convert_g_to_mgUi(trainX_unscaled)
        xy_cols = trainXY.columns.tolist()
        for col in nonlbls: xy_cols.remove(col)
        testX_unscaled, testY = get_testsetXY(args.testing_set, xy_cols, args.rxtr_param)
        scaler = StandardScaler().fit(trainX_unscaled)
        trainX = scaler.transform(trainX_unscaled)
        testX = scaler.transform(testX_unscaled)
        ext_test_compare(trainX, trainY, testX, testY, alg, init, csv_name, args.rxtr_param)
    if args.random_error == True:
        random_error(trainX_unscaled, trainY, alg, init, kfold, csv_name, args.rxtr_param)
    if args.valid_curves == True:
        knn_init = KNeighborsRegressor(weights='distance', p=1, metric='minkowski')
        dtr_init = DecisionTreeRegressor()
        svr_init = SVR(gamma='scale', C=c)
        if args.rxtr_param == 'reactor':
            knn_init = KNeighborsClassifier(weights='distance', p=1, metric='minkowski')
            dtr_init = DecisionTreeClassifier(class_weight='balanced')
            svr_init = SVC(gamma='scale', C=c, class_weight='balanced')
        if alg == 'knn':
            init = knn_init
        elif alg == 'dtree':
            init = dtr_init
        else:
            init = svr_init
        validation_curves(trainX, trainY, alg, init, kfold, score, csv_name)
    return
def main ( ) : args = parse_args ( sys . argv [ 1 : ] ) CV = args . cv alg = args . alg tset_frac = args . tset_frac csv_name = args . outfile lbls = [ 'ReactorType' , 'CoolingTime' , 'Enrichment' , 'Burnup' , 'OrigenReactor' ] nonlbls = [ 'AvgPowerDensity' , 'ModDensity' , 'UiWeight' ] trainXY = pd . read_pickle ( args . train_db ) if tset_frac < 1.0 : trainXY = trainXY . sample ( frac = tset_frac ) trainX_unscaled , rY , cY , eY , bY = splitXY ( trainXY ) if ( ( args . learn_curves == True ) or ( args . valid_curves == True ) ) : trainX = scale ( trainX_unscaled ) trainY = pd . Series ( ) if args . rxtr_param == 'cooling' : trainY = cY elif args . rxtr_param == 'enrichment' : trainY = eY elif args . rxtr_param == 'burnup' : trainY = bY else : trainY = rY k , depth , feats , g , c = get_hyperparam ( args . rxtr_param , args . train_db ) score = 'neg_mean_absolute_error' kfold = KFold ( n_splits = CV , shuffle = True ) knn_init = KNeighborsRegressor ( n_neighbors = k , weights = 'distance' , p = 1 , metric = 'minkowski' ) dtr_init = DecisionTreeRegressor ( max_depth = depth , max_features = feats ) svr_init = SVR ( gamma = g , C = c ) if args . rxtr_param == 'reactor' : score = make_scorer ( balanced_accuracy_score , adjusted = True ) kfold = StratifiedKFold ( n_splits = CV , shuffle = True ) knn_init = KNeighborsClassifier ( n_neighbors = k , weights = 'distance' , p = 1 , metric = 'minkowski' ) dtr_init = DecisionTreeClassifier ( max_depth = depth , max_features = feats , class_weight = 'balanced' ) svr_init = SVC ( gamma = g , C = c , class_weight = 'balanced' ) if alg == 'knn' : init = knn_init elif alg == 'dtree' : init = dtr_init else : init = svr_init if args . int_test_compare == True : int_test_compare ( trainX_unscaled , trainY , alg , init , csv_name , tset_frac , args . train_db , args . rxtr_param ) if args . err_n_scores == True : errors_and_scores ( trainX_unscaled , trainY , alg , init , score , kfold , csv_name , args . train_db ) if args . cv_pred == True : cv_predict ( trainX_unscaled , trainY , alg , init , kfold , csv_name , args . train_db , args . rxtr_param ) if args . learn_curves == True : learning_curves ( trainX , trainY , alg , init , kfold , score , csv_name ) if args . ext_test_compare == True : trainX_unscaled = convert_g_to_mgUi ( trainX_unscaled ) xy_cols = trainXY . columns . tolist ( ) for col in nonlbls : xy_cols . remove ( col ) testX_unscaled , testY = get_testsetXY ( args . testing_set , xy_cols , args . rxtr_param ) scaler = StandardScaler ( ) . fit ( trainX_unscaled ) trainX = scaler . transform ( trainX_unscaled ) testX = scaler . transform ( testX_unscaled ) ext_test_compare ( trainX , trainY , testX , testY , alg , init , csv_name , args . rxtr_param ) if args . random_error == True : random_error ( trainX_unscaled , trainY , alg , init , kfold , csv_name , args . rxtr_param ) if args . valid_curves == True : knn_init = KNeighborsRegressor ( weights = 'distance' , p = 1 , metric = 'minkowski' ) dtr_init = DecisionTreeRegressor ( ) svr_init = SVR ( gamma = 'scale' , C = c ) if args . rxtr_param == 'reactor' : knn_init = KNeighborsClassifier ( weights = 'distance' , p = 1 , metric = 'minkowski' ) dtr_init = DecisionTreeClassifier ( class_weight = 'balanced' ) svr_init = SVC ( gamma = 'scale' , C = c , class_weight = 'balanced' ) if alg == 'knn' : init = knn_init elif alg == 'dtree' : init = dtr_init else : init = svr_init validation_curves ( trainX , trainY , alg , init , kfold , score , csv_name ) return
Given training data, this script performs a user-defined number of ML
tasks for three algorithms (kNN, decision trees, SVR), saving the results
as .csv files:
1. cv_predict provides the prediction of each training instance from when
it was in the CV fold for testing
2. errors_and_scores provides the prediction accuracy for each algorithm
for each CV fold
3. learning_curves provides the prediction accuracy with respect to
training set size
4. validation_curves provides the prediction accuracy with respect to
algorithm hyperparameter variations
5. ext_test_compare provides the predictions of each algorithm of an
external test set
6. int_test_compare provides the predictions of each algorithm of an
from a single split-off-from-DB test set
7. random_error calculates prediction performance with respect to
increasing error
Given training data this script performs a user - defined number of ML tasks for three algorithms ( kNN decision trees SVR ) saving the results as . csv files : 1 . cv_predict provides the prediction of each training instance from when it was in the CV fold for testing 2 . errors_and_scores provides the prediction accuracy for each algorithm for each CV fold 3 . learning_curves provides the prediction accuracy with respect to training set size 4 . validation_curves provides the prediction accuracy with respect to algorithm hyperparameter variations 5 . ext_test_compare provides the predictions of each algorithm of an external test set 6 . int_test_compare provides the predictions of each algorithm of an from a single split - off - from - DB test set 7 . random_error calculates prediction performance with respect to increasing error
"""
    Given training data, this script performs a user-defined number of ML 
    tasks for three algorithms (kNN, decision trees, SVR), saving the results
    as .csv files:
    1. cv_predict provides the prediction of each training instance from when
       it was in the CV fold for testing
    2. errors_and_scores provides the prediction accuracy for each algorithm
       for each CV fold
    3. learning_curves provides the prediction accuracy with respect to
       training set size
    4. validation_curves provides the prediction accuracy with respect to
       algorithm hyperparameter variations
    5. ext_test_compare provides the predictions of each algorithm of an 
       external test set
    6. int_test_compare provides the predictions of each algorithm of an 
       from a single split-off-from-DB test set
    7. random_error calculates prediction performance with respect to 
       increasing error

    """ #trainXY.reset_index(inplace=True, drop=True)  # set ground truth  # get hyperparams #if args.ext_test_compare == True: #    # hacking the use of these arguments, sorry future me #    k, depth, feats, g, c = get_sfco_hyperparam(tset_frac, CV) #else: ## initialize learners ## create test set from train set (no CV) ## calculate errors and scores ## return all CV predictions # learning curves # compare against external test set # convert trainset to be same units as testset # scale testset using scale fit from trainset # pred results wrt random error # validation curves  # VC needs different inits