python对列表中的数值进行统计运算_Python数据分析基础笔记---数据计算

四则运算df['平均成绩'] = (df['张三'] + df['李四'] + df['王五'] ) / 3df['张三'] / df['平均成绩']逻辑运算df['张三'] > df['平均成绩']统计运算df.count(axis=0)#返回该表中每列的非空值的个数df.count(axis=1)#返回该表中每行的非空值的个数df['平均成绩'].count()#返回某列中的非空值个数...
摘要由CSDN通过智能技术生成

四则运算

df['平均成绩'] = (df['张三'] + df['李四'] + df['王五'] ) / 3

df['张三'] / df['平均成绩']

逻辑运算

df['张三'] > df['平均成绩']

统计运算

df.count(axis=0) #返回该表中每列的非空值的个数

df.count(axis=1) #返回该表中每行的非空值的个数

df['平均成绩'].count()  #返回某列中的非空值个数

求和

df.sum(axis=0) #求每列的总和

print(df.sum(axis=1) #求每行的总和

print(df['张三'].sum() #对某列求和

求均值

df.mean(axis=0) #求每列的

df.mean(axis=1) #求每行的

df['张三'].mean()

求最值

df.max(axis=0) #求每列的

df.max(axis=1) #求每行的

df['张三'].max() #求某行的

df.max().max() #求整表的

df.min(axis=0) #求每列的

df.min(axis=1) #求每行的

df['张三'].min()

df.min().min()

求中位数

df.median(axis=0) #求每列的

df.median(axis=1) #求每行的

df['张三'].median()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遗传算法是一种优化算法,用于解决最值问题。下面是一个简单的遗传算法的 Python 代码示例,用于求解一个目标函数的最大值。 ```python import random # 目标函数 def objective_function(x): return -x**2 + 4 # 初始化种群 def initialize_population(population_size, num_variables): population = [] for _ in range(population_size): chromosome = [random.randint(0, 1) for _ in range(num_variables)] population.append(chromosome) return population # 计算适应度 def fitness(chromosome): x = decode_chromosome(chromosome) return objective_function(x) # 解码染色体得到实际数值 def decode_chromosome(chromosome): x = 0 for i in range(len(chromosome)): x += chromosome[i] * (2**i) return x # 交叉操作 def crossover(parent1, parent2): crossover_point = random.randint(1, len(parent1)-1) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] return child1, child2 # 变异操作 def mutation(chromosome, mutation_rate): mutated_chromosome = [] for gene in chromosome: if random.random() < mutation_rate: mutated_gene = 1 - gene else: mutated_gene = gene mutated_chromosome.append(mutated_gene) return mutated_chromosome # 遗传算法主函数 def genetic_algorithm(population_size, num_variables, num_generations): population = initialize_population(population_size, num_variables) for _ in range(num_generations): # 计算适应度 fitness_values = [fitness(chromosome) for chromosome in population] # 选择操作 selected_indices = random.choices(range(population_size), weights=fitness_values, k=2) parent1 = population[selected_indices[0]] parent2 = population[selected_indices[1]] # 交叉操作 child1, child2 = crossover(parent1, parent2) # 变异操作 child1 = mutation(child1, mutation_rate=0.01) child2 = mutation(child2, mutation_rate=0.01) # 更新种群 population[selected_indices[0]] = child1 population[selected_indices[1]] = child2 # 找到最优解 best_chromosome = max(population, key=fitness) best_solution = decode_chromosome(best_chromosome) best_fitness = fitness(best_chromosome) return best_solution, best_fitness # 示例运行 best_solution, best_fitness = genetic_algorithm(population_size=100, num_variables=4, num_generations=50) print("最优解:", best_solution) print("最优解的适应度:", best_fitness) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值