Unit4
Unit4
What is Numpy?
• NumPy is a Python library.
• NumPy is used for working with arrays.
• NumPy is short for "Numerical Python".
• NumPy is a Python library and is written partially in Python, but most
of the parts that require fast computation are written in C or C++.
Why Use NumPy?
• In Python we have lists that serve the purpose of arrays, but they are slow to
process.
• NumPy aims to provide an array object that is up to 50x faster than traditional
Python lists.
• Arrays are very frequently used in data science, where speed and resources are
very important.
• Data Science: is a branch of computer science where we study how to store, use
and analyze data for deriving information from it.
Why is NumPy Faster Than Lists?
import numpy as np
print(np.__version__)
NumPy Creating Arrays
import numpy as np
print(arr)
print(type(arr))
Dimensions in Arrays
• 0-D Arrays - 0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example
import numpy as np
arr = np.array(42)
print(arr)
• 1-D Arrays - An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. These are the most common and basic arrays.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
• 2-D Arrays - An array that has 1-D arrays as its elements is called a 2-D array.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
• 3-D arrays- An array that has 2-D arrays (matrices) as its elements is called 3-D array.
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
Check Number of Dimensions?
• NumPy Arrays provides the ndim attribute that returns an integer
that tells us how many dimensions the array have.
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
Higher Dimensional Arrays
• An array can have any number of dimensions.
• When the array is created, you can define the number of dimensions
by using th
import numpy as np
print(arr)
print('number of dimensions :', arr.ndim)e ndmin argument.
Access Array Elements
• Array indexing is the same as accessing an array element.
• You can access an array element by referring to its index number.
• The indexes in NumPy arrays start with 0, meaning that the first
element has index 0, and the second has index 1 etc.
• Example
import numpy as np
print(arr[0])
Access 2-D Arrays
• To access elements from 2-D arrays we can use comma separated
integers representing the dimension and the index of the element.
• Think of 2-D arrays like a table with rows and columns, where the row
represents the dimension and the index represents the column.
• Example1
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])
• Example 2
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('5th element on 2nd row: ', arr[1, 4])
Access 3-D Arrays
• To access elements from 3-D arrays we can use comma separated
integers representing the dimensions and the index of the element.
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
• Negative Indexing
• Use negative indexing to access an array from the end.
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', arr[1, -1])
NumPy Array Slicing
• Slicing in python means taking elements from one given index to another given index.
• We pass slice instead of index like this: [start:end].
• We can also define the step, like this: [start:end:step].
• If we don't pass start its considered 0
• If we don't pass end its considered length of array in that dimension
• If we don't pass step its considered 1
• Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
• Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])
NumPy Array Slicing
• Negative Slicing
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
• STEP
Use the step value to determine the step of the slicing:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])
NumPy Array Slicing
• Slicing 2-D Arrays
• Example
• From the second element, slice elements from index 1 to index 4 (not
included):
import numpy as np
print(arr[1, 1:4])
Shape of array
importing numpy module
import numpy as np
# creating list
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
• Using *
In this approach we use the * operator. It gives the result as
actual values instead of the position of the values in the array.
import numpy as np
A = np.array([5, 9, 11, 4, 31, 27,8])
# printing initial array
print("Given Array : ", A)
# Range 6 to 15
res = A [ (A >=6) * (A <= 15)]
# Result
print("Array with condition : ", res)
Broadcasting Arrays
• Broadcasting is the name given to the method that Numpy uses to
allow array arithmetic between arrays with a different shape or size.
• Broadcasting solves the problem of arithmetic between arrays of
differing shapes by in effect replicating the smaller array along the
mismatched dimension.
• Generally people think that the smaller arrays are duplicated
but instead, it makes memory and computationally use of existing
structures in memory heap to achieve the same result.
Examples of Broadcasting in Numpy
• Scalar and One-Dimensional Array
from numpy import array
a = array([1, 2, 3])
print(a)
b=2
print(b)
c = a + b print(c)
Scalar and Two-Dimensional Array
# scalar and two-dimensional from numpy
import array A = array([[1, 2, 3], [1, 2, 3]])
print(A)
b = 2 print(b)
C=A+b
print(C)
Examples of Broadcasting in Numpy
import numpy as np
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))
• Example
import numpy as np
print(np.sort(arr))
• numpy.sort() : This function returns a sorted copy of an array.
• Parameters :
• arr : Array to be sorted.
• axis : Axis along which we need array to be started.
• order : This argument specifies which fields to compare first.
• kind : [‘quicksort’{default}, ‘mergesort’, ‘heapsort’]Sorting algorithm.
Sort a Numpy array in Descending Order
arr = np.array([6, 1, 4, 2, 18, 9, 3, 4, 2, 8, 11])
# Get a sorted copy of numpy array (Descending Order)
arr = np.sort(arr)[::-1]
print('Sorted Array in Descending Order: ', arr)
Sorting with axis
a = np.array([[1,4],[3,1]])
np.sort(a) # sort along the last axis
O/p-array([[1, 4],
[1, 3]])
np.sort(a, axis=None) # sort the flattened array
O/p-array([1, 1, 3, 4])
np.sort(a, axis=0) # sort along the first axis
O/p-array([[1, 1],
[3, 4]])
Use the order keyword to specify a field to
use when sorting a structured array:
dtype = [('name', 'S10'), ('height', float), ('age', int)]
values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),
('Galahad', 1.7, 38)]
a = np.array(values, dtype=dtype) # create a structured array
np.sort(a, order='height')
O/p-array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
('Lancelot', 1.8999999999999999, 38)],
dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
Sort by age, then height if ages are equal:
np.sort(a, order=['age', 'height'])
O/p-array([('Galahad', 1.7, 38), ('Lancelot', 1.8999999999999999, 38),
('Arthur', 1.8, 41)],
dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
Fast Sorting in NumPy
x = np.array([2, 1, 4, 3, 5])
np.sort(x)
O/p-array([1, 2,
A related function is argsort, which instead returns the indices of the
sorted elements:
x = np.array([2, 1, 4, 3, 5])
i = np.argsort(x)
print(i)
O/p-[1 0 3 2 4]
Sorting along rows or columns
rand = np.random.RandomState(42)
X = rand.randint(0, 10, (4, 6))
print(X)
O/p-[[6 3 7 4 6 9]
[2 6 7 4 3 7]
[7 2 5 4 1 7]
[5 1 4 0 9 5]]
# sort each column of X
np.sort(X, axis=0)
array([[2, 1, 4, 0, 1, 5],
[5, 2, 5, 4, 3, 7],
[6, 3, 7, 4, 6, 7],
[7, 6, 7, 4, 9, 9]])
# sort each row of X
np.sort(X, axis=1)
array([[3, 4, 6, 6, 7, 9],
[2, 3, 4, 6, 7, 7],
[1, 2, 4, 5, 7, 7],
[0, 1, 4, 5, 5, 9]])
Partial Sorts: Partitioning
• Sometimes we're not interested in sorting the entire array, but simply
want to find the k smallest values in the array. NumPy provides this in
the np.partition function. np.partition takes an array and a number K;
the result is a new array with the smallest K values to the left of the
partition, and the remaining values to the right, in arbitrary order:
• x = np.array([7, 2, 3, 1, 6, 5, 4])
• np.partition(x, 3)
• O/p-array([2, 1, 3, 4, 6, 5, 7])
Python: Numpy’s Structured Array
• Numpy’s Structured Array is similar to Struct in C. It is used for grouping
data of different types and sizes. Structure array uses data containers called
fields. Each data field can contain data of any type and size. Array elements
can be accessed with the help of dot notation.
• Example
# Python program to demonstrate
# Structured array
import numpy as np
a = np.array([('Sana', 2, 21.0), ('Mansi', 7, 29.0)],
dtype=[('name', (np.str_, 10)), ('age', np.int32), ('weight', np.float64)])
print(a)
Python: Numpy’s Structured Array
Example
# Python program to demonstrate
# Structured array
import numpy as np
a = np.array([('Sana', 2, 21.0), ('Mansi', 7, 29.0)],
dtype=[('name', (np.str_, 10)), ('age', np.int32), ('weight', np.float64)])
# Sorting according to the name
b = np.sort(a, order='name')
print('Sorting according to the name', b)
# Sorting according to the age
b = np.sort(a, order='age')
print('\nSorting according to the age', b)