简体   繁体   English

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

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

I have a for loop that multiplies customer data by an array list based on age and state. The loop works but I need to split the results of the output by customer for my next calculation, any Idea how to do this each customers output varies in length based on age.Example customer us111 has an output of size (12,10,10), and customer us112 has an output of size (11,10,10)我有一个 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)

For this example split is the array list with size (15,10,10), where age is represented by each matrix example split[0]= age 1, split[1] =2 and so on until age 15 is reached.Each matrix has 10 states (AJ) for this case.对于此示例,split 是大小为 (15,10,10) 的数组列表,其中年龄由每个矩阵示例 split[0]= age 1,split[1] =2 等表示,直到达到 15 岁。每个对于这种情况,矩阵有 10 个状态 (AJ)。

customer data below:客户资料如下:

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

The loop puts each users data in order of their current state example us111= np.array([0,1000,0,0,0,0,0,0,0,0])该循环将每个用户的数据按其当前 state 示例的顺序排列us111= np.array([0,1000,0,0,0,0,0,0,0,0])

and then multiplies by the matrices from the customers age until age 15 is reached.然后乘以客户年龄的矩阵,直到达到 15 岁。

The current ouptut of the loop appends each customers results below each other but I need to add their user_id next to each array, it can be in a dataframe as well, it doesnt have to be in an array.循环的当前输出将每个客户结果附加到彼此下方,但我需要在每个数组旁边添加他们的 user_id,它也可以在 dataframe 中,它不必在数组中。

Maybe you can separate it in a dictionary changing the loop:也许你可以把它放在改变循环的字典中:

   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