繁体   English   中英

如何拆分多维数组的结果 python

[英]How to split results of a multi-dimensional array python

我有一个 for 循环,将客户数据乘以基于年龄和 state 的数组列表。该循环有效,但我需要按客户拆分 output 的结果以进行下一次计算,任何想法如何做到这一点每个客户 output 各不相同基于年龄的长度。示例客户 us111 的大小为 output (12,10,10),客户 us112 的大小为 output (11,10,10)

split=np.random.rand(15, 10, 10)
age = [3,4,9]
customers = np.array([
    [0,1000,0,0,0,0,0,0,0,0],
    [0,0,0,4890,0,0,0,0,0,0],
    [0,0,0,0,624,0,0,0,0,0],
])
results = []
for c, y in zip(customers, age):
    for m in split[y:]:
        c = c @ m
        results.append(c)
results = np.stack(results)

对于此示例,split 是大小为 (15,10,10) 的数组列表,其中年龄由每个矩阵示例 split[0]= age 1,split[1] =2 等表示,直到达到 15 岁。每个对于这种情况,矩阵有 10 个状态 (AJ)。

客户资料如下:

user_id | current_state | age   | amount
us111   |   B           |   3   |  1000
us112   |   D           |   4   |  4890
us113   |   E           |   9   |  624

该循环将每个用户的数据按其当前 state 示例的顺序排列us111= np.array([0,1000,0,0,0,0,0,0,0,0])

然后乘以客户年龄的矩阵,直到达到 15 岁。

循环的当前输出将每个客户结果附加到彼此下方,但我需要在每个数组旁边添加他们的 user_id,它也可以在 dataframe 中,它不必在数组中。

也许你可以把它放在改变循环的字典中:

   results_dict = {}
    for _id, c, y in zip(user_id ,customers, age):
        results = []
        for m in split[y:]:
            c = c @ m
            results.append(c)
    results_dict[_id] = results

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM