ARRAY AND VECTORIZED COMPUTATION
NumPy operations perform complex computations on entire arrays without the need for
Python for loops, which can be slow for large sequences. Arrays enable to perform mathematical
operations on whole blocks of data using similar syntax to the equivalent operations between scalar
elements.
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a)
Mathematical operations :
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a*10)
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a+a)
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a-a)
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a/a)
An ndarray is a generic multidimensional container for homogeneous data; that is, all of the
elements must be the same type. Every array has a shape, a tuple indicating the size of each
dimension, and a dtype, an object describing the data type of the array:
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a.shape)
O: (2,4)
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a.dtype)
O: dtype(float64)
NumPy internally stores data in a contiguous block of memory, independent of other built-in
Python objects. NumPy's library of algorithms written in the C language can operate on this
memory without any type checking or other overhead. NumPy arrays also use much less memory
than built-in Python sequences.
NumPy operations perform complex computations on entire arrays without the need for
Python for loops, which can be slow for large sequences. NumPy is faster than regular Python
code because its C-based algorithms avoid overhead present with regular interpreted Python
code.
range( ) and arange( )
range( ):
import numpy as np
for i in range(5)
print(i)
O: (1,2,3,4,5)
arange( )
numpy.arange() function creates an array of evenly spaced values within a given interval. It is
similar to Python's built-in range() function but returns a NumPy array instead of a list.
Syntax:
numpy.arange([start, ]stop, [step, ], dtype=None)
start (optional): The starting value of the sequence (inclusive). Defaults to 0 if not provided.
stop (required): The end value of the sequence (exclusive). The generated array will not include
this value.
step (optional): The spacing between consecutive values. Defaults to 1 if not provided.
dtype (optional): The desired data type of the output array elements. If not specified, NumPy
infers the type from the input arguments.
import numpy as np
arr = np.arange(5) # Creates an array [0, 1, 2, 3, 4]
print(arr)
import numpy as np
arr = np.arange(5,10) # Creates an array [5, 6, 7, 8, 9]
print(arr)
import numpy as np
arr = np.arange(0,10,2) # Creates an array [0, 2, 4, 6, 8]
print(arr)
n-dimensional
In NumPy, ndim is an attribute of a NumPy array (ndarray) that returns the number of
dimensions (or axes) of the array. It is a fundamental property for understanding the structure of
a multi-dimensional array
import numpy as np
a = np.array([[1,2,3,4]])
print(a.ndim)
O: 1
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8]])
print(a.ndim)
O: 2
import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8],[9,1,11,12]])
print(a.ndim)
O:3