Numpy

Numpy is a Python library for scientific computing. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.

#Getting Started

#inroduction

  • NumPy stands for "Numerical Python" and is a Python library used for scientific computing and data analysis.

  • It provides support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions to operate on these arrays efficiently.

  • NumPy is built on top of the C programming language, which provides fast execution times and efficient memory usage.

  • NumPy arrays are homogeneous and can store only one data type (such as integers or floats).

#install and import

  • install numpy
pip install numpy
  • import numpy
import numpy as np

#Numpy Arrays

#creating Numpy Arrays from lists and tuples

  • From a Python List
my_list = [1, 2, 3, 4, 5]
arr = np.array(my_list)
  • From a Python Tuple
my_tuple = (1, 2, 3, 4, 5)
arr = np.array(my_tuple)
  • From a Python List of Lists
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr = np.array(my_list)
  • From a Python Tuple of Tuples
my_tuple = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
arr = np.array(my_tuple)

#Numpy Arrays using np.zeros

  • We can use the np.zeros() function to create an array of zeros.

  • this function takes shape parameter which can be a tuple of integers or just one integer specifying the shape of the array.

  • np.zeros()

arr = np.zeros(5)
print(arr)
# output
[0. 0. 0. 0. 0.]

arr = np.zeros((3, 4))
print(arr)

# output
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

#Numpy Arrays using np.ones

  • We can use the np.ones() function to create an array of ones.
  • this function takes shape parameter which can be a tuple of integers or just one integer specifying the shape of the array.
arr = np.ones(5)
print(arr)

# output
[1. 1. 1. 1. 1.]

arr = np.ones((3, 4))
print(arr)

# output
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

#Numpy Arrays using np.arange

  • We can use the np.arange() function to create an array of numbers in a given range.
  • this function takes start, stop and step parameters.
arr = np.arange(5, 10, 2)
print(arr)

# output
[5 7 9]

#Numpy Arrays using np.linspace

  • We can use the np.linspace() function to create an array of numbers in a given range.
  • this function takes start, stop and number of elements parameters.
arr = np.linspace(0, 10, 5)
print(arr)

# output
[ 0.   2.5  5.   7.5 10. ]

#Numpy Arrays using np.eye

  • We can use the np.eye() function to create an identity matrix.
  • this function takes n parameter which is the number of rows and columns of the identity matrix.
arr = np.eye(5)
print(arr)

# output
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]

arr = np.eye(3, 4)
print(arr)

# output
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]

#Numpy Arrays using np.random

  • We can use the np.random.rand() function to create an array of random numbers.
  • this function takes shape parameter which can be a tuple of integers or just one integer specifying the shape of the array.
arr = np.random.rand(5)

# output
[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548 ]
arr = np.random.rand(3, 4)

# output
[[0.64589411 0.43758721 0.891773   0.96366276]
 [0.38344152 0.79172504 0.52889492 0.56804456]
 [0.92559664 0.07103606 0.0871293  0.0202184 ]]
  • We can use the np.random.randint() function to create an array of random integers.
  • this function takes shape parameter which can be a tuple of integers or just one integer specifying the shape of the array.
arr = np.random.randint(5)
arr = np.random.randint(5, 10)
arr = np.random.randint(5, 10, 3)

#Numpy Array Attributes

#shape

  • The shape attribute returns a tuple of integers representing the size of the array in each dimension.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)

# output
(2, 3)

#ndim

  • The ndim attribute returns the number of dimensions of the array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)

# output
2

#size

  • The size attribute returns the total number of elements in the array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size)

# output
6

#dtype

  • The dtype attribute returns the data type of the array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.dtype)

# output
int32

#itemsize

  • The itemsize attribute returns the size of each element in the array in bytes.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.itemsize)

# output
4

#nbytes

  • The nbytes attribute returns the total size of the array in bytes.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.nbytes)

# output
24

#Numpy Array Indexing

#1D Array Indexing

  • We can use the index operator [] to access an element in a 1D array.
arr = np.array([1, 2, 3, 4, 5])
print(arr[0])

# output
1

#multi dimension Array Indexing

  • We can use the index operator [] to access an element in a multi dimension array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[0, 1])

# output
2

#Numpy Array Slicing

#1D Array Slicing

  • We can use the slice operator : to access a range of elements in a 1D array.
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:3])

# output
[2 3]

#multi dimension Array Slicing

  • We can use the slice operator : to access a range of elements in a multi dimension array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[0, 1:3])

# output
[2 3]

#Numpy Array Reshaping

#1D Array Reshaping

  • We can use the reshape() function to reshape a 1D array.
arr = np.array([1, 2, 3, 4, 5, 6])
print(arr.reshape(2, 3))

# output
[[1 2 3]
 [4 5 6]]

#multi dimension Array Reshaping

  • We can use the reshape() function to reshape a multi dimension array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.reshape(3, 2))

# output
[[1 2]
 [3 4]
 [5 6]]

#Numpy Array Iterating

#1D Array Iterating

  • We can use the for loop to iterate over a 1D array.
arr = np.array([1, 2, 3, 4, 5])
for x in arr:
  print(x)

#multi dimension Array Iterating

  • We can use the for loop to iterate over a multi dimension array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
  for y in x:
    print(y)

#Numpy Array Joining

#1D Array Joining

  • We can use the concatenate() function to join two or more 1D arrays.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)

# output
[1 2 3 4 5 6]

#multi dimension Array Joining

  • We can use the concatenate() function to join two or more multi dimension arrays.
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)

# output
[[1 2 5 6]
 [3 4 7 8]]

#Numpy Array Stacking

#1D Array Stacking

  • We can use the stack() function to stack two or more 1D arrays along rows.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)

# output
[[1 4]
 [2 5]
 [3 6]]

#multi dimension Array Stacking

  • We can use the stack() function to stack two or more multi dimension arrays along rows.
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.stack((arr1, arr2), axis=1)
print(arr)

# output
[[[1 2]
  [5 6]]

 [[3 4]
  [7 8]]]

#Numpy Array Splitting

#1D Array Splitting

#Splitting 1D Array into 3 Parts

  • We can use the array_split() function to split a 1D array into 3 parts.
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)

# output
[array([1, 2]), array([3, 4]), array([5, 6])]

#Splitting 1D Array into 4 Parts

  • We can use the array_split() function to split a 1D array into 4 parts.
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 4)
print(newarr)

# output
[array([1, 2]), array([3, 4]), array([5]), array([6])]

#multi dimension Array Splitting

#Splitting multi dimension Array into 3 Parts

  • We can use the array_split() function to split a multi dimension array into 3 parts.
arr = np.array([[1, 2, 3], [4, 5, 6]])
newarr = np.array_split(arr, 3)
print(newarr)

# output
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([], shape=(0, 3), dtype=int64)]

#Splitting multi dimension Array into 4 Parts

  • We can use the array_split() function to split a multi dimension array into 4 parts.
arr = np.array([[1, 2, 3], [4, 5, 6]])
newarr = np.array_split(arr, 4)
print(newarr)

# output
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([], shape=(0, 3), dtype=int64), array([], shape=(0, 3), dtype=int64)]

#Numpy Array Searching

#1D Array Searching

  • We can use the where() function to search for an element in a 1D array.
  • The where() function returns the indexes where the element exists.
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)

# output
(array([3, 5, 6], dtype=int64),)

#multi dimension Array Searching

  • We can use the where() function to search for an element in a multi dimension array.
  • The where() function returns the indexes where the element exists.
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
x = np.where(arr%2 == 0)
print(x)

# output
(array([0, 0, 1, 1, 1, 1], dtype=int64), array([1, 3, 0, 2, 3, 4], dtype=int64))

#Numpy Array Sorting

#1D Array Sorting

  • We can use the sort() function to sort a 1D array.
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))

# output
[0 1 2 3]

#multi dimension Array Sorting

  • We can use the sort() function to sort a multi dimension array.
arr = np.array([[3, 2, 4], [5, 0, 1]])
print(np.sort(arr))

# output
[[2 3 4]
 [0 1 5]]

#Numpy Array Filtering

#1D Array Filtering

  • We can filter an array by using a boolean index list.
arr = np.array([41, 42, 43, 44])
x = [True, False, True, False]
newarr = arr[x]
print(newarr)

# output
[41 43]

#multi dimension Array Filtering

  • We can filter a multi dimension array by using a boolean index list.
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
filter_arr = arr > 5
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)

# output
[[False False False False]
 [False  True  True  True]]
[6 7 8]

#Numpy Array Random

#Random Number

  • We can generate random numbers using the random() function.
from numpy import random
x = random.randint(100)
print(x)

# output
49

#Random Float

  • We can generate random float numbers using the random() function.
from numpy import random
x = random.rand()
print(x)

# output
0.9194024197301045

#Random Array

  • We can generate random array using the random() function.
from numpy import random
x = random.randint(100, size=(5))
print(x)

# output
[ 0  3  9  5  2]

#Random Array 2D

  • We can generate random 2D array using the random() function.
from numpy import random
x = random.randint(100, size=(3, 5))
print(x)

# output
[[ 0  3  9  5  2]
 [ 4  7  6  8  8]
 [ 1  6  7  7  0]]

#Random Float Array

  • We can generate random float array using the random() function.
from numpy import random
x = random.rand(5)
print(x)

# output
[0.91940242 0.7142413  0.99884701 0.1494483  0.86812606]

#Random Float Array 2D

  • We can generate random float 2D array using the random() function.
from numpy import random
x = random.rand(3, 5)
print(x)

# output
[[0.16595599 0.44064899 0.14090086 0.88212403 0.80655667]
 [0.0010831  0.96366276 0.38344152 0.79172504 0.52889492]
 [0.56804456 0.92559664 0.07103606 0.0871293  0.0202184 ]]

#Random Choice

  • We can choose random number from an array using the choice() function.
from numpy import random
x = random.choice([3, 5, 7, 9])
print(x)

# output
5

#Random Choice 2D

  • We can choose random number from an 2D array using the choice() function.
from numpy import random
x = random.choice([3, 5, 7, 9], size=(3, 5))
print(x)

# output
[[5 3 3 7 7]
 [7 5 5 5 3]
 [5 5 7 3 7]]

#Numpy Array Copy vs View

#Copy

  • The copy SHOULD NOT be affected by the changes made to the original array.
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42
print(arr)
print(x)

# output
[42  2  3  4  5]
[1 2 3 4 5]

#View

  • The view SHOULD be affected by the changes made to the original array.
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42
print(arr)
print(x)

# output
[42  2  3  4  5]
[42  2  3  4  5]

#Numpy Array Math

#Add

  • We can add two arrays and the result will be the sum of each element.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
arr = arr1 + arr2
print(arr)

# output
[ 6  8 10 12]

#Subtract

  • We can subtract two arrays and the result will be the difference of each element.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
arr = arr2 - arr1
print(arr)

# output
[4 4 4 4]

#Multiply

  • We can multiply two arrays and the result will be the product of each element.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
arr = arr1 * arr2
print(arr)

# output
[ 5 12 21 32]

#Divide

  • We can divide two arrays and the result will be the quotient of each element.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
arr = arr2 / arr1
print(arr)

# output
[5.         3.         2.33333333 2.        ]

#Power

  • We can raise the power of each element in an array using the power() function.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
arr = np.power(arr1, arr2)
print(arr)

# output
[    1    64  2187 65536]

#Remainder

  • We can get the remainder of each element in an array using the remainder() function.

arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
arr = np.remainder(arr2, arr1)
print(arr)

# output
[0 0 1 0]

#Numpy Array Mean Median Mode

#Mean

  • We can get the mean of an array using the mean() function.
arr = np.array([1, 2, 3, 4])
x = np.mean(arr)
print(x)

# output
2.5

#Median

  • We can get the median of an array using the median() function.
arr = np.array([1, 2, 3, 4, 5])
x = np.median(arr)
print(x)

# output
3.0

#Numpy Array Standard Deviation

#Standard Deviation

  • We can get the standard deviation of an array using the std() function.

arr = np.array([1, 2, 3, 4])
x = np.std(arr)
print(x)

# output
1.118033988749895