Reshaping a nested Numpy Arrays using reshape [duplicate] - python-3.x

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

Related

How can I convert my code into a single line? [duplicate]

This question already has answers here:
How can I use a conditional expression (expression with if and else) in a list comprehension? [duplicate]
(6 answers)
Closed 1 year ago.
I have the following code which I would like to convert into a single line I guess using list comprehension? But I have been unsuccessful in converting it.
exp_days = ["16/04/2021","23/04/2021","27/04/2021"]
for i in range(len(df)):
if df["Date"][i] in exp_days:
list_of_days.append(1)
else:
list_of_days.append(0)
My dataframe:
Date
16/04/2021
19/04/2021
20/04/2021
21/04/2021
22/04/2021
23/04/2021
26/04/2021
27/04/2021
Expected output:
list_of_days = [1,0,0,0,0,1,0,1]
list_of_days = [ 1 if df["Date"][i] in exp_days else 0 for i in range(len(df)) ]
Alternative via numpy -
exp_days = ["16/04/2021","23/04/2021","27/04/2021"]
import numpy as np
result = np.where(df['Date'].isin(exp_days),1,0)

How to split python3 List? [duplicate]

This question already has answers here:
How to extract the n-th elements from a list of tuples
(8 answers)
Closed 3 years ago.
I have this list:
[('5.333333333333333', 'n04'), ('5.0', 'n01'), ('3.9936507936507932', 'n03'), ('2.4206349206349205', 'n05'), ('1.9629629629629628', 'n02')]
and I like to have the list like this:
[n04, n01, n03, n02, n04]
how to do it? I have spend too many houres on this problem.
Help please!
You can use a list comprension to iterate over the list and pick out the values you are interested in and put them in a new list
my_list = [('5.333333333333333', 'n04'), ('5.0', 'n01'), ('3.9936507936507932', 'n03'), ('2.4206349206349205', 'n05'), ('1.9629629629629628', 'n02')]
my_new = [item[1] for item in my_list]
print(my_new)
OUTPUT
['n04', 'n01', 'n03', 'n05', 'n02']
Try:
x,y=zip(*[('5.333333333333333', 'n04'), ('5.0', 'n01'), ('3.9936507936507932', 'n03'), ('2.4206349206349205', 'n05'), ('1.9629629629629628', 'n02')])
y=list(y)
print(y)
Outputs:
['n04', 'n01', 'n03', 'n05', 'n02']

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

Sliding window on a 2D numpy array [duplicate]

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)

Resources