GA-SVM算法python实现

本文介绍了如何在Python中实现GA-SVM算法,首先定义了SVM和MSE函数,接着创建了一个遗传算法种群个体计算的类,包括种群选择、交叉和变异的操作。然后利用Geatpy工具箱进行计算,该工具箱提供了高效的遗传和进化算法算子,执行效率高,学习成本低。最后给出了完整代码的概览。
摘要由CSDN通过智能技术生成

利用GA实现对SVM超参数的优化


最近在学习如何用遗传算法对SVM的超参数进行优化,参考了一些其他博主的文章,实现了这一方法。

遗传算法程序

首先定义SVM和MSE的函数:

def msefunc(predictval, realval):
    squaredError = []
    # absError = []
    for i in range(len(predictval)):
        val = predictval[i] - realval[i]
        squaredError.append(val * val)  # 预测值与真实值之差的平方

    print("Square Error: ", squaredError)
    print("MSE = ", sum(squaredError) / len(squaredError))  # 均方误差MSE
    return sum(squaredError) / len(squaredError)


def SVMResult(vardim, x, bound):
    X_train = [[0, 0], [2, 2], [1, 4], [3, 7], [3, 6]]
    y_train = [0.5, 2.5, 3.0, 4.0, 5]
    X_valid = [[1, 1], [3, 5]]
    y_valid = [3, 4]
    c = x[0]
    e = x[1]
    g = x[2]
    clf = svm.SVR(C=c, epsilon=e, gamma=g, kernel='rbf')
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_valid)
    print("y_pred is", y_pred , "y_true is" , y_valid)
    # 返回svm的mse作为适应度值
    return msefunc(y_pred, y_valid)

遗传算法种群个体计算:

class GAIndividual:
    '''
    individual of genetic algorithm
    创建pop中的单个个体
    '''
    def __init__(self, vardim, bound):
        '''
        vardim: dimension of variables
        bound: boundaries of variables
        '''
        self.vardim = vardim
        self.bound = bound
        self.fitness = 0.

    def generate(self):
        '''
        generate a random chromsome for genetic algorithm
        '''
        len = self.vardim
        rnd = np.random.random(size=len)
        self.chrom = np.zeros(len)
        for i in range(0, len):
            self.chrom[i] = self.bound[0, i] + \
                (self.bound[1, i] - self.bound[0, i]) * rnd[i]

    def calculateFitness(self):
        '''
        calculate the fitness of the chromsome
        '''
        self.fitness = SVMResult(self.vardim, self.chrom, self.bound)

建立种群衍变的类,其中种群选择、交叉、变异的方法可以根据需要重新定义。

class GeneticAlgorithm:
    '''
    The class for genetic algorithm
    '''
    def __init__(self, sizepop, vardim, bound, MAXGEN, params):
        '''
        sizepop: population sizepop
        vardim: dimension of variables
        bound: boundaries of variables
        MAXGEN: termination condition
        param: algorithm required parameters, it is a list which is consisting 
               of crossover rate, mutation rate, alpha
        '''
        self.sizepop = sizepop
        self.MAXGEN = MAXGEN
        self.vardim = vardim
        self.bound = bound
        self.population = []
        self.fitness = np.zeros((self.sizepop, 1))
        self.trace = np.zeros((self.MAXGEN, 2))
        self.params = params

    def initialize(self):
        '''
        initialize the population
        '''
        for i in range(0, self.sizepop):
            ind = GAIndividual(self.vardim, self.bound)
            ind.generate()
            self.population.append(ind)

    def evaluate(self):
        '''
        evaluation of the population fitnesses
        '''
        for i in range(0, self.sizepop):
            self.population[i].calculateFitness()
            self.fitness[i] = self.population[i].fitness

    def solve(self):
        '''
        evolution process of genetic algorithm
        '''
        self.t = 0 # 迭代次数
        self.initialize() # 初始化种群
        self.evaluate() # 计算适应度
        best = np.max(self.fitness) #选出适应度最大的个体
        bestIndex = np.argmax(self.fitness) # 最大适应度的索引
        self.best = copy.deepcopy(self.population[bestIndex]) 
        self.avefitness = np.mean(self.fitness) # 平均适应度
        self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness
        self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness
        print(
            "Generation %d: optimal function value is: %f; average function value is %f"
            % (self.t, self.trace[self.t, 0], self.trace[self.t, 1]))
        while (self.t < self.MAXGEN - 1):
            self.t += 1
            self.selectionOperation() # 选择
            self.crossoverOperation() # 交叉
            self.mutationOperation()  # 变异
            self.evaluate()           # 重新计算新种群适应度
            best = np.max(self.fitness)
            bestIndex = np.argmax(self.fitness)
            if best > self.best.fitness:
                self.best = copy.deepcopy(self.population[bestIndex])
            self.avefitness = np.mean(self.fitness)
            # 种群中表现最好的个体的适应度变化
            self.trace[self.t, 0] = (1 - self.best.fitness) / self.best.fitness 
            # 种群平均适应度变化
            self.trace[self.t, 1] = (1 - self.avefitness) / self.avefitness
            print(
                "Generation %d: optimal function value is: %f; average function value is %f"
                % (self.t, self.trace[self.t, 0], self.trace[self.t, 1]))
        print("Optimal function value is: %f; " % self.trace[self.t, 0])
        print("Optimal solution is:",self.best.chrom)
        self.printResult()

    def selectionOperation(self):
        '''
        selection operation for Genetic Algorithm
        '''
        newpop = []
        totalFitness = np.sum(self.fitness)
        accuFitness = np.zeros((self.sizepop, 1))

        # 适应度的累进占比
        sum1 = 0.
        for i in range(0, self.sizepop):
            accuFitness[i] = sum1 + self.fitness[i] / totalFitness
            sum1 = accuFitness[i]

        # 随机选出新种群的索引
        for i in range(0, self.sizepop):
            r = random.random()
            idx = 0
            for j in range(0, self.sizepop - 1):
                if j == 0 and r < accuFitness[j]:
                    idx = 0
                    break
                elif r >= accuFitness[j] and r < accuFitness[j + 1]:
                    idx = j + 1
                    break
            newpop.append(self.population[idx])
        self.population = newpop

    def crossoverOperation(self):
        '''
        crossover operation for genetic algorithm
        '''
        newpop = []
        # 选出两个个体进行交换
        for i 
  • 15
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
ga-svm是一种支持向量机(Support Vector Machine)算法的变种,它使用遗传算法(Genetic Algorithm)来优化支持向量机的超参数。 下面是一个基于Python实现代码示例: ```python import numpy as np from sklearn.svm import SVC from sklearn.model_selection import cross_val_score # 设置遗传算法参数 POPULATION_SIZE = 50 # 种群大小 GENERATION_NUM = 100 # 迭代代数 CROSSOVER_PROB = 0.8 # 交叉概率 MUTATION_PROB = 0.1 # 变异概率 # 初始化种群 def init_population(): population = [] for _ in range(POPULATION_SIZE): # 随机生成C和gamma的取值 C = np.random.uniform(0.1, 10) gamma = np.random.uniform(0.1, 5) population.append([C, gamma]) return population # 评估适应度函数 def evaluate_fitness(population, X, y): fitness = [] for ind in population: # 创建支持向量机模型,使用交叉验证计算适应度 svc = SVC(C=ind[0], gamma=ind[1]) score = cross_val_score(svc, X, y, cv=5).mean() # 5折交叉验证 fitness.append(score) return fitness # 选择操作 def selection(population, fitness): # 根据适应度值进行排序 sorted_indices = np.argsort(fitness) # 选择适应度较高的个体 selected_population = [population[i] for i in sorted_indices[-POPULATION_SIZE:]] return selected_population # 交叉操作 def crossover(population): new_population = [] for _ in range(POPULATION_SIZE): # 随机选择两个个体 parent1, parent2 = np.random.choice(population, size=2, replace=False) if np.random.rand() < CROSSOVER_PROB: # 按一定比例交叉生成新个体 child = [parent1[0], parent2[1]] else: # 保留原个体 child = parent1 new_population.append(child) return new_population # 变异操作 def mutation(population): for ind in population: if np.random.rand() < MUTATION_PROB: # 对C和gamma进行随机变异 ind[0] = np.random.uniform(0.1, 10) ind[1] = np.random.uniform(0.1, 5) return population # 主函数 def ga_svm(X, y): population = init_population() for _ in range(GENERATION_NUM): fitness = evaluate_fitness(population, X, y) population = selection(population, fitness) population = crossover(population) population = mutation(population) # 选择最佳个体 best_ind = population[np.argmax(fitness)] return best_ind # 使用示例 X = np.array([[0, 0], [1, 1]]) y = np.array([0, 1]) best_ind = ga_svm(X, y) print('Best individual:', best_ind) ``` 以上是一个用Python实现的基于遗传算法SVM代码示例,该代码通过遗传算法优化SVM的超参数选取,以获得更好的分类性能。代码中使用了`sklearn`库中的SVC类来建立支持向量机模型,使用5折交叉验证评估模型性能。遗传算法中的选择、交叉和变异操作通过相应的函数实现。最终输出的是训练出的最佳个体,对应的C和gamma参数。这个示例可以根据具体的数据集和问题进行配置和调整。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值