old_scripts/alt_learnme.py
def main():
    """
    Given training data, this script trains and tracks each prediction for
    several algorithms and saves the predictions and ground truth to a CSV file
    """
    # Parameters for the training and predictions
    CV = 10
    
    subsets = ('fiss', 'act', 'fissact', 'all')
    subset = subsets[2]
    
    pkl_base = './pkl_trainsets/2jul2018/2jul2018_trainset'
    
    for trainset in ('1', '2'):
        pkl = pkl_base + trainset + '_nucs_' + subset + '_not-scaled.pkl'
        trainXY = pd.read_pickle(pkl)
        trainX, rY, cY, eY, bY = splitXY(trainXY)
        if subset == 'all':
            top_n = 100
            nuc_set = top_nucs(trainX, top_n)
            trainX = filter_nucs(trainX, nuc_set, top_n)
        trainX = scale(trainX)
        
        # loops through each reactor parameter to do separate predictions
        for Y in ('r', 'b', 'c', 'e'):
            trainY = pd.Series()
            # get param names and set ground truth
            if Y == 'c':
                trainY = cY
                parameter = 'cooling'
            elif Y == 'e': 
                trainY = eY
                parameter = 'enrichment'
            elif Y == 'b':
                trainY = bY
                parameter = 'burnup'
            else:
                trainY = rY
                parameter = 'reactor'
            
            #######################
            # optimize parameters #
            #######################
            
            # initialize learners
            score = 'explained_variance'
            kfold = KFold(n_splits=CV, shuffle=True)
            alg1_init = DecisionTreeRegressor()
            alg2_init = ExtraTreeRegressor()
            alg3_init = BayesianRidge()
            if Y is 'r':
                score = 'accuracy'
                kfold = StratifiedKFold(n_splits=CV, shuffle=True)
                alg1_init = DecisionTreeClassifier(class_weight='balanced')
                alg2_init = ExtraTreeClassifier(class_weight='balanced')
                alg3_init = GaussianNB()
            
            # CV search the hyperparams
            # alg1
            alg1_grid = {"max_depth": np.linspace(3, 90).astype(int), 
                         "max_features": np.linspace(5, len(trainXY.columns)-6).astype(int)}
            alg1_opt = RandomizedSearchCV(estimator=alg1_init, param_distributions=alg1_grid, 
                                          n_iter=20, scoring=score, n_jobs=-1, cv=kfold, 
                                          return_train_score=True)
            alg1_opt.fit(trainX, trainY)
            alg1_init = alg1_opt.best_estimator_
            d1 = alg1_opt.best_params_['max_depth']
            f1 = alg1_opt.best_params_['max_features']
            
            # alg2
            alg2_grid = alg1_grid
            alg2_opt = RandomizedSearchCV(estimator=alg2_init, param_distributions=alg2_grid,
                                          n_iter=20, scoring=score, n_jobs=-1, cv=kfold, 
                                          return_train_score=True)
            alg2_opt.fit(trainX, trainY)
            alg2_init = alg2_opt.best_estimator_
            d2 = alg2_opt.best_params_['max_depth']
            f2 = alg2_opt.best_params_['max_features']
            
            # alg3
            alg3_grid = {'n_iter': np.linspace(50, 1000).astype(int), 
                         'alpha_1': np.logspace(-8, 2), 'alpha_2' : np.logspace(-8, 2), 
                         'lambda_1': np.logspace(-8, 2), 'lambda_2' : np.logspace(-8, 2)}
            if Y is not 'r':
                alg3_opt = RandomizedSearchCV(estimator=alg3_init, param_distributions=alg3_grid,
                                              n_iter=20, scoring=score, n_jobs=-1, cv=kfold, 
                                              return_train_score=True)
                alg3_opt.fit(trainX, trainY)
                alg3_init = alg3_opt.best_estimator_
                it = alg3_opt.best_params_['n_iter']
                a1 = alg3_opt.best_params_['alpha_1']
                a2 = alg3_opt.best_params_['alpha_2']
                l1 = alg3_opt.best_params_['lambda_1']
                l2 = alg3_opt.best_params_['lambda_2']

            # Save dat info
            param_file = 'trainset_' + trainset + '_hyperparameters_alt-algs.txt'
            with open(param_file, 'a') as pf:
                pf.write('The following parameters are best from the randomized search for the {} parameter prediction:\n'.format(parameter))
                pf.write('max depth for dtree is {}\n'.format(d1)) 
                pf.write('max features for dtree is {}\n'.format(f1)) 
                pf.write('max depth for xtree is {}\n'.format(d2)) 
                pf.write('max features for xtree is {}\n'.format(f2)) 
                if Y is not 'r':
                    pf.write('num iterations for bayes reg is {}\n'.format(it))
                    pf.write('alpha 1 for bayes reg is {}\n'.format(a1))
                    pf.write('alpha 2 for bayes reg is {}\n'.format(a2))
                    pf.write('lambda 1 for bayes reg is {}\n'.format(l1))
                    pf.write('lambda 2 for bayes reg is {}\n'.format(l2))

            ########################
            # run predictions, etc #
            ########################
            
            #scores = ['explained_variance', 'neg_mean_absolute_error']
            #if Y is 'r':
            #    scores = ['accuracy', ]
            #csv_name = 'trainset_' + trainset + '_' + subset + '_' + parameter
            #
            #print("The {} predictions in trainset {} are beginning\n".format(parameter, trainset), flush=True)
            #
            ## track predictions 
            #track_predictions(trainX, trainY, alg1_init, alg2_init, alg3_init, scores, kfold, csv_name)
            #print("\t Prediction tracking done\n", flush=True)

            ## calculate errors and scores
            #errors_and_scores(trainX, trainY, alg1_init, alg2_init, alg3_init, scores, kfold, csv_name)
            #print("\t CV scoring done\n", flush=True)

            ## learning curves
            #learning_curves(trainX, trainY, alg1_init, alg2_init, alg3_init, kfold, csv_name)
            #print("\t Learning curves done\n", flush=True)
            #
            #print("The {} predictions in trainset {} are complete\n".format(parameter, trainset), flush=True)
            #print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", flush=True)

    return

    Given training data, this script trains and tracks each prediction for
    several algorithms and saves the predictions and ground truth to a CSV file
    
Given training data, this script trains and tracks each prediction for
several algorithms and saves the predictions and ground truth to a CSV file
Given training data this script trains and tracks each prediction for several algorithms and saves the predictions and ground truth to a CSV file
def main():
    CV = 10
    subsets = ('fiss', 'act', 'fissact', 'all')
    subset = subsets[2]
    pkl_base = './pkl_trainsets/2jul2018/2jul2018_trainset'
    for trainset in ('1', '2'):
        pkl = pkl_base + trainset + '_nucs_' + subset + '_not-scaled.pkl'
        trainXY = pd.read_pickle(pkl)
        trainX, rY, cY, eY, bY = splitXY(trainXY)
        if subset == 'all':
            top_n = 100
            nuc_set = top_nucs(trainX, top_n)
            trainX = filter_nucs(trainX, nuc_set, top_n)
        trainX = scale(trainX)
        for Y in ('r', 'b', 'c', 'e'):
            trainY = pd.Series()
            if Y == 'c':
                trainY = cY
                parameter = 'cooling'
            elif Y == 'e': 
                trainY = eY
                parameter = 'enrichment'
            elif Y == 'b':
                trainY = bY
                parameter = 'burnup'
            else:
                trainY = rY
                parameter = 'reactor'
            score = 'explained_variance'
            kfold = KFold(n_splits=CV, shuffle=True)
            alg1_init = DecisionTreeRegressor()
            alg2_init = ExtraTreeRegressor()
            alg3_init = BayesianRidge()
            if Y is 'r':
                score = 'accuracy'
                kfold = StratifiedKFold(n_splits=CV, shuffle=True)
                alg1_init = DecisionTreeClassifier(class_weight='balanced')
                alg2_init = ExtraTreeClassifier(class_weight='balanced')
                alg3_init = GaussianNB()
            alg1_grid = {"max_depth": np.linspace(3, 90).astype(int), 
                         "max_features": np.linspace(5, len(trainXY.columns)-6).astype(int)}
            alg1_opt = RandomizedSearchCV(estimator=alg1_init, param_distributions=alg1_grid, 
                                          n_iter=20, scoring=score, n_jobs=-1, cv=kfold, 
                                          return_train_score=True)
            alg1_opt.fit(trainX, trainY)
            alg1_init = alg1_opt.best_estimator_
            d1 = alg1_opt.best_params_['max_depth']
            f1 = alg1_opt.best_params_['max_features']
            alg2_grid = alg1_grid
            alg2_opt = RandomizedSearchCV(estimator=alg2_init, param_distributions=alg2_grid,
                                          n_iter=20, scoring=score, n_jobs=-1, cv=kfold, 
                                          return_train_score=True)
            alg2_opt.fit(trainX, trainY)
            alg2_init = alg2_opt.best_estimator_
            d2 = alg2_opt.best_params_['max_depth']
            f2 = alg2_opt.best_params_['max_features']
            alg3_grid = {'n_iter': np.linspace(50, 1000).astype(int), 
                         'alpha_1': np.logspace(-8, 2), 'alpha_2' : np.logspace(-8, 2), 
                         'lambda_1': np.logspace(-8, 2), 'lambda_2' : np.logspace(-8, 2)}
            if Y is not 'r':
                alg3_opt = RandomizedSearchCV(estimator=alg3_init, param_distributions=alg3_grid,
                                              n_iter=20, scoring=score, n_jobs=-1, cv=kfold, 
                                              return_train_score=True)
                alg3_opt.fit(trainX, trainY)
                alg3_init = alg3_opt.best_estimator_
                it = alg3_opt.best_params_['n_iter']
                a1 = alg3_opt.best_params_['alpha_1']
                a2 = alg3_opt.best_params_['alpha_2']
                l1 = alg3_opt.best_params_['lambda_1']
                l2 = alg3_opt.best_params_['lambda_2']
            param_file = 'trainset_' + trainset + '_hyperparameters_alt-algs.txt'
            with open(param_file, 'a') as pf:
                pf.write('The following parameters are best from the randomized search for the {} parameter prediction:\n'.format(parameter))
                pf.write('max depth for dtree is {}\n'.format(d1)) 
                pf.write('max features for dtree is {}\n'.format(f1)) 
                pf.write('max depth for xtree is {}\n'.format(d2)) 
                pf.write('max features for xtree is {}\n'.format(f2)) 
                if Y is not 'r':
                    pf.write('num iterations for bayes reg is {}\n'.format(it))
                    pf.write('alpha 1 for bayes reg is {}\n'.format(a1))
                    pf.write('alpha 2 for bayes reg is {}\n'.format(a2))
                    pf.write('lambda 1 for bayes reg is {}\n'.format(l1))
                    pf.write('lambda 2 for bayes reg is {}\n'.format(l2))
            print("The {} predictions in trainset {} are beginning\n".format(parameter, trainset), flush=True)
             track predictions 
            track_predictions(trainX, trainY, alg1_init, alg2_init, alg3_init, scores, kfold, csv_name)
            print("\t Prediction tracking done\n", flush=True)
             calculate errors and scores
            errors_and_scores(trainX, trainY, alg1_init, alg2_init, alg3_init, scores, kfold, csv_name)
            print("\t CV scoring done\n", flush=True)
             learning curves
            learning_curves(trainX, trainY, alg1_init, alg2_init, alg3_init, kfold, csv_name)
            print("\t Learning curves done\n", flush=True)
            print("The {} predictions in trainset {} are complete\n".format(parameter, trainset), flush=True)
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", flush=True)
    return
def main ( ) : CV = 10 subsets = ( 'fiss' , 'act' , 'fissact' , 'all' ) subset = subsets [ 2 ] pkl_base = './pkl_trainsets/2jul2018/2jul2018_trainset' for trainset in ( '1' , '2' ) : pkl = pkl_base + trainset + '_nucs_' + subset + '_not-scaled.pkl' trainXY = pd . read_pickle ( pkl ) trainX , rY , cY , eY , bY = splitXY ( trainXY ) if subset == 'all' : top_n = 100 nuc_set = top_nucs ( trainX , top_n ) trainX = filter_nucs ( trainX , nuc_set , top_n ) trainX = scale ( trainX ) for Y in ( 'r' , 'b' , 'c' , 'e' ) : trainY = pd . Series ( ) if Y == 'c' : trainY = cY parameter = 'cooling' elif Y == 'e' : trainY = eY parameter = 'enrichment' elif Y == 'b' : trainY = bY parameter = 'burnup' else : trainY = rY parameter = 'reactor' score = 'explained_variance' kfold = KFold ( n_splits = CV , shuffle = True ) alg1_init = DecisionTreeRegressor ( ) alg2_init = ExtraTreeRegressor ( ) alg3_init = BayesianRidge ( ) if Y is 'r' : score = 'accuracy' kfold = StratifiedKFold ( n_splits = CV , shuffle = True ) alg1_init = DecisionTreeClassifier ( class_weight = 'balanced' ) alg2_init = ExtraTreeClassifier ( class_weight = 'balanced' ) alg3_init = GaussianNB ( ) alg1_grid = { "max_depth" : np . linspace ( 3 , 90 ) . astype ( int ) , "max_features" : np . linspace ( 5 , len ( trainXY . columns ) - 6 ) . astype ( int ) } alg1_opt = RandomizedSearchCV ( estimator = alg1_init , param_distributions = alg1_grid , n_iter = 20 , scoring = score , n_jobs = - 1 , cv = kfold , return_train_score = True ) alg1_opt . fit ( trainX , trainY ) alg1_init = alg1_opt . best_estimator_ d1 = alg1_opt . best_params_ [ 'max_depth' ] f1 = alg1_opt . best_params_ [ 'max_features' ] alg2_grid = alg1_grid alg2_opt = RandomizedSearchCV ( estimator = alg2_init , param_distributions = alg2_grid , n_iter = 20 , scoring = score , n_jobs = - 1 , cv = kfold , return_train_score = True ) alg2_opt . fit ( trainX , trainY ) alg2_init = alg2_opt . best_estimator_ d2 = alg2_opt . best_params_ [ 'max_depth' ] f2 = alg2_opt . best_params_ [ 'max_features' ] alg3_grid = { 'n_iter' : np . linspace ( 50 , 1000 ) . astype ( int ) , 'alpha_1' : np . logspace ( - 8 , 2 ) , 'alpha_2' : np . logspace ( - 8 , 2 ) , 'lambda_1' : np . logspace ( - 8 , 2 ) , 'lambda_2' : np . logspace ( - 8 , 2 ) } if Y is not 'r' : alg3_opt = RandomizedSearchCV ( estimator = alg3_init , param_distributions = alg3_grid , n_iter = 20 , scoring = score , n_jobs = - 1 , cv = kfold , return_train_score = True ) alg3_opt . fit ( trainX , trainY ) alg3_init = alg3_opt . best_estimator_ it = alg3_opt . best_params_ [ 'n_iter' ] a1 = alg3_opt . best_params_ [ 'alpha_1' ] a2 = alg3_opt . best_params_ [ 'alpha_2' ] l1 = alg3_opt . best_params_ [ 'lambda_1' ] l2 = alg3_opt . best_params_ [ 'lambda_2' ] param_file = 'trainset_' + trainset + '_hyperparameters_alt-algs.txt' with open ( param_file , 'a' ) as pf : pf . write ( 'The following parameters are best from the randomized search for the {} parameter prediction:\n' . format ( parameter ) ) pf . write ( 'max depth for dtree is {}\n' . format ( d1 ) ) pf . write ( 'max features for dtree is {}\n' . format ( f1 ) ) pf . write ( 'max depth for xtree is {}\n' . format ( d2 ) ) pf . write ( 'max features for xtree is {}\n' . format ( f2 ) ) if Y is not 'r' : pf . write ( 'num iterations for bayes reg is {}\n' . format ( it ) ) pf . write ( 'alpha 1 for bayes reg is {}\n' . format ( a1 ) ) pf . write ( 'alpha 2 for bayes reg is {}\n' . format ( a2 ) ) pf . write ( 'lambda 1 for bayes reg is {}\n' . format ( l1 ) ) pf . write ( 'lambda 2 for bayes reg is {}\n' . format ( l2 ) ) return
Given training data, this script trains and tracks each prediction for
several algorithms and saves the predictions and ground truth to a CSV file
Given training data this script trains and tracks each prediction for several algorithms and saves the predictions and ground truth to a CSV file
"""
    Given training data, this script trains and tracks each prediction for
    several algorithms and saves the predictions and ground truth to a CSV file
    """ # Parameters for the training and predictions # loops through each reactor parameter to do separate predictions # get param names and set ground truth ####################### # optimize parameters # ####################### # initialize learners # CV search the hyperparams # alg1 # alg2 # alg3 # Save dat info ######################## # run predictions, etc # ######################## #scores = ['explained_variance', 'neg_mean_absolute_error'] #if Y is 'r': #    scores = ['accuracy', ] #csv_name = 'trainset_' + trainset + '_' + subset + '_' + parameter # #print("The {} predictions in trainset {} are beginning\n".format(parameter, trainset), flush=True) # ## track predictions  #track_predictions(trainX, trainY, alg1_init, alg2_init, alg3_init, scores, kfold, csv_name) #print("\t Prediction tracking done\n", flush=True) ## calculate errors and scores #errors_and_scores(trainX, trainY, alg1_init, alg2_init, alg3_init, scores, kfold, csv_name) #print("\t CV scoring done\n", flush=True) ## learning curves #learning_curves(trainX, trainY, alg1_init, alg2_init, alg3_init, kfold, csv_name) #print("\t Learning curves done\n", flush=True) # #print("The {} predictions in trainset {} are complete\n".format(parameter, trainset), flush=True) #print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", flush=True)