I am doing very simple subtraction and I cannot get it right. I'd like to subtract the first column from every column of an array. For example:
winner=
[1,2,3]
[2,3,5]
[3,5,7]
I'd like to get the result like this:
[0,1,2]
[0,1,3]
[0,2,4]
Here is the error, I am not sure how to do it.
winner.shape
(5, 3)
winner_difference=winner-winner[:,0]
winner_difference
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-69-d385252ef2de> in <module>
----> 1 winner_difference=winner-winner[:,0].T
2 winner_difference
ValueError: operands could not be broadcast together with shapes (5,3) (5,)
CodePudding user response:
A more efficient method, taking advantage of numpy broadcasting:
output = winner - winner[:, 0, None]
Output:
>>> output
array([[0, 1, 2],
[0, 1, 3],
[0, 2, 4]])
CodePudding user response:
This is a pretty trivial thing to achieve with list comprehensions.
>>> winner = [[1,2,3], [2,3,5], [3,5,7]]
>>> [[y - x[0] for y in x] for x in winner]
[[0, 1, 2], [0, 1, 3], [0, 2, 4]]
>>>
CodePudding user response:
I believe you are looking for:
winner - np.vstack(winner[0])