简体   繁体   中英

Numpy adding rows each step and then work by columns

I'm trying in Python using Numpy to do the following.

Receive every step a row of values. Call a function for each column.

To make it simple: assume I call a function: GetRowOfValues()

And after 5 rows I want to sum each column.

And return a full row which is the sum of all 5 rows received.

Anyone has an idea how to implement to using numpy?

Thanks for the help

I'm assuming that rows have a fixed length n and that their values are of float data type.

import numpy as np
n = 10 # adjust according to your need
cache = np.empty((5, n), dtype=float) # allocate empty 5xn array
cycle = True
while cycle:
    for i in range(5):
        cache[i,:] = GetRowOfValues() # save result of function call in i-th row
    column_sum = np.sum(cache, axis=0) # sum by column
    # your logic here...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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