Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

Assist me in creating a 4d array. Below is my code for 3 dimension array. How do I get output in the 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]]])

1 Answer

0 votes
by (36.8k points)

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)

If you are a beginner and want to know more about Data Science the do check out the Data Science course

For more information, kindly refer to our Python Certification course.

Related questions

Browse Categories

...