2 views
in Python
closed

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

closed

by (25.7k points)
selected

The ndim attribute in NumPy returns the number of dimensions or axes of a given array. In your case, the array a is a one-dimensional array, also known as a 1D array or a vector. Although you have specified three elements in the array [1, 2, 3, 4], it is still considered a 1D array because those elements are arranged in a single row or a single dimension.

If you want to create a 3D array, you need to provide nested lists or arrays to represent multiple dimensions. Here's an example:

import numpy as np

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

[4, 5, 6],

[7, 8, 9]]])

print(a.ndim)

In this case, a is a 3D array because it has three nested levels, representing three dimensions. The ndim attribute will return 3 in this example.

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...

by (15.4k points)
The ndim attribute in NumPy provides information about the number of dimensions or axes in an array. In your code, the array a is a one-dimensional array, often referred to as a vector or a 1D array. Despite having three elements [1, 2, 3, 4], it is considered a 1D array because the elements are arranged in a single dimension or row.

To create a three-dimensional (3D) array, you need to use nested lists or arrays to represent multiple dimensions. Here's an example:

import numpy as np

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

[4, 5, 6],

[7, 8, 9]]])

print(a.ndim)

In this case, a is a 3D array as it contains three levels of nesting, representing three dimensions. The ndim attribute will output 3 in this example.
by (19k points)
The ndim attribute in NumPy provides the number of dimensions in an array. In your code, the array a is one-dimensional (1D) because it has a single row of elements. The ndim attribute returns 1 to indicate the array's dimensionality. To create a three-dimensional array, you need to use nested lists or arrays to represent multiple dimensions.