Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

How the measurement/dimension is 1. I have given a condition of 3 factors/variables it implies it is a 3 dimension however it is showing the measurement/dimension as 1. Would anyone be able to reveal to me the rationale of ndim?

import numpy as np

>>> a=np.array([1,2,3,4])

>>> a

array([1, 2, 3, 4])

>>> a.ndim

1

1 Answer

0 votes
by (26.4k points)

According to the numpy docs, numpy.ndim(a) will returns:

The number of dimensions in a. Scalars are zero-dimensional

e.g.:

a = np.array(111)

b = np.array([1,2])

c = np.array([[1,2], [4,5]])

d = np.array([[1,2,3,], [4,5]])

print a.ndim, b.ndim, c.ndim, d.ndim

#outputs: 0 1 2 1

Here, the last array d is a variety of object dtype, so its dimension is as yet 1

What you wanna use could be a.shape (or a.size for a one-dimensional exhibit/array):

print a.size, b.size

print c.size # == 4, which is the total number of elements in the array

#outputs:

1 2

4

Here, the method .shape will returns you a tuple, you ought to get your measurement utilizing [0]:

print a.shape, b.shape, b.shape[0]

() (2L,) 2

Wanna become a Python expert? Come and join the python certification course and get certified.

For more details, do check out the below video tutorial...

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...