Populating array elements with lists - python

I have following array:
b=np.zeros((5,5)).astype('int32')
I wish to populate each element of above array with a list using below two arrays:
x=np.linspace(11, 15, 5)
`y=np.linspace(6, 10, 5)`
The output i am looking at:
`array([[11,6], [11,7], [11,8], [11,9], [11,10]],
[[12,6], [12,7], [12,8], [12,9], [12,10]],
[[13,6], [13,7], [13,8], [13,9], [13,10]],
[[14,6], [14,7], [14,8], [14,9], [14,10]],
[[15,6], [15,7], [15,8], [15,9], [15,10]])`

Like #DocDriven said, you'll have to adjust the shape of the b array first, to (5, 5, 2).
After that, note that you can set a whole row of y values by doing b[row,:,1] = y and a whole column of x values by doing b[:,col,0] = x.
Numpy also broadcasts shapes, which means you can use a 1d array to fill a 2d array; b[:,:,1] = y will fill all the y values in a single operation, while b[:,:,0] = x.reshape((5, 1)) will do the same for the x values.
In short, you can get what you want by doing just:
b = np.zeros((5, 5, 2)).astype('int32')
b[:,:,1] = y
b[:,:,0] = x.reshape((5, 1))
Another way is to use np.meshgrid():
b = np.array(np.meshgrid(x, y)).T.astype('int32')

I slightly adjusted your original numpy array because you cannot replace a single integer with a sequence.
import numpy as np
b = np.zeros((5,5,2)).astype('int32')
x = np.linspace(11, 15, 5).astype('int32')
y = np.linspace(6, 10, 5).astype('int32')
idx_x = 0
idx_y = 0
for row in b:
for _ in row:
b[idx_x, idx_y] = [x[idx_x], y[idx_y]]
idx_y += 1
idx_y = 0
idx_x += 1
print(b.tolist())
Output:
[[[11, 6], [11, 7], [11, 8], [11, 9], [11, 10]],
[[12, 6], [12, 7], [12, 8], [12, 9], [12, 10]],
[[13, 6], [13, 7], [13, 8], [13, 9], [13, 10]],
[[14, 6], [14, 7], [14, 8], [14, 9], [14, 10]],
[[15, 6], [15, 7], [15, 8], [15, 9], [15, 10]]]
If you want to keep it as a numpy array, do not cast it via tolist().

Related

transpose function of numpy

I am new to numpy and python and I am trying to understand the usage of transpose function of numpy. The code below works fine but I am still not be able to understand the effect of transpose function and also the use of the arguments inside it. It would be great help if someone can explain the usage and effect of transpose function in below code.
import numpy as np
my_list = [[[[[[1,2],[3,4]],[[1,2],[3,4]]], [[[1,2],[3,4]],[[1,2],[3,4]]]],[[[[1,2],[3,4]],[[1,2],[3,4]]], [[[1,2],[3,4]],[[1,2],[3,4]]]]], [[[[[1,2],[3,4]],[[1,2],[3,4]]], [[[1,0],[1,1]],[[1,0],[1,1]]]],[[[[1,0],[1,1]],[[1,0],[1,1]]], [[[1,0],[1,1]],[[1,0],[1,1]]]]]]
arr = np.array(my_list)
perm_testing = [0,1,2,3,4,5]
testing = arr.transpose(perm_testing)
print(testing)
Edit
import numpy as np
my_list = [[1,2],[3,4]]
arr = np.array(my_list)
perm_testing = [1,0]
testing = arr.transpose(perm_testing)
print(testing)
[[1 3]
[2 4]]
Here's an attempt to visually explain for a 3d-array. I hope it'll help you better understand what's happening:
a=np.arange(24).reshape(2,4,3)
# array([[[ 0, 1, 2],
# [ 3, 4, 5],
# [ 6, 7, 8],
# [ 9, 10, 11]],
#
# [[12, 13, 14],
# [15, 16, 17],
# [18, 19, 20],
# [21, 22, 23]]])
And a visual 3d representation of a (axis 0 corresponds to the first bracket level and to the first size in the shape, and so on for axis 1 and 2):
a.transpose(1,0,2) # swapping axis 0 and 1
# array([[[ 0, 1, 2],
# [12, 13, 14]],
#
# [[ 3, 4, 5],
# [15, 16, 17]],
#
# [[ 6, 7, 8],
# [18, 19, 20]],
#
# [[ 9, 10, 11],
# [21, 22, 23]]])
Visual 3d representation of the new array (sorry, my drawing skills are quite limited):

Delete rows and column from a numpy array, given information to delete is in another vector array

I have the following code:
deleterowandcolumns = np.array([0,3])
original = np.array ([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
])
The rows and columns to delete is contained in the deleterowandcolumns array. Manually, I deleted the specified rows and columns like:
deleted = np.delete(original,(0,3), axis=0)
deleted = np.delete(deleted,(0,3), axis=1)
The following required output is obtained:
[[ 6 7]
[10 11]]
However this method is very manual and does not use the deleterowandcolumns array at all. Is there an efficient way of doing this?

Get indices of element of one array using indices in another array

Suppose I have an array a of shape (2, 2, 2):
a = np.array([[[7, 9],
[19, 18]],
[[24, 5],
[18, 11]]])
and an array b that is the max of a: b=a.max(-1) (row-wise):
b = np.array([[9, 19],
[24, 18]])
I'd like to obtain the index of elements in b using index in flattened a, i.e. a.reshape(-1):
array([ 7, 9, 19, 18, 24, 5, 18, 11])
The result should be an array that is the same shape with b with indices of b in flattened a:
array([[1, 2],
[4, 6]])
Basically this is the result of maxpool2d when return_indices= True in pytorch, but I'm looking for an implementation in numpy. I've used where but it seems doesn't work, also is it possible to combine finding max and indices in one go, to be more efficient? Thanks for any help!
I have a solution similar to that of Andras based on np.argmax and np.arange. Instead of "indexing the index" I propose to add a piecewise offset to the result of np.argmax:
import numpy as np
a = np.array([[[7, 9],
[19, 18]],
[[24, 5],
[18, 11]]])
off = np.arange(0, a.size, a.shape[2]).reshape(a.shape[0], a.shape[1])
>>> off
array([[0, 2],
[4, 6]])
This results in:
>>> a.argmax(-1) + off
array([[1, 2],
[4, 6]])
Or as a one-liner:
>>> a.argmax(-1) + np.arange(0, a.size, a.shape[2]).reshape(a.shape[0], a.shape[1])
array([[1, 2],
[4, 6]])
The only solution I could think of right now is generating a 2d (or 3d, see below) range that indexes your flat array, and indexing into that with the maximum indices that define b (i.e. a.argmax(-1)):
import numpy as np
a = np.array([[[ 7, 9],
[19, 18]],
[[24, 5],
[18, 11]]])
multi_inds = a.argmax(-1)
b_shape = a.shape[:-1]
b_size = np.prod(b_shape)
flat_inds = np.arange(a.size).reshape(b_size, -1)
flat_max_inds = flat_inds[range(b_size), multi_inds.ravel()]
max_inds = flat_max_inds.reshape(b_shape)
I separated the steps with some meaningful variable names, which should hopefully explain what's going on.
multi_inds tells you which "column" to choose in each "row" in a to get the maximum:
>>> multi_inds
array([[1, 0],
[0, 0]])
flat_inds is a list of indices, from which one value is to be chosen in each row:
>>> flat_inds
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
This is indexed into exactly according to the maximum indices in each row. flat_max_inds are the values you're looking for, but in a flat array:
>>> flat_max_inds
array([1, 2, 4, 6])
So we need to reshape that back to match b.shape:
>>> max_inds
array([[1, 2],
[4, 6]])
A slightly more obscure but also more elegant solution is to use a 3d index array and use broadcasted indexing into it:
import numpy as np
a = np.array([[[ 7, 9],
[19, 18]],
[[24, 5],
[18, 11]]])
multi_inds = a.argmax(-1)
i, j = np.indices(a.shape[:-1])
max_inds = np.arange(a.size).reshape(a.shape)[i, j, multi_inds]
This does the same thing without an intermediate flattening into 2d.
The last part is also how you can get b from multi_inds, i.e. without having to call a *max function a second time:
b = a[i, j, multi_inds]
This is a long one-liner
new = np.array([np.where(a.reshape(-1)==x)[0][0] for x in a.max(-1).reshape(-1)]).reshape(2,2)
print(new)
array([[1, 2],
[4, 3]])
However number = 18 is repeated twice; So which index is the target.

Creating multiple arrays from one array, based on values in another array - python

The situation is the following:
Lets say I have two arrays, x and y:
Input:
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
y = [2,6,9,13]
Expected output:
arr1 = [2,3,4,5]
arr2 = [6,7,8]
arr3 = [9,10,11,12]
I would like to create a python script that can let me split up array x into multiple arrays based on the values of array y as the end points.
So x will split up between 2 and 6, then between 6 and 9, then between 9 and 13 in this example.
I am not sure how to get started on this, I am a beginner. I would appreciate the help and I would love to know how you broke down the problem to solve it? Thank you!
Find the index of x based on value in y and then use indexing
arr = []
for i in range(len(y)-1):
arr.append(x[x.index(y[i]):x.index(y[i+1])])
arr
[[2, 3, 4, 5], [6, 7, 8], [9, 10, 11, 12]]
This works if there are duplicates in the array,
x = [2,3,4,5,6,7,7,8,9,9,10,11,11,12,13,14,15]
y = [2,6,9,13]
arr = []
for i in range(len(y)-1):
arr.append(x[x.index(y[i]):x.index(y[i+1])])
[[2, 3, 4, 5], [6, 7, 7, 8], [9, 9, 10, 11, 11, 12]]
For x sorted, we can use np.searchsorted with np.split to split x using the indices where y is contained in x:
import numpy as np
i = np.searchsorted(x, y)
np.split(x,i+1)[1:-1]
# [array([3, 4, 5, 6]), array([7, 8, 9]), array([10, 11, 12, 13])]
You are somewhat inconsistent in your expected output, or I don`t get the pattern behind it, but this should get you close to what you want
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
y = [2,6,9,13]
m = []
for i in range(len(y) - 1):
m += [x[y[i] - 1:y[i+1] - 1]]
print(m)
Output
[[2, 3, 4, 5], [6, 7, 8], [9, 10, 11, 12]]

replace min value to another in numpy array

Lets say we have this array and I want to replace the minimum value with number 50
import numpy as np
numbers = np.arange(20)
numbers[numbers.min()] = 50
So the output is [50,1,2,3,....20]
But now I have problems with this:
numbers = np.arange(20).reshape(5,4)
numbers[numbers.min(axis=1)]=50
to get [[50,1,2,3],[50,5,6,7],....]
However I get this error:
IndexError: index 8 is out of bounds for axis 0 with size 5 ....
Any ideas for help?
You need to use numpy.argmin instead of numpy.min:
In [89]: numbers = np.arange(20).reshape(5,4)
In [90]: numbers[np.arange(len(numbers)), numbers.argmin(axis=1)] = 50
In [91]: numbers
Out[91]:
array([[50, 1, 2, 3],
[50, 5, 6, 7],
[50, 9, 10, 11],
[50, 13, 14, 15],
[50, 17, 18, 19]])
In [92]: numbers = np.arange(20).reshape(5,4)
In [93]: numbers[1,3] = -5 # Let's make sure that mins are not on same column
In [94]: numbers[np.arange(len(numbers)), numbers.argmin(axis=1)] = 50
In [95]: numbers
Out[95]:
array([[50, 1, 2, 3],
[ 4, 5, 6, 50],
[50, 9, 10, 11],
[50, 13, 14, 15],
[50, 17, 18, 19]])
(I believe my original answer was incorrect, I confused rows and columns, and this is right)

Categories

Resources