微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

np.astype 不改变数组类型

如何解决np.astype 不改变数组类型

我有很多数据集正在摄取,其中包括纬度和经度值。其中之一的 lat 和 lon 字段设置为 float64,而其他所有人都使用 float32。所以,我最初只是尝试简单地转换类型 - 除了类型转换似乎没有发生。

更糟糕的是,我没有收到任何错误消息,因此很难排除故障。

我这里有什么明显的错误吗? lat_ref 应该存在于一个数组中,最初是一个 Float64。我想用值为 Float32 的数组替换该数组,并且我不介意这些值是否被截断以匹配 float32,因为带有 float64 的纬度将以纳米为单位进行测量。

示例代码

lat_ref = nc.variables['lat'][:] #This is where I read in the lat from my data
lat_ref = lat_ref.astype(np.float32)

print("The shape of lat_ref is: " + str(np.shape(lat_ref)))
print("The dtype of lat_ref is: " + str(lat_ref.dtype))

打印结果为:

The shape of lat_ref is: (681,)
The dtype of lat_ref is: float64

我的其他数据集对打印的响应(没有 .astype 转换)如下:

The shape of lat_ref is: (408,)
The dtype of lat_ref is: float32

我希望变换使 lat_ref 像上面那样。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。