Beispiel #1
0
def main():

    fileW = FromFinessFileMLR.createAnOutputFile()
    model = mlr.MLR()

    #Number of descriptor should be 396 and number of population should be 50 or more

    numOfPop = 50
    numOfFea = 396
    unfit = 1000

    # Final model requirements

    R2req_train = .6
    R2req_validate = .5
    R2req_test = .5

    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData(
    )
    TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData(
        TrainX, ValidateX, TestX)

    unfit = 1000
    fittingStatus = unfit
    """Create a population based on the number of features selected, in this case 10, from the pool of features"""

    population = DifferentialEvolution.Create_A_Population(numOfPop, numOfFea)
    fittingStatus, fitness = FromFinessFileMLR.validate_model(model,fileW, population, \
        TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
Beispiel #2
0
def append_to_file(new_vector, fileW):
    #print new_vector
    model = DE_BPSO_model.DE_BPSO_MODEL()
    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData(
    )
    TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData(
        TrainX, ValidateX, TestX)

    FromFinessFileMLR.validate_model_and_append(model,fileW, new_vector, \
                                    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
Beispiel #3
0
def main():
    # BPSO parameters
    num_pop = 50
    num_feat = 385
    num_gens = 1000

    # initialize objects
    model = mlr.MLR()
    fdf = FromDataFileMLR.FromDataFileMLR()
    fff = FromFinessFileMLR.FromFinessFileMR(fdf)
    bpso = BinaryParticleSwarmOptimization(model, fff, num_pop, num_feat,
                                           num_gens)

    # load in data from files
    trainX, trainY, validateX, validateY, testX, testY = fdf.getAllOfTheData()
    trainX, validateX, testX = fdf.rescaleTheData(trainX, validateX, testX)

    # BPSO algorithm
    bpso.create_initial_population()
    bpso.evaluate_population(trainX, trainY, validateX, validateY, testX,
                             testY)
    bpso.create_initial_velocity()
    initial_local_best_matrix, initial_local_fitness = bpso.create_initial_local_best_matrix(
    )
    bpso.create_initial_global_best_row()
    bpso.evolve_population(initial_local_best_matrix, initial_local_fitness,
                           trainX, trainY, validateX, validateY, testX, testY)
Beispiel #4
0
def main():
    np.random.seed()

    # initialize objects
    fileW = createAnOutputFile()
    model = mlr.MLR()

    # load in data from files
    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData(
    )
    TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData(
        TrainX, ValidateX, TestX)

    # DE_BPSO algorithm
    velocity = create_initial_velocity()
    population = create_initial_population(velocity, Lambda=0.01)
    x, fitness = FromFinessFileMLR.validate_model(model,fileW, population, \
                                        TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)

    local_best_matrix, local_fitness = create_initial_local_best_matrix(
        population, fitness)
    create_initial_global_best_row(local_best_matrix, local_fitness)

    evolve_population(population, fitness, velocity, local_best_matrix, local_fitness, \
                                        model, fileW, TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
Beispiel #5
0
 def __init__(self, numOfPop, numOfFea):
     self.filedata = FromDataFileMLR.DataFromFile()
     self.fitnessdata = FromFinessFileMLR.FitnessResults()
     self.NofIterations = 2000
     self.alpha = 0.5
     self.GlobalBestRow = ndarray(numOfFea)
     self.GlobalBestFitness = 10000
     self.VelocityM = ndarray((numOfPop, numOfFea))
     self.LocalBestM = ndarray((numOfPop, numOfFea))
     self.LocalBestM_Fit = ndarray(numOfPop)
Beispiel #6
0
def cal_fitness_DE(new_vector):
    #print new_vector
    model = DE_BPSO_model.MLR()
    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = FromDataFileMLR.getAllOfTheData(
    )
    TrainX, ValidateX, TestX = FromDataFileMLR.rescaleTheData(
        TrainX, ValidateX, TestX)

    fitness = FromFinessFileMLR.validate_single_model(model, new_vector, \
                                    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
    return fitness
Beispiel #7
0
 def __init__(self, numOfPop, numOfFea):
     # Acquires and formats data from Train, Validation, Test .csv files
     self.filedata = FromDataFileMLR.DataFromFile()
     # Performs data analysis on training, validation, and test data
     self.analyzer = FromFinessFileMLR.FitnessResults()
     self.NumIterations = 1000
     self.alpha = 0.5  # starting alpha value
     self.GlobalBestRow = ndarray(
         numOfFea)  # best-fitting population yet found
     self.GlobalBestFitness = 10000  # fitness of GlobalBestRow, initialized very high
     self.VelocityM = ndarray((numOfPop, numOfFea))  # Velocity matrix
     self.LocalBestM = ndarray((numOfPop, numOfFea))  # local best matrix
     self.LocalBestM_Fit = ndarray(numOfPop)  # local best matrix fitnesses
Beispiel #8
0
def main():
    # Number of descriptor should be 385 and number of population should be 50 or more
    numOfPop = 50
    numOfFea = 385

    # create an object of Multiple Linear Regression model.
    # The class is located in mlr file
    model = mlr.MLR()
    filedata = FromDataFileMLR.DataFromFile()
    fitnessdata = FromFinessFileMLR.FitnessResults()
    analyzer = Fitness(numOfPop, numOfFea)

    # create an output file. Name the object to be FileW
    fileW = analyzer.createAnOutputFile()

    # we continue exhancing the model; however if after 1000 iteration no
    # enhancement is done, we can quit
    unfit = 1000

    # Final model requirements: The following is used to evaluate each model. The minimum
    # values for R^2 of training should be 0.6, R^2 of Validation should be 0.5 and R^2 of
    # test should be 0.5
    R2req_train = .6
    R2req_validate = .5
    R2req_test = .5

    # getAllOfTheData is in FromDataFileMLR file. The following places the data
    # (training data, validation data, and test data) into associated matrices
    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY = filedata.getAllOfTheData(
    )
    TrainX, ValidateX, TestX = filedata.rescaleTheData(TrainX, ValidateX,
                                                       TestX)

    fittingStatus = unfit
    population = analyzer.createInitialPopulation(numOfPop, numOfFea)
    fittingStatus, fitness = fitnessdata.validate_model(model,fileW, population, \
        TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)

    analyzer.CreateInitialVelocity(numOfPop, numOfFea)
    copyto(analyzer.LocalBestM,
           population)  #initializing LocalBestMatrix as the initial population
    copyto(analyzer.LocalBestM_Fit, fitness)
    analyzer.FindGlobalBestRow()

    analyzer.PerformOneMillionIteration(numOfPop, numOfFea, population, fitness, model, fileW, \
                               TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
Beispiel #9
0
def main():
    # GA parameters
    num_pop = 50
    num_feat = 385
    num_gens = 1000

    # initialize objects
    model = mlr.MLR()
    fdf = FromDataFileMLR.FromDataFileMLR()
    fff = FromFinessFileMLR.FromFinessFileMR(fdf)
    GA = GeneticAlgorithm(model, fff, num_pop, num_feat, num_gens)

    # load in data from files
    trainX, trainY, validateX, validateY, testX, testY = fdf.getAllOfTheData()
    trainX, validateX, testX = fdf.rescaleTheData(trainX, validateX, testX)

    # genetic algorithm
    GA.create_initial_population()
    GA.evaluate_population(trainX, trainY, validateX, validateY, testX, testY)
    GA.evolve_population(trainX, trainY, validateX, validateY, testX, testY)
Beispiel #10
0
def main():
    # DE parameters
    num_pop = 50
    num_feat = 385
    num_gens = 100

    # initialize objects
    model = mlr.MLR()
    FDF = FromDataFileMLR.FromDataFileMLR()
    FFF = FromFinessFileMLR.FromFinessFileMR(FDF)
    DE = DifferentialEvolution(model, FFF, num_pop, num_feat, num_gens)

    # load in data from files
    trainX, trainY, validateX, validateY, testX, testY = FDF.getAllOfTheData()
    trainX, validateX, testX = FDF.rescaleTheData(trainX, validateX, testX)

    # differential evolution algorithm
    DE.create_initial_population()
    DE.evaluate_population(trainX, trainY, validateX, validateY, testX, testY)
    DE.evolve_population(trainX, trainY, validateX, validateY, testX, testY)
Beispiel #11
0
def evolve_population(initial_population, initial_fitness, initial_velocity,
                      initial_local_best_matrix, initial_local_fitness, model,
                      fileW, TrainX, TrainY, ValidateX, ValidateY, TestX,
                      TestY):
    print('gen #' + str(0) + '\t\t min fit: ' + str(initial_fitness.min()) +
          '\t\t avg fit: ' + str(np.average(initial_fitness)))
    alpha = 0.5

    for i in range(1, numOfGens):
        velocity = update_velocity(initial_velocity, initial_population)
        population = create_new_population(numOfPop, numOfFea,
                                           initial_population, velocity,
                                           initial_local_best_matrix, alpha, i)
        x, fitness = FromFinessFileMLR.validate_model(model, fileW, population, \
                                                    TrainX, TrainY, ValidateX, ValidateY, TestX, TestY)
        local_best_matrix, local_fitness = update_local_best_matrix(
            population, fitness, initial_local_best_matrix,
            initial_local_fitness)
        update_global_best_row(local_best_matrix, local_fitness)

        alpha = alpha - (0.17 / numOfGens)

        print('gen #' + str(i) + '\t\t min fit: ' + str(fitness.min()) +
              '\t\t avg fit: ' + str(np.average(fitness)))
Beispiel #12
0
 def __init__(self):
     self.DataFile = FromDataFileMLR.DataMLR()
     self.FitnessFile = FromFinessFileMLR.FitnessMLR()
Beispiel #13
0
def evaluate_population(model, fileW, population, trainX, trainY, validateX,
                        validateY, testX, testY):
    fittingStatus, fitness = FromFinessFileMLR.validate_model(
        model, fileW, population, trainX, trainY, validateX, validateY, testX,
        testY)
    return fitness
Beispiel #14
0
 def __init__(self):
     self.fitnessdata = FromFinessFileMLR.FitnessResults()
Beispiel #15
0
 def __init__(self):
     self.DF = FromDataFileMLR.DataFile()
     self.FF = FromFinessFileMLR.FitnessFile()
Beispiel #16
0
 def __init__(self):
     self.filedata = FromDataFileMLR.DataFromFile()
     self.fitnessdata = FromFinessFileMLR.FitnessResults()
Beispiel #17
0
import time  # provides timing for benchmarks
from numpy import *  # provides complex math and array functions
from sklearn import svm  # provides Support Vector Regression
import csv
import math
import sys

# Local files created by me
import mlr
import FromDataFileMLR
import FromFinessFileMLR
import BPSO
"""Create the output file"""

fileW = FromFinessFileMLR.createAnOutputFile()
#fileW = 0
model = mlr.MLR()
start = time.time()
#Number of descriptor should be 396 and number of population should be 50 or more
"""Number of population"""
numOfPop = 50
"""Number of total features"""
numOfFea = 396

# Final model requirements

R2req_train = .6
R2req_validate = .5
R2req_test = .5
alpha = 0.5
Beispiel #18
0
Compute a better fitness function """

import time  # provides timing for benchmarks
from numpy import *  # provides complex math and array functions
from sklearn import svm  # provides Support Vector Regression
import csv
import math
import sys

# Local files created by me
import mlr
import FromDataFileMLR
import FromFinessFileMLR
import DifferentialEvolution

fileW = FromFinessFileMLR.createAnOutputFile()
#fileW = 0
model = mlr.MLR()

#Number of descriptor should be 396 and number of population should be 50 or more
"""Number of population"""
numOfPop = 50
"""Number of total feautres"""
#numOfFea = 396
numOfFea = 385

# Final model requirements

R2req_train = .6
R2req_validate = .5
R2req_test = .5