Suppose I have four one-dimensional numpy arrays A, B, C, D, and I want to create a matrix M such that each entry M[i, j, k, l] of the matrix is the tuple (a, b, c, d)
where a = A[i], b = B[j], c = C[k] and d = D[d].
How can I go about constructing it efficiently without loops?
You can create an empty array M with the correct shape (note the 4 in the last dimension -- that's your tuple), and use broadcasting to assign entire rows/columns in M afterwards.
M = np.empty((
len(a), len(b), len(c), len(d), 4
))
M[..., 0] = a[:, None, None, None]
M[..., 1] = b[None, :, None, None]
M[..., 2] = c[None, None, :, None]
M[..., 3] = d[None, None, None, :]
Related
I have a input tensor that is of size [B, N, 3] and I have a test tensor of size [N, 3] . I want to apply a dot product of the two tensors such that I get [B, N] basically. Is this actually possible?
Yes, it's possible:
a = torch.randn(5, 4, 3)
b = torch.randn(4, 3)
c = torch.einsum('ijk,jk->ij', a, b) # torch.Size([5, 4])
Another alternative:
a = torch.randn(5, 4, 3)
b = torch.randn(4, 3)
c = (a * b[None, ...]).sum(dim=-1) # torch.Size([5, 4])
Suppose I have a 4D numpy array A with indexes i, j, k, l for the four dimensions, suppose 50 x 40 x 30 x 20. Also suppose I have some other list B.
How can I set all cells in A that satisfy some condition to 0? Is there a way to do it efficiently without loops (with vectorization?).
Example condition: All cells that have 3rd dimensional index k whereby B[k] == x
For instance,
if we have the 2D matrix A = [[1,2],[3,4]] and B = [7,8]
Then for the 2nd dimension of A (i.e. columns), I want to zero out all cells in the 2nd dimension whereby the index of the cell in that dimension (call the index i), satisfies the condition B[i] == 7. In this case, A will be converted to
A = [[0,0],[3,4]].
You can specify boolean arrays for specific axes:
import numpy as np
i, j, k, l = 50, 40, 30, 20
a = np.random.random((i, j, k, l))
b_k = np.random.random(k)
b_j = np.random.random(j)
# i, j, k, l
a[:, :, b_k < 0.5, :] = 0
# You can alsow combine multiple conditions along the different axes
# i, j, k, l
a[:, b_j > 0.5, b_k < 0.5, :] = 0
# Or work with the index explicitly
condition_k = np.arange(k) % 3 == 0 # Is the index divisible by 3?
# i, j, k, l
a[:, :, condition_k, :] = 0
To work with the example you have given
a = np.array([[1, 2],
[3, 4]])
b = np.array([7, 8])
# i, j
a[b == 7, :] = 0
# array([[0, 0],
# [3, 4]])
Does the following help?
A = np.arange(16,dtype='float64').reshape(2,2,2,2)
A[A == 2] = 3.14
I'm replacing the entry equal to 2 with 3.14. You can set it to some other value.
I have 2 lists:
a=[0,2,0,5]
b=[3,4,5,6]
I want to find remove all the 0 from list a and remove corresponding values(with same index) in list b.
My result should be:
a=[2,5]
b=[4,6]
until now I did:
a = [idx for idx, val in enumerate(a) if val == 0]
and get a=[1,3]
but I don't manage to get the corresponding list in b
a=[0,2,0,5]
b=[3,4,5,6]
a, b = map(list, zip(*[[i, j] for i, j in zip(a, b) if i != 0]))
print(a)
print(b)
Prints:
[2, 5]
[4, 6]
You got a list indexes correctly, to get valid elements from b list the easy way is to do
[b[idx] for idx, val in enumerate(a) if val != 0]
and to get a values
[val for val in a if val != 0]
to do it in one iteration:
x = [(val, b[idx]) for idx, val in enumerate(a) if val != 0]
or
x = [(val_a, val_b) for val_a, val_b in zip(a, b) if val_a != 0]
but it gives you list of tuples, but you can use some python magic to turn it into two lists
a, b = map(list, zip(*x))
I have a Python code partially borrowed from Generating Markov transition matrix in Python:
# xstates is a dictionary
# n - is the matrix size
def prob(xstates, n):
# we want to do smoothing, so create matrix of all 1s
M = [[1] * n for _ in range(n)]
# populate matrix by (row, column)
for key, val in xstates.items():
(row, col) = key
M[row][col] = val
# and finally calculate probabilities
for row in M:
s = sum(row)
if s > 0:
row[:] = [f/s for f in row]
return M
xstates here comes in a form of dictionary, e.g. :
{(2, 2): 387, (1, 2): 25, (0, 1): 15, (2, 1): 12, (3, 2): 5, (2, 3): 5, (6, 2): 4, (5, 6): 4, (4, 2): 2, (0, 2): 1}
where (1, 2) means state 1 transits to state 2 and similar to others.
This function generates the matrix of transition probabilities, the sum of all elements in a row is 1. Now I need to normalize the values. How would I do that? Can I do that with numpy library?
import numpy as np
M = np.random.random([3,2])
print(M)
row sum to 1
M = M / M.sum(axis=1)[:, np.newaxis]
print(M)
column sum to 1
M = M / M.sum(axis=0)[np.newaxis,:]
print(M)
I have a pytorch tensor A, that's of size (n,m) and a list of indices for size n, such that each entry of 0 <= indices[i] < m. For each row i of A, I want to multiply A[i, indices[i]] *= -1, in a vectorized way. Is there an easy way to do this?
A = torch.tensor([[1,2,3],[4,5,6]])
indices = torch.tensor([1, 2])
#desired result
A = [[1,-2,3],[4,5,-6]]
Sure there is, fancy indexing is the way to go:
import torch
A = torch.tensor([[1, 2, 3], [4, 5, 6]])
indices = torch.tensor([1, 2]).long()
A[range(A.shape[0]), indices] *= -1
Remember indices must be torch.LongTensor type. You could cast it if you have float using .long() member function.