Given 2 numPy arrays of length N, I would like to create a pairwise 2D array (N x N) based on a custom function.
import numpy as np import pandas as pd A = np.array(A) # size N B = np.array(B) # size N Fij = f(A[i], B[i], A[j], B[j]) # f is a pairwise function of Ai, Bi and Aj, Bj
I would like to create an array C of size NxN. such that
C = Fij # with i, j in range(N)
Example:
A = [1,2,3,4] B = ['a', 'b', 'c', 'd'] def f(i,j): return str(A[i]+A[j])+B[i]+B[j] # f(0,1) = 3ab
I would like to create
C = [[2aa, 3ab, 4ac, 5ad], [3ba, 4bb, 5bc, 6bd], [4ca, 5cb, 6cc, 7cd], [5da, 6db, 7dc, 8dd]]
I know I can do this by nested loop:
C=np.empty([N, N]) for i in range(N): for j in range(N): C[i, j] = f(i,j)
but looking for a neat and more efficient solution.
Advertisement
Answer
In numpy you can make a nice solution by using +
to both add the integers and concatenate the characters.
A = np.array([1,2,3,4]) B = np.char.array(['a', 'b', 'c', 'd']) # char array so can use "+" instead of "add" A0 = A[:,None] + A[None,:] # add the integers B0 = B[:,None] + B[None,:] # concatenate the characters C = A0.astype('<U1') + B0
Or as a single line:
C = (A[:,None] + A[None,:]).astype('<U1') + B[:,None] + B[None,:]
Printing out each line in the above, gives:
A0 [[2 3 4 5] [3 4 5 6] [4 5 6 7] [5 6 7 8]] B0 [['aa' 'ab' 'ac' 'ad'] ['ba' 'bb' 'bc' 'bd'] ['ca' 'cb' 'cc' 'cd'] ['da' 'db' 'dc' 'dd']] C [['2aa' '3ab' '4ac' '5ad'] ['3ba' '4bb' '5bc' '6bd'] ['4ca' '5cb' '6cc' '7cd'] ['5da' '6db' '7dc' '8dd']]