Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ndim array
#1
Hi all
thrD = np.array([[1,2,3],[3,4,5],[1,-2,8]])

print(thrD.ndim)
Output

Output:
2
My doubts: What are dimensions in thrD
Why I am getting 2 as Output

Please anyone can help me
Reply
#2
Dim=2 because you're using a 2D array; in the following examples, 3 type of arrays have been defined:
  1. a vector = 1D array (dim=1)
  2. a 3x3 matrix = 2D array (dim=2)
  3. a 3D matrix (equivalent to n "layers" of 2D matrixes) - dim=3

import numpy as np

thrD = np.array([[1,2,3],
                 [3,4,5],
                 [1,-2,8]])

print(f"thrD = \n{thrD}")
print(f"thrd dim = {thrD.ndim}\n")

Vector1 = np.array([1,2,3]) 
Vector2 = np.arange(3)
print(f"Vector1 = {Vector1}")
print(f"Vector2 = {Vector2}")
print(f"vector1 dim = {Vector1.ndim} and vector2 dim = {Vector2.ndim}")

Matrix3D = np.arange(27).reshape(3,3,3)
print(f"Matrix3D = \n{Matrix3D}")
print(f"Matrix3D dim = {Matrix3D.ndim}\n")
giving
Output:
thrD = [[ 1 2 3] [ 3 4 5] [ 1 -2 8]] thrd dim = 2 Vector1 = [1 2 3] Vector2 = [0 1 2] vector1 dim = 1 and vector2 dim = 1 Matrix3D = [[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]] Matrix3D dim = 3
Reply
#3
Thank you!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020