Numpy Basics — 3

Devil’s Advocate
3 min readFeb 15, 2023

--

Continuing from the last post on Numpy, let’s look at a few more nuances in using the array functions of Numpy.

np.zeros: returns a new array of a given shape and type, filled with zeros.

import numpy as np
a = np.zeros((2, 3), dtype=np.int32)
print(a)
# Output:
# [[0 0 0]
# [0 0 0]]

np.ones: returns a new array of the given shape and type, filled with ones.

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

The zeros() and ones() functions are used to create arrays of the same size as an existing array but filled with zeros or ones, respectively. This can be useful when you need to perform arithmetic operations on arrays of different sizes.

import numpy as np
# create a 2x2 array of zeros of the same size as an existing array
arr = np.array([[1, 2], [3, 4]])
arr_zeros_like = np.zeros_like(arr)
print(arr_zeros_like)
# Output: [[0 0]
# [0 0]]

np.arange: returns an array with evenly spaced values within a given interval. This is an interesting way to quickly generate odd or even number arrays.

import numpy as np
a = np.arange(0, 10, 2)
# Starting from 0 to 10, space each value with a gap of 2
print(a)
# Output: [0 2 4 6 8]

By default, it can generate only a 1D array. But if one wishes to create a 2D array out of the output, there is an indirect way, using the reshape method.

import numpy as np
# create a 1D array with 12 elements
arr_1d = np.arange(12)
# reshape the 1D array to a 2D array with shape (3, 4)
arr_2d = arr_1d.reshape(3, 4)
print(arr_2d)

array.sum: returns the sum of all elements in an array.

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a.sum())
# Output: 21

Interestingly, the array.sum() function returns the sum of all the elements of the input array, regardless of the number of dimensions.

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

The sum() function also provides an optional parameter axis that can be used to specify the axis or axes along which the sum is to be performed.

When the axis parameter is specified, the function returns an array with reduced dimensionality.

import numpy as np
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(np.sum(a, axis=0))
# Output: array([[ 6, 8],
# [10, 12]])

Here, the axis=0 parameter indicates that the sum should be taken along the first dimension of the input array resulting in a (2, 2) array.

array.mean: returns the average of all elements in an array.

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a.mean())
# Output: 3.5

Here is an example with a 2-dimensional array.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
mean = arr.mean()
print(mean)
# Output: 3.5

Similar to the array.sum function, array.mean also takes in an axis parameter.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
mean_row = arr.mean(axis=1)
print(mean_row)
# Output: [2. 5.]

The resulting array has shape (2,) with the mean of each row of the input array.

--

--

Devil’s Advocate

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