Sliding window on a 2D numpy array [duplicate] - python-3.x

This question already has answers here:
Sliding windows from 2D array that slides along axis=0 or rows to give a 3D array
(2 answers)
Closed 4 years ago.
I have a 64x64 numpy array and I have a 5x64 window. I want to slide this window over the main numpy array with a step size of 1 and save the values that lie in that window in a column in an empty numpy array.
Thanks

Exactly as you said in the comment, use the array index and incrementally iterate. Create a list (a in my case) to hold your segmented windows (window). In the end, use np.hstack to concatenate them.
import numpy as np
yourArray = np.random.randn(64,64) # just an example
winSize = 5
a = [] # a python list to hold the windows
for i in range(0, yourArray.shape[0]-winSize+1):
window = yourArray[i:i+winSize,:].reshape((-1,1)) # each individual window
a.append(window)
result = np.hstack(a)

Related

How to create a variable number of dimensions for mgrid

I would like to create a meshgrid of variable dimensions by specifying the dimensions with a variable i.e. specifying dim=2, rather than manually changing the expression as in the example below to set a 2D mesh grid.
How would I implement a wrapper function for this?
The problem stems from not being familiar with the syntax that mgrid uses (index_tricks).
import numpy as np
mgrid = np.mgrid[
-5:5:5j,
-5:5:5j,
]
Observed documentation for mgrid, but there seems to be no info on setting the number of dimensions with a variable.
You can create a tuple containing the slice manually, and repeat it some number of times:
import numpy as np
num_dims = 2
mgrid = np.mgrid[(slice(-5, 5, 5j),) * num_dims]

Reshaping a nested Numpy Arrays using reshape [duplicate]

This question already has answers here:
Transposing a 1D NumPy array
(15 answers)
Closed 2 years ago.
I have a numpy array :
ar = [[1,2],[3,4],[5,6]]
I want to convert it to :
ar = [[1,3,5],[2,4,6]]
I have tried numpy reshape function it did not work.
Is there any way to reshape it using numpy reshape method or any other way??
Thank you.
Use Transpose (.T)
ar = np.array([[1,2],[3,4],[5,6]])
ar.T

python 3 numpy save multiple arrays

I have 3 arrays and one list:
array1.shape = (1000,5,5,7)
array2.shape = (1000,)
array3.shape = (1000,)
len(list1) = (1000)
I want to save all of these to a numpy file. When I used to run in Python 2.7, the command I used to use was:
np.save(filename,[array1, array2, array3, list1])
And everything worked great, including loading all of the data with np.load. However, when I try this command in Python 3.6 I get an error:
could not broadcast input array from shape (1000,5,5,7) into shape (1000)
How am I able to save the 3 arrays as well as the list into a single numpy array in Python 3.6?

How to delete an element by index from a numpy array in Python 3? [duplicate]

This question already has answers here:
How to remove specific elements in a numpy array
(13 answers)
Closed 3 years ago.
I want to delete an element from a numpy array by index.
The commands
arr = np.linspace(-5,5,10)
del arr[0]
The code above throws an error saying cannot delete array elements.
Using pop doesn't work either. What should I do?
You should use np.delete for it.
arr = np.linspace(-5,5,10)
arr = np.delete(arr, 0)

Create named list from matrix using rpy2

I have a 2D numpy array which I converted to R matrix and now I need to convert it further to named list:
rpy2.robjects.numpy2ri.activate()
nr,nc = counts.shape
r_mtx = robjects.r.matrix(counts, nrow=nr, ncol=nc)
So, I got the matrix r_mtx, but I am not sure how to make a named list out of it similar to how we do it in R:
named_list <- list(counts=mtx)
I need it to feed into SingleCellExperiment object to do dataset normalization:
https://bioconductor.org/packages/devel/bioc/vignettes/scran/inst/doc/scran.html
I tried using rpy2.rlike.container both TaggedList and OrdDict but can't figure out how to apply them to my case.
Ultimately I solved it (avoiding convertion of numpy array to r matrix), straight making the named list from the numpy array:
named_list = robjects.r.list(counts=counts)
Where counts is a 2D numpy array

Resources