Numpy Basics — 2

Devil’s Advocate
2 min readFeb 14, 2023

--

Continuing from our last post on Numpy … Here are some more common array methods in the Numpy library of Python.

array.ndim: returns the number of dimensions of an array.

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.ndim)
# Output: 2 (columns and rows)

array.dtype: returns the data type of an array.

In NumPy, arrays are homogeneous, which means that all elements in a NumPy array must be of the same data type.

import numpy as np
a = np.array([1, 2, 3], dtype=np.float32)
print(a)
# Output: [1. 2. 3.]
print(a.dtype)
# Output: float32

There are many data types in Numpy.

When you add a non-homogeneous entity to a NumPy array, NumPy will try to upcast the non-homogeneous entity to a compatible data type with the existing homogeneous entities in the array.

If the upcasting is not possible, NumPy will raise a ValueError.

A simple example:

import numpy as np
arr = np.array([1, 2, 3]) # create NumPy array of integers
print(arr.dtype) # O/P: int64
arr = np.append(arr, "four") # Add a string to array
print(arr) # O/P: ['1' '2' '3' 'four']
print(arr.dtype) # O/P: <U21> a unicode string array

In this example, NumPy upcasted the string “four” to a string data type to make it compatible with the existing integers in the array. Note that the resulting array is no longer of integer data type, but is instead of string data type.

If you try to add a non-homogeneous entity that cannot be upcasted to a compatible data type, NumPy will raise a ValueError. But that happens only if it cannot be upcasted automatically and not otherwise.

import numpy as np
arr = np.array([1, 2, 3])
# try to add a non-homogeneous entity (a list) to the array, it is upcasted
arr = np.append(arr, [4, 5])
print(arr)
# O/P: [1,2,3,4,5]

There is no error in this case. The 2D array is flattened and appended to the 1D array.

array.astype: returns an array with a new data type, without changing the original array.

import numpy as np
a = np.array([1, 2, 3], dtype=np.float32)
print(a)
# Output: [1. 2. 3.]
b = a.astype(np.int32)
print(b)
# Output: [1 2 3]

But if you try to convert an array to an incompatible data type, you get an error.

import numpy as np
arr = np.array(['one', 'two', 'three'])
arr = arr.astype(np.int64) # Convert string array to integer array
# O/P: ValueError: invalid literal for int() with base 10: 'one'

--

--

Devil’s Advocate
Devil’s Advocate

Written by Devil’s Advocate

Seeker for life. Looking to make technology simpler for everyone.