im trying to find a special Matrix multiplication fully vectorized(without loops). Basically im trying to multipy each row with every row. for examplea=np.array([[1,2],[3,4],[5,6]]) and b=np.array([[1,1],[2,2],[3,3]]) then the result should be([3,7,11],[6,14,22],[9,21,33]])
(first all rows of "a" are multiplied with the first row of b, which gives us (3,7,11) and then the next row of a with all rows of be..)
Here is a solution using np.einsum:
>>> import numpy as np
>>> a = np.array([[1,2],[3,4],[5,6]])
>>> b = np.array([[1,1],[2,2],[3,3]])
>>> np.einsum('kj,ij->ik', a, b)
array([[ 3, 7, 11],
[ 6, 14, 22],
[ 9, 21, 33]])
This is equivalent to b#a.T, or equivalently (a#b.T).T like the other answers have already pointed out.
you can use dot function in numpy.
a = [[1,0],[0,1]]
b = [[4,1],[2,2]]
AdotB = np.dot(a,b)
AdotB is equal to [[4, 1],[2, 2]]
but you should be careful because it will raise ValueError if the last dimension of a is not the same size as the second-to-last dimension of b.
The answer:
a=np.array([[1,2],[3,4],[5,6]])
b=np.array([[1,1],[2,2],[3,3]])
res = a.dot(b.T).T
print('a =\n', a, '\n')
print('b =\n', b, '\n')
print('result =', '\n', res)
The output:
a =
[[1 2]
[3 4]
[5 6]]
b =
[[1 1]
[2 2]
[3 3]]
result =
[[ 3 7 11]
[ 6 14 22]
[ 9 21 33]]
Related
I have 2 numpy arrays, one 2D and the other 1D, for example like this:
import numpy as np
a = np.array(
[
[1, 2],
[3, 4],
[5, 6]
]
)
b = np.array(
[7, 8, 9, 10]
)
I want to get all possible combinations of the elements in a and b, treating a like a 1D array, so that it leaves the rows in a intact, but also joins the rows in a with the items in b. It would look something like this:
>>> combine1d(a, b)
[ [1 2 7] [1 2 8] [1 2 9] [1 2 10]
[3 4 7] [3 4 8] [3 4 9] [3 4 10]
[5 6 7] [5 6 8] [5 6 9] [5 6 10] ]
I know that there are slow solutions for this (like a for loop), but I need a fast solution to this as I am working with datasets with millions of integers.
Any ideas?
This is one of those cases where it's easier to build a higher dimensional object, and then fix the axes when you're done. The first two dimensions are the length of b and the length of a. The third dimension is the number of elements in each row of a plus 1. We can then use broadcasting to fill in this array.
x, y = a.shape
z, = b.shape
result = np.empty((z, x, y + 1))
result[...,:y] = a
result[...,y] = b[:,None]
At this point, to get the exact answer you asked for, you'll need to swap the first two axes, and then merge those two axes into a single axis.
result.swapaxes(0, 1).reshape(-1, y + 1)
An hour later. . . .
I realized by being a little bit more clever, I didn't need to swap axes. This also has the nice benefit that the result is a contiguous array.
def convert1d(a, b):
x, y = a.shape
z, = b.shape
result = np.empty((x, z, y + 1))
result[...,:y] = a[:,None,:]
result[...,y] = b
return result.reshape(-1, y + 1)
this is very "scotch tape" solution:
import numpy as np
a = np.array(
[
[1, 2],
[3, 4],
[5, 6]
]
)
b = np.array(
[7, 8, 9, 10]
)
z = []
for x in b:
for y in a:
z.append(np.append(y, x))
np.array(z).reshape(3, 4, 3)
You need to use np.c_ to attach to join two dataframe. I also used np.full to generate a column of second array (b). The result are like what follows:
result = [np.c_[a, np.full((a.shape[0],1), x)] for x in b]
result
Output
[array([[1, 2, 7],
[3, 4, 7],
[5, 6, 7]]),
array([[1, 2, 8],
[3, 4, 8],
[5, 6, 8]]),
array([[1, 2, 9],
[3, 4, 9],
[5, 6, 9]]),
array([[ 1, 2, 10],
[ 3, 4, 10],
[ 5, 6, 10]])]
The output might be kind of messy. But it's exactly like what you mentioned as your desired output. To make sure, you cun run below to see what comes from the first element in the result array:
print(result[0])
Output
array([[1, 2, 7],
[3, 4, 7],
[5, 6, 7]])
Is there a simple way to lock/freeze an element within a Numpy Array. I would like to do several operations over a Numpy Array in python while keeping some specific values as they are originally.
for example,
if a have a Numpy Array a ;
[[ 1 3 4 5],
[6 7 8 0],
[9 10 11 2]]
and another Numpy Array b ;
[[2 0 4 10],
[11 5 12 3],
[6 8 7 9]]
and c = a+b but keeping the original values of 3, 8 and 2 in a.
My arrays are quite big and I would like a solution where I don't have to use a for loop, an if statement or something similar.
You can use np.isin to build a mask, and then np.where to populate from either a or a+b depending on on the result:
m = np.isin(a, [3,8,2])
c = np.where(m, a, a+b)
Or as #hpaulj suggests, you could also use where and out in np.add, which would modify a in-place:
np.add(a, b, where=~np.isin(a,[3,8,2]), out=a)
array([[ 3, 3, 8, 15],
[17, 12, 8, 3],
[15, 18, 18, 2]])
I have a 2-D array for example:
p = np.array([[21,2,3,1,12,13],
[4,5,6,14,15,16],
[7,8,9,17,18,19]])
b = np.argpartition(p, np.argmin(p, axis=1))[:, -2:]
com = np.ones([3,6],dtype=np.int)
com[np.arange(com.shape[0])[:,None],b] = 0
print(com)
b is the indices of top 2 values of each row in p:
b = [[0 5]
[4 5]
[4 5]]
com is np.ones matrix, the same size as p, the element whose index is same as b will change to 0.
So the result is :
com = [[0 1 1 1 1 0]
[1 1 1 1 0 0]
[1 1 1 1 0 0]]
Now I have one more constraint :
p[0:2,0:2]
The numbers in these area should not be considered,
so the result should be:
b = [[4 5]
[4 5]
[4 5]]
How can I do this ?
Thanks in advance!
Make sure your question is clear. Not sure I understand your constraints. Here's a take:
# the data
p = np.array([[21, 2, 3, 1, 12, 13],
[4, 5, 6, 14, 15, 16],
[7, 8, 9, 17, 18, 19]])
# not sure if this is what you mean by constraint
# but lets ignore values in first two cols and rows
p[0:2, 0:2] = 0
# return the idx of highest values
b = np.argpartition(p, -2)[:, -2:]
Let A,C and B be numpy arrays with the same number of rows.
I want to update 0th element of A[0], 2nd element of A[1] etc. That is, update B[i]th element of A[i] to C[i]
import numpy as np
A = np.array([[1,2,3],[3,4,5],[5,6,7],[0,8,9],[3,7,5]])
B = np.array([0,2,1,2,0])
C = np.array([8,9,6,5,4])
for i in range(5):
A[i, B[i]] = C[i]
print ("FOR", A)
A = np.array([[1,2,3],[3,4,5],[5,6,7],[0,8,9],[3,7,5]])
A[:,B[:]] = C[:]
print ("Vectorized, A", A)
Output:
FOR [[8 2 3]
[3 4 9]
[5 6 7]
[0 8 5]
[4 7 5]]
Vectorized, A [[4 6 5]
[4 6 5]
[4 6 5]
[4 6 5]
[4 6 5]]
The for loop and vectorization gave different results.
I am unsure how to vectorize this for loop using Numpy.
The reason that your approach doesn't work is that you're passing the whole B as the column index and replace them with C instead you need to specify both row index and column index. Since you just want to change the first 4 rows you can simply use np.arange(4) to select the rows B[:4] the columns and C[:4] the replacement items.
In [26]: A[np.arange(4),B[:4]] = C[:4]
In [27]: A
Out[27]:
array([[8, 2, 3],
[3, 4, 9],
[5, 6, 7],
[0, 8, 5],
[3, 7, 5]])
Note that if you wanna update the whole array, as mentioned in comments by #Warren you can use following approach:
A[np.arange(A.shape[0]), B] = C
I'm having a hard time on doing this. I have two m x n matrices (A and B) and I need to multiply every column of A by the rows in B, to generate a m x (n*n) matrix. I guess I wasn't very clear in the explanation so I'll post an example:
A =
[1 2
3 4]
B =
[5 6
7 8]
I wish to have:
[[5 6] [10 12]
[21 24] [28 32]]
I was able to do it using for loops but I want to avoid for as much as possible. Also using numpy to all this and all data is stored as np.array.
Maybe:
>>> A = np.array([[1,2],[3,4]])
>>> B = np.array([[5,6],[7,8]])
>>> (A * B[None, :].T).T
array([[[ 5, 6],
[21, 24]],
[[10, 12],
[28, 32]]])
where we use None to add an extra dimension to B, and a few transpositions to get the alignment right.
If I understand you right, you want basic ( m * n ) multiplication right? Use numpy.dot():
>>> a = [[1, 0], [0, 1]]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([[4, 1],
[2, 2]])