gpt4 book ai didi

python - 如何将数据分组到1度纬度的箱中?

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:46 26 4
gpt4 key购买 nike

我有一些地理数据(全局)作为数组:

  • 纬度:lats = ([34.5,34.2,67.8,-24,...])
  • 风速:u = ([2.2,2.5,6,-3,-0.5,...])

我想说明一下风速如何取决于纬度。因此,我想将数据分入 1 度的纬度分箱中。

latbins = np.linspace(lats.min(),lat.(max),180)

我如何计算哪个风速落在哪个箱中。我读到了 pandas.groupby 的内容。这是一个选择吗?

最佳答案

numpy 函数 np.digitize做这个任务。

下面是一个对 bin 中的每个值进行分类的示例:

import numpy as np
import math

# Generate random lats
lats = np.arange(0,10) - 0.5
print("{:20s}: {}".format("Lats", lats))
# Lats : [-0.5 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5]

# Generate bins spaced by 1 from the minus to max values of lats
bins = np.arange(math.floor(lats.min()), math.ceil(lats.max()) +1, 1)
print("{:20s}: {}".format("Bins", bins))
# Bins : [-1 0 1 2 3 4 5 6 7 8 9]

lats_bins = np.digitize(lats, bins)
print("{:20s}: {}".format("Lats in bins", lats_bins))
# Lats in bins : [ 1 2 3 4 5 6 7 8 9 10]

正如 @High Performance Mark 在评论中所建议的,由于您想要以 1 度数划分容器,因此可以使用 floor提取每个lats的下限(注意:如果存在负值,此方法会引入负索引箱):

lats_bins_floor = np.floor(lats)
# lats_bins_floor = lats_bins_floor + abs(min(lats_bins_floor))
print("{:20s}: {}".format("Lats in bins (floor)", lats_bins_floor))
# Lats in bins (floor): [-1. 0. 1. 2. 3. 4. 5. 6. 7. 8.]

关于python - 如何将数据分组到1度纬度的箱中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57379181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com