I've been going over the numpy docs looking for a specific operation. The words I would use for this are "overlay" or "mask" but the numpy concepts of those words don't seem to match mine.
I want to take two arrays, one dense and one sparse and combine them thusly:
[ 1, 2, 3, 4, 5 ]
X [ N, N, 10, N, 12 ]
= [ 1, 2, 10, 4, 12 ]
where X is the operation and N is None, or Null, -1, or some other special character.
How is this achieved in numpy/python3?
You can use np.where:
# pick special value
N = -1
dns = [ 1, 2, 3, 4, 5 ]
sprs = [ N, N, 10, N, 12 ]
# this is important otherwise the comparison below
# is not done element by element
sprs = np.array(sprs)
# tada!
np.where(sprs==N,dns,sprs)
# array([ 1, 2, 10, 4, 12])
When called with three arguments m,a,b where "mixes" a and b taking elements from a where m is True and from b where it is False.
You can "fill" the masked array, with np.ma.filled(..) [numpy-doc], for example:
>>> a
array([1, 2, 3, 4, 5])
>>> b
masked_array(data=[--, --, 10, --, 12],
mask=[ True, True, False, True, False],
fill_value=999999)
>>> b.filled(a)
array([ 1, 2, 10, 4, 12])
>>> np.ma.filled(b, a)
array([ 1, 2, 10, 4, 12])
Here we thus fill the masked values from b with the corresponding values of a.
Related
suppose I have:
>>> a = torch.tensor([1, 2, 3, 0, 0, 1])
>>> b = torch.tensor([0, 1, 3, 3, 0, 0])
I want to update b with elements in a if it's not zero. How can I beneficently do that?
Expected:
>>> b = torch.tensor([1, 2, 3, 3, 0, 1])
To add to the previous answer and for more simplicity you can do it by one line of code:
b = torch.where(a!=0,a, b)
Output:
tensor([1, 2, 3, 3, 0, 1])
torch.where is your answer. I assume based on your example that you also want to replace only elements in a that are 0.
mask = torch.logical_and(b!=0,a==0)
output = torch.where(mask,b,a)
I have two arrays as follows and would like to get the number of rows by function .shape.
X = np.array([0, 4, 3, 5, 1, 2])
Y = np.array([[-1, 0, 4, 4],
[ 1, 0, 5, 0],
[ 2, 7, 4, 0],
[ 3, 0, 4, 9],
[ 4, 6, 4, 0]])
X.shape[0]
Y.shape[0]
The result is
6
5
Because X is a matrix with 1 row, I expect X.shape[0] returns 1. However, it returns 6 which is the number of columns. Could you please suggest a function to achieve my goal?
From #Divakar's comment, the command to achieve this goal is np.atleast_2d.
I'm doing matrix factorization whose output matrix has non-integer entries. To make the display more compact, I would like to ask how to limit the number of decimals of results. Thank you so much!
import numpy as np
import scipy.linalg as la
A = np.array([[1, 0, 8, 7, 8, 1],
[0, 1, 2, 8, 9, 2],
[5, 1, 0, 6, 10, 3],
[5, 4, 4, 8, 10, 4]])
(P, L, U) = la.lu(A)
F = np.array(sorted(U, key=lambda x: tuple(x!=0), reverse=False))
print(F)
[[ 0. 0. 0. 6.85483871 8.51612903 1.62903226]
[ 0. 0. 8.26666667 5.93333333 6. 0.46666667]
[ 0. 3. 4. 2. 0. 1. ]
[ 5. 1. 0. 6. 10. 3. ]]
PS: I would like to ask for a global setting instead of repeating apply the function round to each output.
You can use numpy.around:
import numpy as np
import scipy.linalg as la
A = np.array([[1, 0, 8, 7, 8, 1],
[0, 1, 2, 8, 9, 2],
[5, 1, 0, 6, 10, 3],
[5, 4, 4, 8, 10, 4]])
(P, L, U) = la.lu(A)
F = np.array(sorted(U, key=lambda x: tuple(x!=0), reverse=False))
F = list(map(lambda x: np.around(x,2),F)) # 2 here is the number of decimals
print(F)
I need to do some calculations with a NetCDF file.
So I have two variables with following dimensions and sizes:
A [time | 1] x [lev | 12] x [lat | 84] x [lon | 228]
B [lev | 12]
What I need is to produce a new array, C, that is shaped as (1,12,84,228) where B contents are propagated to all dimensions of A.
Usually, this is easily done in NCL with the conform function. I am not sure what is the equivalent of this in Python.
Thank you.
The numpy.broadcast_to function can do something like this, although in this case it does require B to have a couple of extra trailing size 1 dimension added to it to satisfy the numpy broadcasting rules
>>> import numpy
>>> B = numpy.arange(12).reshape(12, 1, 1)
>>> B
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> B = B.reshape(12, 1, 1)
>>> B.shape
(12, 1, 1)
>>> C = numpy.broadcast_to(b, (1, 12, 84, 228))
>>> C.shape
(1, 12, 84, 228)
>>> C[0, :, 0, 0]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> C[-1, :, -1, -1]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
I have to numpy.ndarray A & B which are of the following shape
A=(500000,784),B =(500000,).I need to add these 2 arrays in a way that the array B , which has labels gets added as the 785th column in the array without changing any sequence in its row- wise data.
i.e, A becomes of shape (500000,785).
np.append(A.T,[B.T], axis=0).T
For example:
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,9,11]])
B = np.array([4,5,3,6])
np.append(A.T,[B.T], axis=0).T
Output:
array([[ 1, 2, 3, 4],
[ 4, 5, 6, 5],
[ 7, 8, 9, 3],
[10, 9, 11, 6]])