float() argument must be a string or a number not map

win10下的python3程序报错如题,

TypeError: float() argument must be a string or a number, not 'map'

具体出错的语句:

image_batch = np.array(map(lambda x: ndimage.imread(x, mode='RGB'), image_batch_file)).astype(np.float32)

网上查到map外要加list(),改成下面语句就解决了:

image_batch = np.array(list(map(lambda x: ndimage.imread(x, mode='RGB'), image_batch_file))).astype(np.float32)

import requests import numpy as np import time from itertools import combinations from concurrent.futures import ThreadPoolExecutor, as_completed # 新增并发处理库 # ========== 配置参数优化 ========== BAIDY_KEY = "您的百度API密钥" CITY_COORDINATES = { "北京": (39.9042, 116.4074), "上海": (31.2304, 121.4737), "广州": (23.1291, 113.2644), "成都": (30.5728, 104.0668) } MAX_WORKERS = 5 # 控制并发线程数,根据API限制调整 # ========== 优化后的坐标系转换 ========== def wgs84_to_bd09(lat, lng): """ 使用向量化计算优化三角函数运算 """ x_pi = np.pi * 3000.0 / 180.0 x, y = np.radians(lng), np.radians(lat) z = np.hypot(x, y) + 0.00002 * np.sin(y * x_pi) theta = np.arctan2(y, x) + 0.000003 * np.cos(x * x_pi) return z * np.sin(theta) + 0.006, z * np.cos(theta) + 0.0065 # ========== 优化后的距离获取函数 ========== def get_baidu_distance(origin, destination, retry=3): """ 增加缓存机制避免重复计算 """ if not hasattr(get_baidu_distance, "cache"): get_baidu_distance.cache = {} cache_key = frozenset([origin, destination]) if cache_key in get_baidu_distance.cache: return get_baidu_distance.cache[cache_key] # 原有请求逻辑... # 此处保持原有实现,实际返回时写入缓存 get_baidu_distance.cache[cache_key] = result return result # ========== 并行化距离矩阵生成 ========== def generate_baidu_matrix(coords_dict, max_workers=MAX_WORKERS): """ 使用线程池并行请求 """ cities = list(coords_dict.keys()) n = len(cities) matrix = np.full((n, n), np.nan) pairs = list(combinations(range(n), 2)) with ThreadPoolExecutor(max_workers=max_workers) as executor: futures = { executor.submit( get_baidu_distance, coords_dict[cities[i]], coords_dict[cities[j]] ): (i, j) for i, j in pairs } for future in as_completed(futures): i, j = futures[future] try: distance = future.result() matrix[i][j] = matrix[j][i] = distance or np.nan except Exception as e: print(f"处理({cities[i]}-{cities[j]})失败: {str(e)}") return matrix, cities # ========== 主程序 ========== if __name__ == "__main__": start_time = time.time() distance_matrix, city_order = generate_baidu_matrix(CITY_COORDINATES) print(f"\n总耗时: {time.time()-start_time:.2f}秒") print("\n城市顺序:", city_order) print("\n优化后距离矩阵(公里):") print(np.round(distance_matrix, 1))处理两点距离失败,float() argument must be a string or a number, not 'module',优化后把代码发给我
最新发布
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xqlily

鼓励原创

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值