Python Summing A 2D Array In Steps Defined With Element Number Range - python

import Numpy as np
a = np.array([[5, 1, 8, 1, 6, 1, 3, 2],[2, 3, 4, 1, 6, 1, 4, 2]])
n = 2
[(a[0:2, i:i+n]).sum(axis=1) for i in range(0,a.shape[1],n)]
The output is:
[array([6, 5]), array([9, 5]), array([7, 7]), array([5, 6])]
How can I get a 2D array instead of 4 arrays I got in above output...Is there a better more elegant way doing this using reshape?

You can use sliding_window_view:
import numpy as np
from numpy.lib.stride_tricks import sliding_window_view
a = np.array([[5, 1, 8, 1, 6, 1, 3, 2], [2, 3, 4, 1, 6, 1, 4, 2]])
n = 2
out = sliding_window_view(a, (n, n)).sum(axis=n+1)[:, ::n].squeeze()
Output:
array([[6, 5],
[9, 5],
[7, 7],
[5, 6]])

You can always stack the results to get a single array:
import numpy as np
a = np.array([[5, 1, 8, 1, 6, 1, 3, 2],[2, 3, 4, 1, 6, 1, 4, 2]])
n = 2
np.stack([(a[0:2, i:i+n]).sum(axis=1) for i in range(0,a.shape[1],n)])
Giving you:
array([[6, 5],
[9, 5],
[7, 7],
[5, 6]])
Alternatively, you can reshape the array and sum:
a.T.reshape(-1, n, 2).sum(axis=1)
Giving the same result. But note, the array will need to be reshape-able to the given n, so n=4 is fine n=3 is an error.

Another solution:
from numpy.lib.stride_tricks import sliding_window_view
a = np.array([[5, 1, 8, 1, 6, 1, 3, 2],[2, 3, 4, 1, 6, 1, 4, 2]])
n = 2
print(sliding_window_view(a, (n,n))[:,::n].sum(axis=n+1))
Prints:
[[[6 5]
[9 5]
[7 7]
[5 6]]]

Related

How can I print out the max nos in each column of a numpy array in an object using python?

I have the below numpy array
[[7, 0, 0, 6],
[5, 6, 6, 1],
[4, 1, 6, 7],
[5, 3, 4, 7]]
I want to find the max no in each column using np.max and then print out the result in an object such that output will be as shown below
[7, 6, 6, 7]
If arr is your array, then you just need to use the max function, indicating the chosen axis:
arr.max(axis=0)
Output:
array([7, 6, 6, 7])
If you want a list instead of a numpy array:
arr.max(axis=0).tolist()
Output:
[7, 6, 6, 7]
You can traverse the transposed array and look for the max value with np.max():
import numpy as np
m =np.array([[7, 0, 0, 6],
[5, 6, 6, 1],
[4, 1, 6, 7],
[5, 3, 4, 7]])
out = [np.max(i) for i in m.transpose()]
print(out)
Output:
[7, 6, 6, 7]
import numpy as np
m =np.array([[7, 0, 0, 6],
[5, 6, 6, 1],
[4, 1, 6, 7],
[5, 3, 4, 7]])
#list
max_numbers = [max(x) for x in m]
#array
max_num_array = np.array(max_numbers)

Split nested numpy array

I have a numpy array of shape 28 x 1875. Each element is a 3-element list (only floats). I need to split each of these elements to individual ones, to obtain an array of shape 28x5625(1875*3). I've tried np.split, however it only separates each element, but no each sub-element. Is there a fast way to do this?
Making a 2d array of lists:
In [522]: arr = np.empty(6,object)
In [523]: arr[:] = [list(range(i,i+3)) for i in range(6)]
In [524]: arr = arr.reshape(2,3)
In [525]: arr
Out[525]:
array([[list([0, 1, 2]), list([1, 2, 3]), list([2, 3, 4])],
[list([3, 4, 5]), list([4, 5, 6]), list([5, 6, 7])]], dtype=object)
It's easier to fill such an array if it is 1d, which is why I start with (6,) and reshape after.
Paul Panzer's suggestion:
In [526]: np.array(arr.tolist())
Out[526]:
array([[[0, 1, 2],
[1, 2, 3],
[2, 3, 4]],
[[3, 4, 5],
[4, 5, 6],
[5, 6, 7]]])
In [527]: _.reshape(2,-1)
Out[527]:
array([[0, 1, 2, 1, 2, 3, 2, 3, 4],
[3, 4, 5, 4, 5, 6, 5, 6, 7]])
You can also use np.stack (a version of np.concatenate) to create a nd array. It does though, require a 1d object array - hence the ravel:
In [536]: np.stack(arr.ravel())
Out[536]:
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7]])
That can be reshaped as needed:
In [537]: np.stack(arr.ravel()).reshape(2,-1)
Out[537]:
array([[0, 1, 2, 1, 2, 3, 2, 3, 4],
[3, 4, 5, 4, 5, 6, 5, 6, 7]])
In some cases we need to transpose axes to get the desired order.

How to omit all the last elements of a numpy ndarray in python?

So I can slice a numpy array quite simply as:
a = np.arange(10)
a[:-3]
array([0, 1, 2, 3, 4, 5, 6])
but now say I do:
a = np.vstack((a, a))
a
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
Is there a nice pythonic way (without looping) to get:
array([[0, 1, 2, 3, 4, 5, 6],
[0, 1, 2, 3, 4, 5, 6]])
Thanks.
Thanks to Divakar in comments.
a[:,:-3]

Combining multiple 1D arrays returned from a function into a 2D array python

I m a little new to python. I have a function named featureExtraction which returns a 1-D array for an image. I need to stack all such 1-d arrays row wise to form a 2-d array. I have the following equivalent code in MATLAB.
I1=imresize(I,[256 256]);
Features(k,:) = featureextraction(I1);
featureextraction returns a 1-d row vector which is stacked row-wise to form a 2-d array. What is the equivalent code snippet in python?
Thank You in advance.
Not sure what you're looking for, but maybe vstack or column_stack?
>>> np.vstack((a,a,a))
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> np.column_stack((a,a,a))
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8],
[9, 9, 9]])
Or even just np.array:
>>> np.array([a,a,a])
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
You can use numpy.vstack():
a = np.array([1,2,3])
np.vstack((a,a,a))
#array([[1, 2, 3],
# [1, 2, 3],
# [1, 2, 3]])

combining two arrays in numpy,

I use genfromtxt to read in an array from a text file and i need to split this array in half do a calculation on them and recombine them. However i am struggling with recombining the two arrays. here is my code:
X2WIN_IMAGE = np.genfromtxt('means.txt').T[1]
X2WINa = X2WIN_IMAGE[0:31]
z = np.mean(X2WINa)
X2WINa = X2WINa-z
X2WINb = X2WIN_IMAGE[31:63]
ww = np.mean(X2WINb)
X2WINb = X2WINb-ww
X2WIN = str(X2WINa)+str(X2WINb)
print X2WIN
How do i go about recombining X2WINa and X2WINb in one array? I just want one array with 62 components
X2WINc = np.append(X2WINa, X2WINb)
if you want to combine row-wise use np.vstack(), and if column-wise use np.hstack(). Example:
np.hstack( (np.arange(10), np.arange(10)) )
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.vstack( (np.arange(10), np.arange(10)) )
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
combined_array = np.concatenate((X2WINa, X2Winb))
And another one using numpy.r_:
X2WINc = np.r_[X2WINa,X2WINb]
e.g.:
>>> import numpy as np
>>> np.r_[np.arange(10),np.arange(10)]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
There's also np.c_ to column stack:
>>> np.c_[np.arange(10),np.arange(10)]
array([[0, 0],
[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5],
[6, 6],
[7, 7],
[8, 8],
[9, 9]])

Categories