roberto tomás
roberto tomás

Reputation: 4697

summing elements in a list of lists

I have a list like:

[array([ 0.6,  0.6, -0.6, -0.6, -0.6, -0.6, -0.6, -0.6, -0.6, -0.6]), array([ 0.35,  0.35,  0.35,  0.35,  0.35,  0.35,  0.35,  0.35, -0.35,
       -0.35])]

and I can guarantee that each sublist is the same size. I need to sum every element at each position (producing one list of the same size as the sublists).

This is generated like this:

    sums = []
    for key in est_dict.keys():
        model, alpha = est_dict[key]

        fX = model.predict(X)
        y_fx = np.where(fX==0, -1, fX)
        sums.append(y_fx * alpha)

if there's a way to build the sum immediately, without knowing the size of the sublists in advance, that would be even better.

Upvotes: 1

Views: 63

Answers (1)

CrafterKolyan
CrafterKolyan

Reputation: 1052

np.sum(sums, axis=0)

You even don't need to create a new 2D numpy array.

Upvotes: 1

Related Questions