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

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

## Convert 2D array to 3D numpy array

You need to use

` data.reshape((data.shape[0], data.shape[1], 1))`

Example

`from numpy import array`

data = [[11, 22],

[33, 44],

[55, 66]]

data = array(data)

print(data.shape)

data = data.reshape((data.shape[0], data.shape[1], 1))

print(data.shape)

Running the example first prints the size of each dimension in the 2D array, reshapes the array, then summarizes the shape of the new 3D array.

Result

`(3,2)`

(3,2,1)

Source :https://machinelearningmastery.com/index-slice-reshape-numpy-arrays-machine-learning-python/

## Convert a 2D array into 3D array repeating existing values

You could use numpy.repeat function

https://numpy.org/doc/stable/reference/generated/numpy.repeat.html

`array3d = np.repeat(array2d[:, :, None], repeats=3, axis=2)`

## How to Reshape 2D to 3D Array in Python?

It is easy to do with `numpy`

using `.reshape`

method:

`A = np.array([[0,0,0,0,1], [1,0,0,0,0], [0,0,0,1,0], [0,1,0,0,0]])`

A = A.reshape(2, 2, 5)

print(A.shape)

So new shape is `(2, 2, 5)`

. For your data you can just add a dummy dimension for a time step:

`A = np.expan_dims(A, 1)`

## reshape 2D array into 3D array C

Your way of assigning the numbers to the newly allocated memory is wrong.

`static int ***alloc_3d(int ar[][12],int rows, int cols,int levels,int colsize)`

{

int count = 0;

int ***array_3d;

ZALLOC(array_3d, levels, int **);

int i1=0,j1=0;

for (i = 0; i < levels; i++)

{ ...

...

for (k = 0; k < cols; k++)

{

array_3d[i][j][k] = ar[i1][j1++];

if( j1 == colsize) i1++,j1=0;

}

}

}

return array_3d;

}

Call like this

`int colsize = 12;`

int ***a3d = alloc_3d(ar,d1, d2, d3,colsize);

This prints:

`0:`

0: 1 2 3

1: 4 5 6

1:

0: 7 8 9

1: 10 11 12

2:

0: 13 14 15

1: 16 17 18

3:

0: 19 20 21

1: 22 23 24

A small note - earlier your code had undefined behavior accessing array index out of the bound.

## How do I reshape a 2d array into a 3d array for a neural network

The only way to make this run is without a CNN. Then, you just don't have to reshape. Speculative reshaping would just yield random pixels.

`import numpy as np`

import tensorflow as tf

from tensorflow.keras.layers import Dense

from tensorflow.keras.models import Sequential

X = np.random.randint(0, 256, (1945, 1800)) # fake data

y = np.random.randint(0, 38, 1945)

model = Sequential([

Dense(128, activation='relu', input_shape=(1800,)),

Dense(39)])

model.compile(optimizer='adam',

loss=tf.keras.losses.SparseCategoricalCrossentropy(

from_logits=True), metrics=['accuracy'])

hist = model.fit(X, y, epochs=10)

Ideally, you would fix your data and then you could run a CNN, which is the best model for this. A CNN maintains 2D relationships between pixels so it would be fantastic, but there is no relationship between your pixels if you don't know the correct shape.

## How to make a 2d numpy array a 3d array?

In addition to the other answers, you can also use slicing with `numpy.newaxis`

:

`>>> from numpy import zeros, newaxis`

>>> a = zeros((6, 8))

>>> a.shape

(6, 8)

>>> b = a[:, :, newaxis]

>>> b.shape

(6, 8, 1)

Or even this (which will work with an arbitrary number of dimensions):

`>>> b = a[..., newaxis]`

>>> b.shape

(6, 8, 1)

### Related Topics

Counting Non Zero Values in Each Column of a Dataframe in Python

How to Plot Pandas Dataframe With Date (Year/Month)

Reduce Multi-Index/Multi-Level Dataframe to Single Index, Single Level

Python: How to Turn CSV Data in to Array

Rotate Tick Labels for Seaborn Barplot

Check If a Key Exists in a Bucket in S3 Using Boto3

How to Extract X,Y Coordinates from Opencv "Cv2.Keypoint" Object

How to Compute the Gradients of Image Using Python

Python Convert Comma Separated List to Pandas Dataframe

Combine Date and Time Columns Using Python Pandas

How to Kill a While Loop With a Keystroke

Pandas: Subtracting Two Date Columns and the Result Being an Integer

How to Repeatedly Execute a Function Every X Seconds

How to Convert Number 1 to a Boolean in Python

Importing Local Module (Python Script) in Airflow Dag