Fill numpy array with other numpy array - python

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.

Related

How can I retrieve indptr, indices from scipy csr_matrix?

According to the documentation of scipy.sparse.csr_matrix, there are many ways to create a csr_matrix. One of the ways is to give data, indptr, and indices as inputs. I wonder, if there is a way to retrieve them in the opposite direction, i.e., assume that I have my_csr_matrix created as below:
>>> indptr = np.array([0, 2, 3, 6])
>>> indices = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> my_csr_matrix = csr_matrix((data, indices, indptr), shape=(3, 3))
>>> my_csr_matrix.toarray()
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
The question is, how can I retrieve indptr and indices from my_csr_matrix without knowing the information about them or data before-hand explicitly?
Literally just my_csr_matrix.indptr and my_csr_matrix.indices. You can also get the data array with my_csr_matrix.data.
These attributes are documented further down the page, under the "Attributes" heading.
Note that these are the actual underlying arrays used by the sparse matrix representation. Modifying these arrays will modify the sparse matrix, unless you do something that causes the CSR matrix to allocate new underlying arrays. (For example, my_csr_matrix[1, 1] = 7 would change the sparsity structure of the matrix and require allocating new arrays.)

Create sorted 2D array from 1D array

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])]

sort numpy 2d array by indice of column

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)

Need help converting Matlab's bsxfun to numpy

I'm trying to convert a piece of MATLAB code, and this is a line I'm struggling with:
f = 0
wlab = reshape(bsxfun(#times,cat(3,1-f,f/2,f/2),lab),[],3)
I've come up with
wlab = lab*(np.concatenate((3,1-f,f/2,f/2)))
How do I reshape it now?
Not going to do it for your code, but more as a general knowledge:
bsxfun is a function that fills a gap in MATLAB that python doesn't need to fill: broadcasting.
Broadcasting is a thing where if a matrix that is being multiplied/added/whatever similar is not the same size as the other one being used, the matrix will be repeated.
So in python, if you have a 3D matrix A and you want to multiply every 2D slice of it with a matrix B that is 2D, you dont need anything else, python will broadcast B for you, it will repeat the matrix again and again. A*B will suffice. However, in MATLAB that will raise an error Matrix dimension mismatch. To overcome that, you'd use bsxfun as bsxfun(#times,A,B) and this will broadcast (repeat) B over the 3rd dimension of A.
This means that converting bsxfun to python generally requires nothing.
MATLAB
reshape(x,[],3)
is the equivalent of numpy
np.reshape(x,(-1,3))
the [] and -1 are place holders for 'fill in the correct shape here'.
===============
I just tried the MATLAB expression is Octave - it's on a different machine, so I'll just summarize the action.
For lab=1:6 (6 elements) the bsxfun produces a (1,6,3) matrix; the reshape turns it into (6,3), i.e. just removes the first dimension. The cat produces a (1,1,3) matrix.
np.reshape(np.array([1-f,f/2,f/2])[None,None,:]*lab[None,:,None],(-1,3))
For lab with shape (n,m), the bsxfun produces a (n,m,3) matrix; the reshape would make it (n*m,3)
So for a 2d lab, the numpy needs to be
np.array([1-f,f/2,f/2])[None,None,:]*lab[:,:,None]
(In MATLAB the lab will always be 2d (or larger), so this 2nd case it closer to its action even if n is 1).
=======================
np.array([1-f,f/2,f/2])*lab[...,None]
would handle any shaped lab
If I make the Octave lab (4,2,3), the `bsxfun is also (4,2,3)
The matching numpy expression would be
In [94]: (np.array([1-f,f/2,f/2])*lab).shape
Out[94]: (4, 2, 3)
numpy adds dimensions to the start of the (3,) array to match the dimensions of lab, effectively
(np.array([1-f,f/2,f/2])[None,None,:]*lab) # for 3d lab
If f=0, then the array is [1,0,0], so this has the effect of zeroing values on the last dimension of lab. In effect, changing the 'color'.
It is equivalent to
import numpy as np
wlab = np.kron([1-f,f/2,f/2],lab.reshape(-1,1))
In Python, if you use numpy you do not need to do any broadcasting, as this is done automatically for you.
For instance, looking at the following code should make it clearer:
>>> import numpy as np
>>> a = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8], [9, 10, 100]])
>>> b = np.array([1, 2, 3])
>>>
>>> a
array([[ 1, 2, 3],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 100]])
>>> b
array([1, 2, 3])
>>>
>>> a - b
array([[ 0, 0, 0],
[ 2, 2, 2],
[ 5, 5, 5],
[ 8, 8, 97]])
>>>

Finding what elements are in a 2-D array

I'm trying to find what elements are in a 2-D array, such as something along the lines below:
import numpy as np
a = np.array([[1,0,0],[1,3,0],[2,7,4]])
print find_element(a)
[0,1,2,3,4,7]
Is there a function that would do this for me?
You could use np.unique:
>>> a = np.array([[1,0,0],[1,3,0],[2,7,4]])
>>> np.unique(a)
array([0, 1, 2, 3, 4, 7])

Categories

Resources