An example in the np.tensordot
docs might help. It creates a object array of strings, and shows that dot
produces string replication.
For strings and list *
means replication
In [134]: 'abc'*3
Out[134]: 'abcabcabc'
Your arrays:
In [126]: a
Out[126]: array([[1, 2, 3], [100, 200]], dtype=object)
In [127]: b
Out[127]: array([2, 4])
and a string array (I could have also done a[:]=['a','B']
)
In [128]: c = np.array(['a','B'],dtype=object)
In [129]: c
Out[129]: array(['a', 'B'], dtype=object)
element multiplication - replicate the elements of a
2 and 3 times.
In [130]: a*b
Out[130]: array([[1, 2, 3, 1, 2, 3], [100, 200, 100, 200, 100, 200, 100, 200]], dtype=object)
dot
product - same thing, but it 'sums' the values after * - and sum for list is concatenation.
In [131]: a.dot(b)
Out[131]: [1, 2, 3, 1, 2, 3, 100, 200, 100, 200, 100, 200, 100, 200]
Same thing happens with the array of characters, replication and concatenation.
In [132]: c*b
Out[132]: array(['aa', 'BBBB'], dtype=object)
In [133]: c.dot(b)
Out[133]: 'aaBBBB'
When dealing with an object dtype array, numpy essentially iterates over the elements and applies the operator/method defined the class of that object.
a*b
is in effect
In [147]: [i*j for i,j in zip(a,b)]
Out[147]: [[1, 2, 3, 1, 2, 3], [100, 200, 100, 200, 100, 200, 100, 200]]
The numpy version is a little fancier in that can handle multidimensional arrays and use broadcasting.