代码随想录算法训练营第27天 |复原IP地址、子集 、子集 II

1、复原IP地址

class Solution:
    def restoreIpAddresses(self, s: str) -> List[str]:
        def isValid(s): # 判断地址是否合法 <256 且没有前置0
            return int(s) < 256 and (s[0] != '0' or s == '0')
        
        def back(s, start, dotNum, path, result):
            if dotNum == 4: # 如果打了四个点,说明有了四段,可以返回了
                if start == len(s): # 如果start到了s的最后一位,说明整个IP复合要求
                    result.append('.'.join(path))
                return
            
            for i in range(1, 4):  # IP地址从1位遍历到3位
                if start + i > len(s):
                    break
                segment = s[start:start+i] # 分割
                if isValid(segment): # 判断分割的地址是否符合,符合的话进入下一层
                    path.append(segment)
                    back(s, start+i, dotNum+1, path, result)
                    #回溯
                    path.pop()
        
        result = []
        back(s, 0, 0, [], result)
        return result

2、子集

class Solution:
    def back(self, nums, idx, path, result):
        result.append(path[:]) # 在每个节点都收集结果,这题的精髓所在
        if idx >= len(nums): # 这里可写可不写,反正for跑完了就会return
            return

        for i in range(idx, len(nums)): # 和组合的回溯法差不多
            path.append(nums[i])
            self.back(nums, i+1, path, result)
            path.pop()
    def subsets(self, nums: List[int]) -> List[List[int]]:
        result = []
        self.back(nums, 0, [] ,result)
        return result

3、子集II

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        def back(nums, startId, used, path, result):
            result.append(path[:])
            if startId >= len(nums):
                return
            for i in range(startId, len(nums)):
                if i > 0 and nums[i] == nums[i-1] and not used[i-1]:
                    continue
                path.append(nums[i])
                # 在上一题的基础上加入used数组树层去重
                # 结合前两天的知识点
                used[i] = True 
                back(nums, i+1, used, path, result)
                path.pop()
                used[i] = False
        
        nums.sort()
        used = [False]*len(nums)
        result = []
        back(nums, 0, used, [], result)
        return result

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Python代码示例,展示如何使用遗传算法选取最优特征子集训练rbf核分类SVM: ```python import numpy as np from sklearn.svm import SVC from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split # 加载乳腺癌数据集 data = load_breast_cancer() X, y = data.data, data.target # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 定义遗传算法的参数 pop_size = 50 generations = 100 mutation_rate = 0.1 # 定义特征向量的维度 n_features = X_train.shape[1] # 随机生成初始种群 population = np.random.randint(2, size=(pop_size, n_features)) # 遗传算法的主循环 for i in range(generations): # 计算每个个体的适应度值 fitness = np.zeros(pop_size) for j in range(pop_size): selected_features = np.where(population[j] == 1)[0] if len(selected_features) > 0: clf = SVC(kernel='rbf') clf.fit(X_train[:, selected_features], y_train) fitness[j] = clf.score(X_test[:, selected_features], y_test) # 选择操作 selected_indices = np.random.choice(pop_size, size=pop_size//2, replace=False, p=fitness/np.sum(fitness)) selected_population = population[selected_indices] # 交叉操作 crossover_population = np.zeros_like(selected_population) for j in range(0, len(selected_indices), 2): crossover_point = np.random.randint(n_features) crossover_population[j][:crossover_point] = selected_population[j][:crossover_point] crossover_population[j][crossover_point:] = selected_population[j+1][crossover_point:] crossover_population[j+1][:crossover_point] = selected_population[j+1][:crossover_point] crossover_population[j+1][crossover_point:] = selected_population[j][crossover_point:] # 变异操作 mutation_population = crossover_population for j in range(len(crossover_population)): if np.random.rand() < mutation_rate: mutation_population[j][np.random.randint(n_features)] = 1 - mutation_population[j][np.random.randint(n_features)] # 更新种群 population = mutation_population # 找到最优的特征子集 best_individual = None best_fitness = 0 for j in range(pop_size): selected_features = np.where(population[j] == 1)[0] if len(selected_features) > 0: clf = SVC(kernel='rbf') clf.fit(X_train[:, selected_features], y_train) current_fitness = clf.score(X_test[:, selected_features], y_test) if current_fitness > best_fitness: best_individual = selected_features best_fitness = current_fitness # 输出最优的特征子集和对应的分类准确率 print('Best individual:', best_individual) print('Best fitness:', best_fitness) ``` 这个代码示例中,我们使用`load_breast_cancer()`函数加载了一个乳腺癌数据集,然后将数据集划分为训练集和测试集。接着,我们定义了遗传算法的参数,随机生成了初始种群,并在遗传算法的主循环中进行了选择、交叉、变异等操作。每个个体的适应度值是通过训练rbf核SVM并在测试集上评估得到的分类准确率。最后,我们在所有个体中找到了最优的特征子集,并输出了对应的分类准确率。 需要注意的是,这个代码示例仅为演示如何使用遗传算法选取最优特征子集训练rbf核分类SVM,具体应用中需要根据具体情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值