np.array()
constructornp.zeros()
np.ones()
np.empty()
np.full()
np.eye()
np.fromstring()
np.arange()
np.linspace()
np.random.rand()
np.random.randint()
np.zeros_like()
- ndim: The number of dimensions, can be 1, 2, 3, ... N
- axis: Each dimension is called an axis:
axis 0
is the vertical axis, goes from top to bottom, represents the number of rows,axis 1
is the horizontal axis, goes from left to right, represents the number of columns,axis 2
is the number of columns along z axis- rank: The number of axes is called the rank.
- shape: It is a tuple whose length is the rank and elements are dimension of each axis.
- size: It is the total number of elements, which is the product of all axis lengths.
- itemsize: It gives you the size in bytes of each element of the array (depends on dtype).
- nbytes: It gives you the total number of bytes occupied by entire array object in memory (size x itemsize).
To understand the internal memory layout of NumPy ndarray object, consider the four additional attributes:
- data: A pointer to block of data in RAM or in memory-mapped file. (Returns memory address)
- dtype: It gives you the dtype of the numPy array elements (fixed size value cells in memory)
- shape: It gives you a tuple indicating the array's shape
- strides: The strides attribute is a tuple of the same length as the number of axes (dimensions) of the array. The strides of an array tell us how many bytes we have to skip in memory to move to the next position along a certain axis. For example, in above 2-D array (shown in figure), we have to skip 4 bytes (1 value) to move to the next column, but 12 bytes (3 values) to get to the same position in the next row (for row-major).
- Row-Major means that if you have a two-dimensional array of data, the values in each row of the array are stored in adjacent memory locations
- Column-Major means that if you have a two-dimensional array of data, the values in each column of the array are stored in adjacent memory locations
- flags: It gives you information about the memory layout of the array.
import sys
!{sys.executable} -m pip install --upgrade pip
Requirement already satisfied: pip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (22.0.3) Collecting pip Using cached pip-22.0.4-py3-none-any.whl (2.1 MB) Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 22.0.3 Uninstalling pip-22.0.3: Successfully uninstalled pip-22.0.3 Successfully installed pip-22.0.4
# Unlike the other modules, we have been working so far, you have to download and install numPy
# To install this library in Jupyter notebook
import sys
!{sys.executable} -m pip install numpy
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.19.5)
import numpy as np
np.__version__ , np.__path__
('1.19.5', ['/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy'])
np.array()
Constructor¶np.array(seq, dtype)
dtype
argument specifies the data type of the array objects.dtype
if you want to limit memory usage.np.sctypes
import numpy as np
arr = np.array([])
print(arr)
print(type(arr))
print("array: ", arr)
print("arr.ndim: ", arr.ndim) # get number of dimensions (an empty array has number of dimensions equals to 1)
print("arr.shape: ", arr.shape) # This is an empty array, so have a shape of (0, )
print("arr.size: ", arr.size) # get total number of elements
print("arr.dtype: ", arr.dtype) # get data type, default for empty array is float64
print("arr.itemsize: ", arr.itemsize) # get size in bytes of each element of the array default is 8 for float64
print("arr.nbytes: ", arr.nbytes) # get number of bytes occupied by entire array object in memory
print("arr.data: ", arr.data)
print("arr.strides: ", arr.strides)
print("arr.flags: \n", arr.flags)
# Create a 0-D numpy array using array() constructor
import numpy as np
arr = np.array(10)
print("array: ", arr)
array: 10
print("array: ", arr)
print("arr.ndim: ", arr.ndim) # get number of dimensions (a 0-D array has number of dimensions equals to 0)
print("arr.shape: ", arr.shape) # This is a scalar value, so have no shape ( )
print("arr.size: ", arr.size) # get total number of elements, in this case it is 1
print("arr.dtype: ", arr.dtype) # get data type, default is what is minimum required to hold the only element
print("arr.itemsize: ", arr.itemsize) # get size in bytes of each element of the array default is 8 for int64
print("arr.nbytes: ", arr.nbytes) # get number of bytes occupied by entire array object in memory
print("arr.data: ", arr.data)
print("arr.strides: ", arr.strides)
print("arr.flags: \n", arr.flags)
array: 10 arr.ndim: 0 arr.shape: () arr.size: 1 arr.dtype: int64 arr.itemsize: 8 arr.nbytes: 8 arr.data: <memory at 0x7f853fcd0e00> arr.strides: () arr.flags: C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
arr = np.array([[5,3,2],[8,9,-1],[2,-3,6]])
arr
array([[ 5, 3, 2], [ 8, 9, -1], [ 2, -3, 6]])
for i in range(3):
for j in range(3):
print(arr[i,j])
5 3 2 8 9 -1 2 -3 6
# Create a 1-D numpy array using array() constructor of numpy
mylist = [1, 4, 2, 5, 3]
arr = np.array(mylist, dtype=np.uint8)
arr = np.array([1, 4, 2, 5, 3], dtype=np.uint32)
print("array: ", arr)
print("arr.ndim: ", arr.ndim) # get number of dimensions
print("arr.shape: ", arr.shape) # This is 1-D array having 5 columns, so return a tuple with one element
print("arr.size: ", arr.size) # get total number of elements
print("arr.dtype: ", arr.dtype) # get data type, default is what is minimum required to hold the max size element
print("arr.itemsize: ", arr.itemsize) # get size in bytes of each element of the array
print("arr.nbytes: ", arr.nbytes) # get number of bytes occupied by entire array object in memory (2*5=10)
print("arr.data: ", arr.data)
print("arr.strides: ", arr.strides)
print("arr.flags: \n", arr.flags)
array: [1 4 2 5 3] arr.ndim: 1 arr.shape: (5,) arr.size: 5 arr.dtype: uint32 arr.itemsize: 4 arr.nbytes: 20 arr.data: <memory at 0x7ffc21117e80> arr.strides: (4,) arr.flags: C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
Note once you print NumPy array elements they are displayed inside
[ ]
as a Python List, however, the elements are space separated in case of NumPy arrays, while in case of Python Lists the elements are comma separated
# creating 2-D (5x4) array using array constructor in numpy
mylist = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[3, 2, 4, 1],
[7, 3, 4, 9],
[4, 0, 3, 1]
]
arr = np.array(mylist)
print("array: \n", arr)
print("arr.ndim: ", arr.ndim) #get number of dimensions (2-D array)
print("arr.shape: ", arr.shape) #get dimension size.For a 2-D array, return a tuple with two elements (rows, cols)
print("arr.size: ", arr.size) #get total number of elements (5*4=20 elements)
print("arr.dtype: ", arr.dtype) #get data type, default is what is minimum required to hold the max size element
print("arr.itemsize: ", arr.itemsize) # get size in bytes of each element of the array
print("arr.nbytes: ", arr.nbytes) # get number of bytes occupied by entire array object in memory (8*20=160)
print("arr.data: ", arr.data)
print("arr.dtypes: ", arr.dtype)
print("arr.strides: ", arr.strides)
print("arr.flags: \n", arr.flags)
array: [[1 2 3 4] [5 6 7 8] [3 2 4 1] [7 3 4 9] [4 0 3 1]] arr.ndim: 2 arr.shape: (5, 4) arr.size: 20 arr.dtype: int64 arr.itemsize: 8 arr.nbytes: 160 arr.data: <memory at 0x7ffc21512ad0> arr.dtypes: int64 arr.strides: (32, 8) arr.flags: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
arr[1][1]
arr[1,1]
# Another example
mylist = [[1], [4], [2], [5], [3]]
arr = np.array(mylist)
arr = np.array([[1], [2], [3]])
print("array: \n", arr)
print("arr.ndim: ", arr.ndim) #get number of dimensions (2-D array)
print("arr.shape: ", arr.shape) #get dimension size.For a 2-D array, return a tuple with two elements (rows, cols)
print("arr.size: ", arr.size) #get total number of elements (5*4=20 elements)
print("arr.dtype: ", arr.dtype) #get data type, default is what is minimum required to hold the max size element
print("arr.itemsize: ", arr.itemsize) # get size in bytes of each element of the array
print("arr.nbytes: ", arr.nbytes) # get number of bytes occupied by entire array object in memory (8*20=160)
print("arr.data: ", arr.data)
print("arr.strides: ", arr.strides)
print("arr.flags: \n", arr.flags)
array: [[1] [2] [3]] arr.ndim: 2 arr.shape: (3, 1) arr.size: 3 arr.dtype: int64 arr.itemsize: 8 arr.nbytes: 24 arr.data: <memory at 0x7fbe36412a00> arr.strides: (8, 8) arr.flags: C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
# creating 3-D (5x4x3) array using array constructor in numpy
mylist = [
[[1, 2, 3], [5, 6, 7], [3, 2, 4], [7, 3, 4]],
[[8, 1, 6], [1, 9, 4], [5, 6, 4], [2, 6, 2]],
[[5, 3, 2], [2, 0, 5], [8, 3, 0], [5, 6, 9]],
[[6, 7, 1], [8, 1, 6], [1, 4, 7], [2, 0, 4]],
[[2, 8, 9], [3, 0, 4], [5, 2, 2], [1, 3, 8]],
]
arr = np.array(mylist, dtype=np.uint8)
print("array: \n", arr)
print("arr.ndim: ", arr.ndim) # get number of dimensions (3-D array)
print("arr.shape: ", arr.shape) # get dimension size.For a 3-D array, return a tuple with three elements
print("arr.size: ", arr.size) # get total number of elements (5*4*3=60 elements)
print("arr.dtype: ", arr.dtype) # get data type, default is what is minimum required to hold the max size element
print("arr.itemsize: ", arr.itemsize) # get size in bytes of each element of the array
print("arr.nbytes: ", arr.nbytes) # get number of bytes occupied by entire array object in memory (8*60=4800)
print("arr.data: ", arr.data)
print("arr.strides: ", arr.strides)
print("arr.flags: \n", arr.flags)
array: [[[1 2 3] [5 6 7] [3 2 4] [7 3 4]] [[8 1 6] [1 9 4] [5 6 4] [2 6 2]] [[5 3 2] [2 0 5] [8 3 0] [5 6 9]] [[6 7 1] [8 1 6] [1 4 7] [2 0 4]] [[2 8 9] [3 0 4] [5 2 2] [1 3 8]]] arr.ndim: 3 arr.shape: (5, 4, 3) arr.size: 60 arr.dtype: uint8 arr.itemsize: 1 arr.nbytes: 60 arr.data: <memory at 0x7ffc2159c130> arr.strides: (12, 3, 1) arr.flags: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
# Strides of 1-D array
import numpy as np
mylist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
arr = np.array(mylist, dtype=np.int64)
print("array: \n", arr)
print("arr.data: ", arr.data)
print("arr.shape: ", arr.shape)
print("arr.dtype: ", arr.dtype)
print("arr.size: ", arr.size)
print("arr.itemsize: ", arr.itemsize)
print("arr.nbytes: ", arr.nbytes)
print("arr.strides: ", arr.strides)
# Use of strides is in Indexing, slicing and reshaping
print(arr[3]) # jump 3*8=24 bytes and then read 8 bytes
# Strides of 2-D array
import numpy as np
mylist = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
arr = np.array(mylist, dtype=np.int64)
print("array: \n", arr)
print("arr.data: ", arr.data)
print("arr.shape: ", arr.shape)
print("arr.dtype: ", arr.dtype)
print("arr.size: ", arr.size)
print("arr.itemsize: ", arr.itemsize)
print("arr.nbytes: ", arr.nbytes)
print("arr.strides: ", arr.strides)
# Use of strides is in Indexing, slicing and reshaping
print(arr[1,2]) # jump 1*32+2*8=48 bytes and then read 8 bytes
# Strides of 3-D array
import numpy as np
mylist = [
[[0, 1], [2, 3]],
[[4, 5], [6, 7]],
[[8, 9], [10, 11]]
]
arr = np.array(mylist, dtype=np.int64)
print("array: \n", arr)
print("arr.data: ", arr.data)
print("arr.shape: ", arr.shape)
print("arr.dtype: ", arr.dtype)
print("arr.size: ", arr.size)
print("arr.itemsize: ", arr.itemsize)
print("arr.nbytes: ", arr.nbytes)
print("arr.strides: ", arr.strides)
# Use of strides is in Indexing, slicing and reshaping
print(arr[1,1,1]) # jump 1*32 + 0*16 + 1*8 =40 bytes and then read 8 bytes
help
function to learn more.np.zeros()
Method¶This method returns an array of given shape and type, filled with zeros.
numpy.zeros(shape, dtype=float)
# Creating 1-Dimensional array of all zeros
arr = np.zeros(3)
arr
# Notice that the elements are having the default data type as the float. That’s why the zeros are 0.
# Creating 2-Dimensional array filled with zeros
arr = np.zeros((2,3))
arr
# Creating 2-Dimensional array filled with zeros of integer types
arr = np.zeros((2,3), dtype=np.int16)
arr, arr.nbytes
np.ones()
Method¶This method returns an array of given shape and type, filled with ones.
numpy.ones(shape, dtype=float)
# Creating one-dimensional array with ones
arr = np.ones(3)
arr
# Creating 2-Dimensional array having 3 columns filled with ones
arr = np.ones((2,3))
arr
# Creating 2-Dimensional array having 3 columns filled with ones of integer types
arr = np.ones((2,3), dtype=np.int16)
arr
np.empty()
Method¶This method returns an array of given shape and type, filled with junk values.
numpy.empty(shape, dtype=float)
Un-like numpy.zeros(), and numpy.ones(), the numpy.empty() function doesn't initialize the entries, so they contain junk values.
arr = np.empty(3)
arr
arr = np.empty(3, dtype=np.int16)
arr
# Creating 2-Dimensional array having 3 columns filled with junk values
arr = np.empty((2,3), dtype=np.int16)
arr
np.full()
Method¶This method return a new array of given shape and type, filled with fill_value.
np.full(shape, fill_value, dtype=None)
arr = np.full(7, 54)
arr
arr = np.full(7, 21.5)
arr
arr = np.full((2,3), 21.5)
arr
np.eye()
Method¶This method is used to create a 2-D array with ones on the diagonal and zeros elsewhere.
eye(N, M=None, k=0 , dtype=float)
# creating 2-D array using eye function
# with 2 rows and 2 columns having 1's at on the main diagonal
eye_arr = np.eye(3,3)
eye_arr
# creating 2-D array of int type using eye function with 4 rows and 3 columns having 1's at on the main diagonal
eye_arr = np.eye(4, 3, dtype=np.int8)
eye_arr
# creating 2-D array of int type using eye function with 4 rows and 3 columns having 1's at one place higher
eye_arr = np.eye(4, 3, k = 1, dtype=int)
eye_arr
# creating 2-D array of int type using eye function with 4 rows and 3 columns having 1's at one place lower
eye_arr = np.eye(4, 3, k = -1, dtype=int)
eye_arr
np.fromstring()
Method¶This function is used to create a new 1-D array initialized from raw binary or text data in a string.
np.fromstring(string, dtype, sep)
# convert the space separated string '25 30 35 40' into an floats array
arr = np.fromstring('25 30 35 40', sep=' ')
arr
# convert the space separated string '25 30 35 40' into an integer array
arr = np.fromstring('25 30 35 40', sep=' ', dtype=np.uint8)
arr
# converting comma separated string into float type array
arr = np.fromstring('0.7, 10.4, 50.5', sep=",")
print(arr)
# It will raise error
arr = np.fromstring('0.57, str, 50.555', dtype=float, sep=',')
np.arange()
Method¶numpy.arange([start,] stop[, step])
The image shows the array created by np.arange(5,9,1)
import numpy as np
arr = np.arange(5)
print(arr)
print(type(arr))
print(arr.dtype)
[0 1 2 3 4] <class 'numpy.ndarray'> int64
arr = np.arange(5, dtype=float)
print(arr)
print(arr.dtype)
[0. 1. 2. 3. 4.] float64
arr = np.arange(5,10)
arr
array([5, 6, 7, 8, 9])
arr = np.arange(5, 10, 2)
arr
array([5, 7, 9])
arr = np.arange(5, 15, -1)
arr
array([], dtype=int64)
arr = np.arange(-1, -4, -.5)
arr
array([-1. , -1.5, -2. , -2.5, -3. , -3.5])
np.linspace()
Method¶This method by default returns an array of 50 evenly spaced elements starting from the first argument (inclusive) to the second argument (inclusive). The third argument, if given, is the count of number of elements of the array, default is 50.
numpy.linspace(start, stop, num=50)
arr = np.linspace(2,3,4)
print(arr)
print(arr.dtype)
[2. 2.33333333 2.66666667 3. ] float64
arr = np.linspace(1,2)
print(arr)
print(arr.dtype)
[1. 1.02040816 1.04081633 1.06122449 1.08163265 1.10204082 1.12244898 1.14285714 1.16326531 1.18367347 1.20408163 1.2244898 1.24489796 1.26530612 1.28571429 1.30612245 1.32653061 1.34693878 1.36734694 1.3877551 1.40816327 1.42857143 1.44897959 1.46938776 1.48979592 1.51020408 1.53061224 1.55102041 1.57142857 1.59183673 1.6122449 1.63265306 1.65306122 1.67346939 1.69387755 1.71428571 1.73469388 1.75510204 1.7755102 1.79591837 1.81632653 1.83673469 1.85714286 1.87755102 1.89795918 1.91836735 1.93877551 1.95918367 1.97959184 2. ] float64
arr = np.linspace(-1,-5)
print(arr)
[-1. -1.08163265 -1.16326531 -1.24489796 -1.32653061 -1.40816327 -1.48979592 -1.57142857 -1.65306122 -1.73469388 -1.81632653 -1.89795918 -1.97959184 -2.06122449 -2.14285714 -2.2244898 -2.30612245 -2.3877551 -2.46938776 -2.55102041 -2.63265306 -2.71428571 -2.79591837 -2.87755102 -2.95918367 -3.04081633 -3.12244898 -3.20408163 -3.28571429 -3.36734694 -3.44897959 -3.53061224 -3.6122449 -3.69387755 -3.7755102 -3.85714286 -3.93877551 -4.02040816 -4.10204082 -4.18367347 -4.26530612 -4.34693878 -4.42857143 -4.51020408 -4.59183673 -4.67346939 -4.75510204 -4.83673469 -4.91836735 -5. ]
np.random.rand()
Method¶numpy.random.rand(d0 [,d1] [,d2]....)
#Python's built-in random module has random() function that returns a single float between 0 and 1
import numpy as np
value = np.random.rand()
value
# Creating 1-D array of 5 elements of random floats between 0 and 1
arr = np.random.rand(5)
arr
array([0.20053717, 0.67323379, 0.28275728, 0.9770833 , 0.20820593])
# Creating 1-D array of 5 elements of random floats between 0 and 10
arr = np.random.rand(5)*10
arr
# Creating 2-D array (4x3) having random floats between 0 and 1
arr = np.random.rand(4,3)*10
arr
array([[2.52589781, 7.45726117, 2.01008613], [8.13257141, 5.63025205, 5.33048513], [9.96415767, 4.77972696, 9.99791874], [8.8997235 , 6.39801751, 1.25707233]])
# Creating 2-D array (4x3) having random floats between 0 and 10
arr = np.random.rand(4,3)*10
arr
array([[9.3194226 , 9.02291444, 9.55027305], [4.38278072, 6.02580758, 7.23071008], [1.53933886, 6.05247583, 1.21925365], [9.66975926, 6.15310811, 4.76285839]])
# Creating 3-D array of with random values using np.random.rand()
arr = np.random.rand(5,4,3)
arr
array([[[0.30197889, 0.49797858, 0.38725267], [0.70668659, 0.36086929, 0.73627458], [0.65957854, 0.29473759, 0.29208301], [0.11654146, 0.92557503, 0.67449756]], [[0.50938416, 0.70830489, 0.22475061], [0.67923685, 0.84583092, 0.20966569], [0.68397828, 0.43892349, 0.21607785], [0.92754154, 0.90540658, 0.88277135]], [[0.41907063, 0.90539579, 0.63679767], [0.31575133, 0.22486231, 0.76004992], [0.43865241, 0.14497764, 0.1188367 ], [0.50535223, 0.9428481 , 0.35010095]], [[0.48230864, 0.43042317, 0.53339408], [0.06012913, 0.52535111, 0.47999186], [0.31028082, 0.93291025, 0.80171092], [0.30478443, 0.39595128, 0.37472118]], [[0.92363078, 0.48968596, 0.6758323 ], [0.61803612, 0.33532638, 0.5253386 ], [0.26828096, 0.8244032 , 0.81776823], [0.7014707 , 0.70560875, 0.47130194]]])
np.random.randint()
Method¶This method returns an array of specified shape and fills it with random integers.
numpy.random.randint(low, high=None, size)
# Generating a random integer scalar b/w interval (0,9)
value = np.random.randint(10)
value
# Generating a random integer scalar b/w interval (5,19)
value = np.random.randint(low=5, high=20)
value
13
# Generating a random integer scalar from -5 to 4
value = np.random.randint(low=-5, high=5)
value
0
# creating 1-D array of size 6 of int type b/w interval (1,100)
arr = np.random.randint(low=1, high=101, size=6)
arr
array([ 99, 99, 100, 13, 76, 17])
# creating 1-D array of size 6 of int type b/w interval (-5,4)
arr = np.random.randint(low = -5, high = 5, size = 4)
arr
array([ 4, -3, -5, -4])
# By passing a tuple to size means rows and columns
arr = np.random.randint(low = 1, high = 10, size = (3,3))
arr
arr = np.random.randint(low = 1, high = 10, size = (2,3,4))
arr
np.zeros_like()
Method¶This method is used to get an array of zeros with the same shape and type as a given array.
zeros_like(arr, dtype=None)
# creating a 2-D array
mylist = [[0, 1],[2, 3]]
arr1 = np.array(mylist)
print("A 2-D array \n", arr1)
# creating the same array as the shape of 'arr' filled with zeros
arr2 = np.zeros_like(arr1)
print("\n Converted Array \n", arr2)
# creating 1-D array
arr1 = np.arange(10)
print("A 1-D array \n", arr1)
# creating the same array as the shape of 'arr' filled with zeros
arr2 = np.zeros_like(arr1)
print("\n Converted Array \n", arr2)
np.fromstring
np.eye(4)