Is there a way to copy values from one numpy masked array to another where ONLY the unmasked values are copied and the target values are left unchanged for masked source values? It seems like this should be handled automatically, but so far I haven't found a good way to do it. Right now I'm using ma.choose with the target region of the destination and the mask, but it really seems like there should be a better way given that the entire purpose of the masked array is to not operate on masked values automatically.
import numpy as np
from numpy import ma
x = ma.array([1, 2, 3, 4], mask=[0, 1, 1, 0])
y = np.array([5, 6, 7, 8])
y[~x.mask] = x[~x.mask]
which gives for y:
array([1, 6, 7, 4])
I want to turn a 1d array into a sorted 2d array. The 1d array looks like this:
[1,5,8,9,9,1,4,6,7,8,41,4,5,31,6,11]
First, I want to split this array up into a 2d array with a width of 4.
[[1,5,8,9]
[9,1,4,6]
[7,8,41,4]
[5,31,6,11]
]
Then, I want to sort the 2d array from the 3rd value in the 2d array like this:
[[9,1,4,6],
[5,31,6,11],
[1,5,8,9],
[7,8,41,4]
]
I am anticipating that the 1d array will be much larger, so I do not want to manually create the 2d array. How do I approach this?
If you can't use numpy, you can do it like this:
a = [1,5,8,9,9,1,4,6,7,8,41,4,5,31,6,11]
result = []
l = len(a)
for i in range(0, l, 4):
result.append(a[i:i+4])
result = sorted(result, key = lambda a: a[2])
# result is [[9, 1, 4, 6], [5, 31, 6, 11], [1, 5, 8, 9], [7, 8, 41, 4]]
You can try numpy array_split
import numpy as np
a=[1,5,8,9,9,1,4,6,7,8,41,4,5,31,6,11]
b=np.array_split(arr, len(a)/4)
for c in b:
c.sort()
If you use numpy.argsort, you can sort it easily.
import numpy as np
arr = np.array([1,5,8,9,9,1,4,6,7,8,41,4,5,31,6,11])
arr_2d = np.reshape(arr, (4,4))
sorted_arr = arr_2d[np.argsort(arr_2d[:, 2])]
I have following numpy arrays:
whole = np.array(
[1, 0, 3, 0, 6]
)
sparse = np.array(
[9, 8]
)
Now I want to replace every zero in the whole array in chronological order with the items in the sparse array. In the example my desired array would look like:
merged = np.array(
[1, 9, 3, 8, 6]
)
I could write a small algorithm by myself to fix this but if someone knows a time efficient way to solve this I would be very grateful for you help!
Do you assume that sparse has the same length as there is zeros in whole ?
If so, you can do:
import numpy as np
from copy import copy
whole = np.array([1, 0, 3, 0, 6])
sparse = np.array([9, 8])
merge = copy(whole)
merge[whole == 0] = sparse
if the lengths mismatch, you have to restrict to the correct length using len(...) and slicing.
I have a list of numpy arrays. My list contains 5000 numpy arrays and each one has the size (1x1000). I want to construct a numpy array of size 5000x1000. I am trying to do something like:
db_array = np.asarray(db_list) # my db_list has 5000 samples of 1x1000 size
The result was a matrix of size (5000, 1, 1000). How can I construct a matrix with size (5000, 1000)?
An MCVE would help here, but if I understand correctly, just use the numpy.array constructor.
>>> import numpy as np
>>> arraylist = [np.array([1,2,3]), np.array([1,2,3])]
>>> arraylist
[array([1, 2, 3]), array([1, 2, 3])]
>>> np.array(arraylist)
array([[1, 2, 3],
[1, 2, 3]])
So, just initialize the list as a simple numpy array
import numpy as np
list = [np.array([1,2,3]), np.array([1,2,3])]
new_array = np.array(list)
print (new_array)
print (new_array.shape)
I am using numpy in python. I have a 1D(nx1) array and a 2D(nxm) array. I used argsort to get a indice of the 1D array. Now I want to use that indice to sort my 2D(nxm) array's colum.
I want to know how to do it?
For example:
>>>array1d = np.array([1, 3, 0])
>>>array2d = np.array([[1,2,3],[4,5,6]])
>>>array1d_indice = np.argsort(array1d)
array([2, 0, 1], dtype=int64)
I want use array1d_indice to sord array2d colum to get:
[[3, 1, 2],
[6, 4, 5]]
Or anyway easier to achieve this is welcome
If what you mean is that you want the columns sorted based on the vector, then you use argsort on the vector:
vi = np.argsort(vector)
then to arrange the columns of array in the right order,
sorted = array[:, tuple(vi)]
to get rows, switch around the order of : and tuple(vi)