Chandrakanth
Chandrakanth

Reputation: 25

How to create 4d Array in Python

Assist me in creating 4d array. Below is my code for 3 dimension array. How do i get output in shape of (4,3,2,3)

Three_d_array - numpy.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])

The above code provides output in shape of (3,2,3) but how to i get the 4d in this shape

Upvotes: 1

Views: 8806

Answers (1)

Sơn Ninh
Sơn Ninh

Reputation: 331

import numpy as np    
a = np.arange(72)
a = np.reshape(a, (4,3,2,3))

or:

a = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]])

or:

a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])
a = np.expand_dims(a, axis=0)
a = np.repeat(a, 4, axis=0)

Upvotes: 3

Related Questions