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

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).

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

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]]])

Cartesian product of 2d and 1d numpy

I have 2d and 1d numpy and I want to join them with Cartesian product.
For example the numpy's are:
td = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
od = np.array([1,2,3])
The expected result should be:
[[1,2,3,1],
[1,2,3,2],
[1,2,3,3],
[4,5,6,1],
[4,5,6,2],
[4,5,6,3],
[7,8,9,1],
[7,8,9,2],
[7,8,9,3]]
The following code does not achieve the desired result:
import numpy as np
rslt = np.transpose([np.tile(td, len(od)), np.repeat(od , len(td))])
What needs to change?
Give this a try:
np.c_[np.repeat(td,3,axis=0),np.tile(od,3).reshape((-1,1))]
output:
array([[1, 2, 3, 1],
[1, 2, 3, 2],
[1, 2, 3, 3],
[4, 5, 6, 1],
[4, 5, 6, 2],
[4, 5, 6, 3],
[7, 8, 9, 1],
[7, 8, 9, 2],
[7, 8, 9, 3]])

Repeat a unidimensional array over columns

Quite straightforward question, I have the following array:
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
I want to repeat this array over columns, having something like this:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8]])
So, in order to do so I have been trying:
repeat_x = np.repeat(x, 3, axis = 1)
However, I get the following error:
AxisError: axis 1 is out of bounds for array of dimension 1
So, is there a way/trick to achieve my goal without having to use any sort of reshape?
Try this code:
np.array([x] * 3).T
Here 3 is the number of times you want to repeat those values
To do it purely in numpy without resorting back to python lists you need to use expand_dims followed by a transpose or use reshape to convert the vector into a matrix before using repeat.
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# array([1, 2, 3, 4, 5, 6, 7, 8])
x = x.reshape(-1, 1)
# array([[1],
# [2],
# [3],
# [4],
# [5],
# [6],
# [7],
# [8]])
np.repeat(x.reshape(-1, 1), 3, 1)
# array([[1, 1, 1],
# [2, 2, 2],
# [3, 3, 3],
# [4, 4, 4],
# [5, 5, 5],
# [6, 6, 6],
# [7, 7, 7],
# [8, 8, 8]])
Using expand dims and a transpose will be like
np.repeat(np.expand_dims(x, 0).T, 3, 1)
Same result.

Delete items in subarrays of a master array

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)

Categories