A H
A H

Reputation: 2570

Multiply vector by vector, scalar-wise - numpy

Can a vectorised calculation be done, where each column in one of the vectors is treated as a scalar?

Say you have two numpy arrays:

a = np.array([(True, False),
              (True, True),
              (False, True),
              (True, False),
              (True, True),
              (False, True),
              (True, True)
             ])

b = np.array([[1, 3, 8, 3, 8, 3, 8],
              [4, 8, 6, 8, 6, 8, 6],
              [5, 9, 4, 9, 4, 9, 4],
              [6, 2, 3, 2, 3, 2, 3],
              [7, 4, 1, 4, 1, 4, 1],
              [8, 9, 9, 9, 9, 9, 9],
              [9, 1, 1, 1, 1, 1, 1]]) 

and want to multiply each column of a by the entire vector b, sum it and return it as a (or to a dataframe). Can this be done vectorised (without a for loop)?

df = pd.DataFrame(np.zeros_like(a))

for column in range(a.shape[1]):
    scalar_of_a = a[:, column][:, None]
    vector_of_a_b = scalar_of_a*b
    df.loc[:, column] = vector_of_a_b.sum(axis=0)[:, None]

Upvotes: 1

Views: 776

Answers (1)

Divakar
Divakar

Reputation: 221584

Use matrix-multiplication -

df = pd.DataFrame(b.T.dot(a)) # or pd.DataFrame(a.T.dot(b).T)

With np.einsum -

df = pd.DataFrame(np.einsum('ij,il->lj',a,b))

Upvotes: 2

Related Questions