Delete items in subarrays of a master array - python

let's say I have the following 3x4 array
master_array = [[1, 3, 4, 5],
[6, 5, 4, 1],
[7, 8, 4, 1]]
Then, I want to delete number 4 from each of the 3 1x4 subarrays. Would I use the following?
for i in range(master_array.shape[0]):
np.delete(master_array[i], 3)
Then, when I print the master_array, would I get?
[[1, 3, 5],
[6, 5, 1],
[7, 8, 1]]

In case master_array is a list of lists, like in your example, you could do
master_array = [[1, 3, 4, 5],
[6, 5, 4, 1],
[7, 8, 4, 1]]
for row in master_array:
del row[2]
In case master_array is indeed a numpy array, you would simply do
master_array = np.array([[1, 3, 4, 5],
[6, 5, 4, 1],
[7, 8, 4, 1]])
np.delete(master_array, 2, axis=1)

Related

Count set of elements in numpy array

i have numpy array
array([[1, 2, 3],
[1, 2, 5],
[3, 4, 6],
[2, 5, 4],
[5, 4, 3],
[3, 5, 1],
[2, 5, 1]])
i want function to count how many times set of values appears in array. For example
count_set([1,2])
#output
3
# because set[1,2] appears in elements 0,1,6
I have tried some np.notezero , but it doesnt workout
Use broadcasted comparison with all/any:
a = np.array([[1, 2, 3],
[1, 2, 5],
[3, 4, 6],
[2, 5, 4],
[5, 4, 3],
[3, 5, 1],
[2, 5, 1]])
def count_set(a, elems):
return (a[..., None]==elems).any(-2).all(-1).sum()
count_set(a, [1, 2])
# 3

Numpy 2D to 3D array based on data in a column

Let's say I have data structured in a 2D array like this:
[[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
The first column denotes a third dimension, so I want to convert this to the following 3D array:
[[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]]
Is there a built-in numpy function to do this?
You can try code below:
import numpy as np
array = np.array([[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]])
array = np.delete(array, 0, 1)
array.reshape(2,3,-1)
Output
array([[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]])
However, this code can be used when you are aware of the array's shape. But if you are sure that the number of columns in the array is a multiple of 3, you can simply use code below to show the array in the desired format.
array.reshape(array.shape[0]//3,3,-3)
Use numpy array slicing with reshape function.
import numpy as np
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# convert the list to numpy array
arr = np.array(arr)
# remove first column from numpy array
arr = arr[:,1:]
# reshape the remaining array to desired shape
arr = arr.reshape(len(arr)//3,3,-1)
print(arr)
Output:
[[[3 4 6]
[4 8 2]
[3 2 9]]
[[2 4 8]
[4 9 1]
[2 9 3]]]
You list a non numpy array. I am unsure if you are just suggesting numpy as a means to get a non numpy result, or you are actually looking for a numpy array as result. If you don't actually need numpy, you could do something like this:
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# Length of the 3rd and 2nd dimension.
nz = arr[-1][0] + (arr[0][0]==0)
ny = int(len(arr)/nz)
res = [[arr[ny*z_idx+y_idx][1:] for y_idx in range(ny)] for z_idx in range(nz)]
OUTPUT:
[[[3, 4, 6], [4, 8, 2], [3, 2, 9]], [[2, 4, 8], [4, 9, 1], [2, 9, 3]]]
Note that the calculation of nz takes into account that the 3rd dimension index in your array is either 0-based (as python is per default) or 1-based (as you show in your example).

I want to convert a 2D numpy array to a 3D array, but there's a catch [duplicate]

This question already has answers here:
How to copy a 2D array into a 3rd dimension, N times?
(7 answers)
Closed 1 year ago.
I'll use a simple 2D array with shape (4,4) as an example:
array([[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]])
And to visualize it:
I want to convert this to a 3D array, so that the values a duplicated along the z-axis, as such:
So that the resulting array has a shape (4,4,3)
It seems really simple, but I can't seem to think of any way to do this.
Edit: I tried np.tile from the answers below, however I would like the output to be this:
array([[[0, 0, 0],
[2, 2, 2],
[6, 6, 6],
[3, 3, 3]],
[[3, 3, 3],
[7, 7, 7],
[3, 3, 3],
[9, 9, 9]],
[[0, 0, 0],
[8, 8, 8],
[3, 3, 3],
[4, 4, 4]],
[[4, 4, 4],
[6, 6, 6],
[2, 2, 2],
[1, 1, 1]]])
I tried changing which axis is duplicated and reshaping, although it doesn't work.
You can use numpy.tile for this
>>> import numpy as np
>>> data = np.array([[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]])
>>> np.tile(data, (3,1,1))
array([[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]],
[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]],
[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]]])

Given indexes, get values from numpy matrix

Let's say I have this numpy matrix:
>>> mat = np.matrix([[3,4,5,2,1], [1,2,7,6,5], [8,9,4,5,2]])
>>> mat
matrix([[3, 4, 5, 2, 1],
[1, 2, 7, 6, 5],
[8, 9, 4, 5, 2]])
Now let's say I have some indexes in this form:
>>> ind = np.matrix([[0,2,3], [0,4,2], [3,1,2]])
>>> ind
matrix([[0, 2, 3],
[0, 4, 2],
[3, 1, 2]])
What I would like to do is to get three values from each row of the matrix, specifically values at columns 0, 2, and 3 for the first row, values at columns 0, 4 and 2 for the second row, etc. This is the expected output:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
I've tried using np.take but it doesn't seem to work. Any suggestion?
This is take_along_axis.
>>> np.take_along_axis(mat, ind, axis=1)
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
This will do it: mat[np.arange(3).reshape(-1, 1), ind]
In [245]: mat[np.arange(3).reshape(-1, 1), ind]
Out[245]:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
(but take_along_axis in #user3483203's answer is simpler).

Rotating a list of lists

m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
rotated_map = []
for i in range(len(m[0])):
rotated_map.append([x[i] for x in m])
print(rotated_map)
"""
my result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
desired result = [[8,7,2,3],
[1,5,3,6],
[9,4,3,7],
[5,2,6,1]]
"""
I am trying to rotate the list by putting all the last elements first from the lists into one list then the second to last element into another and so on until i get to the first element.
Transpose the list with zip, then reverse it with the [::-1] syntax.
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> list(map(list, zip(*m)))[::-1]
>>> [[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
edit:
If you want pretty printing, it's probably easiest to use numpy arrays all the way.
>>> import numpy as np
>>>
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> m = np.array(m)
>>> m
>>>
array([[5, 9, 1, 8],
[2, 4, 5, 7],
[6, 3, 3, 2],
[1, 7, 6, 3]])
>>>
>>> m.T[::-1]
>>>
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
Note that m and m.T[::-1] share the same data, because m.T[::-1] is just another view of m. If you need to duplicate the data, use
result = m.T[::-1].copy()
You could use zip, unpacking your list of lists with the *, and inversing the result with [::-1]:
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
res = [list(i) for i in zip(*m)][::-1]
>>> res
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If numpy is an option, transposing is easier:
import numpy as np
>>> np.transpose(m)[::-1]
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
# or:
>>> np.flip(np.transpose(m),0)
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
You can use numpy module to do it. It has the property to transpose the array. Check the below code:
import numpy as np
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
arr = np.array(m).transpose()
new_list = []
for i in range(arr.shape[0]-1,-1,-1):
new_list.append(list(arr[i]))
print(new_list)
Output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If you want to rotate the list clockwise:
list(map(list, zip(*m[::-1])))
Else, for anti-clockwise:
list(map(list, zip(*m)))[::-1]
use the reverse keyword
result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
result.reverse()
print(result)
output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]

Categories