调用np.var函数时出现inf值的解决方法

本文介绍了一种在使用NumPy的var函数时遇到输出inf值的问题及解决方案。问题原因可能是数值精度不足导致,通过指定更高精度的数据类型float64解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用var函数时发现输出了inf值

出现问题的代码:

var_vec = np.var(data, axis=0)

确认data没有问题,输出值有inf,搜索后发现可能由于精度不够造成的,更改代码如下解决了

var_vec = np.var(data, axis=0, dtype='float64')

附上参考网址:
Why does “numpy.mean” return ‘inf’?

# ====== 2. NSGA-II核心实现 ====== def nsga2(pop_size=100, generations=100): # 初始化参数 n_var = 30 # 变量维度 pc, pm = 0.9, 1/n_var # 交叉/变异概率 eta_c, eta_m = 20, 20 # 分布指数 # 初始化种群 pop = np.random.rand(pop_size, n_var) for _ in range(generations): # 生成子代 offspring = crossover_mutation(pop, pc, pm, eta_c, eta_m) # 合并种群 combined = np.vstack((pop, offspring)) # 非支配排序 fronts = fast_non_dominated_sort(combined) # 拥挤度排序 new_pop = [] for front in fronts: if len(new_pop) + len(front) > pop_size: crowding_distances = crowding_distance(front) front = front[np.argsort(-crowding_distances)] new_pop += front[:pop_size - len(new_pop)].tolist() break else: new_pop += front.tolist() pop = np.array(new_pop) return pop # ====== 3. 核心操作实现 ====== def fast_non_dominated_sort(pop): # 实现快速非支配排序 S = [[] for _ in range(len(pop))] fronts = [[]] n = np.zeros(len(pop), dtype=int) rank = np.zeros(len(pop), dtype=int) # 计算支配关系 for i, p in enumerate(pop): for j, q in enumerate(pop): if all(zdt1(p) <= zdt1(q)) and any(zdt1(p) < zdt1(q)): S[i].append(j) elif all(zdt1(q) <= zdt1(p)) and any(zdt1(q) < zdt1(p)): n[i] += 1 if n[i] == 0: rank[i] = 0 fronts[0].append(p) # 构建前沿层 i = 0 while fronts[i]: next_front = [] for p in fronts[i]: for q in S[np.where((pop == p).all(axis=1))[0][0]]: n[q] -= 1 if n[q] == 0: rank[q] = i + 1 next_front.append(pop[q]) i += 1 fronts.append(next_front) return fronts[:-1] def crowding_distance(front): # 计算拥挤度 distances = np.zeros(len(front)) for obj in range(2): # 对每个目标函数排序 sorted_front = front[np.argsort([zdt1(ind)[obj] for ind in front])] distances[0] = distances[-1] = np.inf norm = zdt1(sorted_front[-1])[obj] - zdt1(sorted_front[0])[obj] if norm == 0: continue for i in range(1, len(front)-1): distances[i] += (zdt1(sorted_front[i+1])[obj] - zdt1(sorted_front[i-1])[obj]) / norm return distances # ====== 4. 遗传算子 ====== def crossover_mutation(pop, pc, pm, eta_c, eta_m): # 模拟二进制交叉和多项式变异 offspring = [] for _ in range(len(pop)//2): parents = pop[np.random.choice(len(pop), 2, replace=False)] if np.random.rand() < pc: # SBX交叉 beta = np.where(np.random.rand(*parents[0].shape) < 0.5, (2 * np.random.rand(*parents[0].shape))**(1/(eta_c+1)), (1/(2*(1 - np.random.rand(*parents[0].shape))))**(1/(eta_c+1))) c1 = 0.5 * ((1 + beta)*parents[0] + (1 - beta)*parents[1]) c2 = 0.5 * ((1 - beta)*parents[0] + (1 + beta)*parents[1]) else: c1, c2 = parents[0].copy(), parents[1].copy() # 多项式变异 for child in [c1, c2]: delta = np.where(np.random.rand(*child.shape) < pm, np.where(np.random.rand(*child.shape) < 0.5, (2*np.random.rand(*child.shape))**(1/(eta_m+1)) - 1, 1 - (2*(1 - np.random.rand(*child.shape)))**(1/(eta_m+1))), 0) child = np.clip(child + delta, 0, 1) offspring.extend([c1, c2]) return np.array(offspring) 将上述代码改为一个class类,以供后续调用
04-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值