Special functions

Lesson 4 Chapter 2

We can create Numpy arrays using some special Numpy functions. I used a couple of them below for your reference:

# Import Numpy library import numpy as np ### Defining arrays using special functions ### # Arguments: # shape: The shape of the numpy array # dtype: Specifying the data type (not required) # Defining an all-zero array zeroArray = np.zeros(shape=(3,5), dtype=np.int16 ) print("zeroArray: ", zeroArray) # Defining an all-one array onesArray = np.ones(shape=(3,5), dtype=np.float32 ) print("onesArray: ", onesArray) # Defining an array filled with one specific elements fullArray = np.full(shape=(3,5), fill_value=4.2, dtype=np.float64 ) print("fullArray: ", fullArray)

zeroArray: [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] onesArray: [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] fullArray: [[4.2 4.2 4.2 4.2 4.2] [4.2 4.2 4.2 4.2 4.2] [4.2 4.2 4.2 4.2 4.2]]

One of the most important special functions is **np.arange **which is similar to **Python range built-in function**. An example of using **np.arrage** is as below:

# Import Numpy library import numpy as np # Define a Numpy array using np.arrage # np.arrage defines an interval of numbers # Arguments: # start: Starting of the interval. # stop: Ending of the interval. # step: Step size. # NOTE: The interval includes "start" number but does NOT include "stop" number. arr = np.arange(start=3,stop=10,step=2) print("Array: ", arr)

Array: [3 5 7 9]