0% found this document useful (0 votes)
4 views

Unit4

NumPy is a Python library designed for efficient array manipulation, offering significant speed advantages over traditional Python lists. It provides various array types, supports multiple dimensions, and includes built-in functions for aggregation and element-wise operations. The library is widely used in data science for its performance and ease of use in handling large datasets.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit4

NumPy is a Python library designed for efficient array manipulation, offering significant speed advantages over traditional Python lists. It provides various array types, supports multiple dimensions, and includes built-in functions for aggregation and element-wise operations. The library is widely used in data science for its performance and ease of use in handling large datasets.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Introduction to NumPy

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.

• The array object in NumPy is called ndarray, it provides a lot of supporting


functions that make working with ndarray very easy.

• 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?

• NumPy arrays are stored at one continuous place in memory unlike


lists, so processes can access and manipulate them very efficiently.
• This behavior is called locality of reference in computer science.
• This is the main reason why NumPy is faster than lists. Also it is
optimized to work with latest CPU architectures.
Installation of NumPy
• If you have Python and PIP already installed on a system, then installation
of NumPy is very easy.
• Install it using this command:
C:\Users\Your Name>pip install numpy
• Once NumPy is installed, import it in your applications by adding the
import keyword
• NumPy is usually imported under the np alias.
• Now the NumPy package can be referred to as np instead of numpy.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Checking NumPy Version

import numpy as np

print(np.__version__)
NumPy Creating Arrays
import numpy as np

arr = np.array([1, 2, 3, 4, 5])

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

arr = np.array([1, 2, 3, 4], ndmin=5)

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

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

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

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

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]

# creating numpy array


sample_array = np.array([list_1,
list_2,
list_3])
print("Numpy array :")
print(sample_array)
# print shape of the array
print("Shape of the array :",
sample_array.shape)
Shape of array
import numpy as np

sample_array = np.array([[0, 4, 2],


[3, 4, 5],
[23, 4, 5],
[2, 34, 5],
[5, 6, 7]])

print("shape of the array :",


sample_array.shape)
Rank:
Data type objects (dtype): Data type objects (dtype) is an instance
of numpy.dtype class. It describes how the bytes in the fixed-size block of
memory corresponding to an array item should be interpreted.
# Import module
import numpy as np
# Creating the array
sample_array_1 = np.array([[0, 4, 2]])
sample_array_2 = np.array([0.2, 0.4, 2.4])
# display data type
print("Data type of the array 1 :",
sample_array_1.dtype)
print("Data type of array 2 :",
sample_array_2.dtype)
Aggregation functions in numpy
NumPy has fast built-in aggregation functions for working on arrays; we'll discuss and
demonstrate some of them here.
max function
• Python numpy max function find the maximum element in an array.
• Syntax:
numpy.max(arr)
• Example
arr = numpy.array([1, 5, 4, 8, 3, 7])
max_element = numpy.max(arr)
minfunction
• Python numpy max function find the maximum element in an array.
• Syntax:
numpy.min(arr)
• Example
arr = numpy.array([1, 5, 4, 8, 3, 7])
max_element = numpy.min(arr)
Example on min and max function
# import numpy library
import numpy
# creating a numpy array of integers
arr = numpy.array([1, 5, 4, 8, 3, 7])
# finding the maximum and
# minimum element in the array
max_element = numpy.max(arr)
min_element = numpy.min(arr)
# printing the result
print('maximum element in the array is: ',
max_element)
print('minimum element in the array is: ',
min_element)
Example on min and max function
# import numpy library
import numpy
# creating a two dimensional
# numpy array of integers
arr = numpy.array([[11, 2, 3],
[4, 5, 16],
[7, 81, 22]])
# finding the maximum and
# minimum element in the array
max_element = numpy.max(arr)
min_element = numpy.min(arr)
# printing the result
print('maximum element in the array is:',
max_element)
print('minimum element in the array is:',
min_element)
Example on min and max function
# importing numpy library
import numpy
# creating two single dimensional array
a = numpy.array([1, 4, 6, 8, 9])
b = numpy.array([5, 7, 3, 9, 22])
# print maximum value
print(numpy.maximum(a, b))
The following table provides a list of useful aggregation functions
available in NumPy:
Find elements within range in numpy in Python
• n this approach we take an numpy array then apply the logical_and
function to it. The where clause in numpy is also used to apply the
and condition. The result is an array showing the position of the
elements satisfying the required range conditions.
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 = np.where(np.logical_and(A >= 6, A <= 15))
# Result
print("Array with condition : ", res)
Find elements within range in numpy in Python

• 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])

print 'First array:'


print a
print '\n'

print 'Second array:'


print b
print '\n'

print 'First Array + Second Array'


print a + b
Comparison Operators as ufuncs
• In Computation on NumPy Arrays: Universal Functions we introduced
ufuncs, and focused in particular on arithmetic operators. We saw
that using +, -, *, /, and others on arrays leads to element-wise
operations.
• NumPy also implements comparison operators such as < (less than)
and > (greater than) as element-wise ufuncs.
• The result of these comparison operators is always an array with a
Boolean data type. All six of the standard comparison operations are
available:
Example
import numpy as np
x = np.array([1, 2, 3, 4, 5])
print(x)
print(x < 3) # less than
print(x > 3) # greater than
print(x <= 3 )# less than or equal
print(x != 3) # not equal
print(x == 3 )# equal
Example
import numpy as np
rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
print(x)
print(x < 6)
Working with Boolean Arrays
• Given a Boolean array, there are a host of useful operations you can do.
We'll work with x, the two-dimensional array we created earlier.
• Counting entries
To count the number of True entries in a Boolean array, np.count_nonzero is
useful:
import numpy as np
rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
print(x)
print(x < 6)
# how many values less than 6?
print(np.count_nonzero(x < 6))
Working with Boolean Arrays
• We see that there are eight array entries that are less than 6. Another way
to get at this information is to use np.sum; in this case, False is interpreted
as 0, and True is interpreted as 1:
import numpy as np
rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
print(x)
print(x < 6)
# how many values less than 6?
print(np.count_nonzero(x < 6))
print(np.sum(x < 6))
• The benefit of sum() is that like with other NumPy aggregation functions,
this summation can be done along rows or columns as well:
np.sum(x < 6, axis=1)
Boolean Arrays as Masks
• A more powerful pattern is to use Boolean arrays as masks, to select
particular subsets of the data themselves. Returning to our x array
from before, suppose we want an array of all values in the array that
are less than, say, 5:
import numpy as np
rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
print(x)
print(x < 5)
• Now to select these values from the array, we can simply index on this
Boolean array; this is known as a masking operation:
• Print(x[x < 5])
Using the Keywords and/or Versus the Operators &/|
• One common point of confusion is the difference between the
keywords and and or on one hand, and the operators & and | on the
other hand. When would you use one versus the other?.
• The difference is this: and and or gauge the truth or falsehood of
entire object, while & and | refer to bits within each object.
• When you use and or or, it's equivalent to asking Python to treat the
object as a single Boolean entity. In Python, all nonzero integers will
evaluate as True. Thus:
print(bool(42), bool(0))
print(bool(42), bool(0))
print(bool(42 or 0))
Fancy Indexing
• With NumPy array fancy indexing, an array can be indexed with another NumPy array, a Python
list, or a sequence of integers, whose values select elements in the indexed array
• Example
• We first create a NumPy array with 11 floating-point numbers, and then index the array with
another NumPy array and Python list, to extract element numbers 0, 2, and 4 from the original
array:
import numpy as np
A = np.linspace(0, 1, 11)
print(A)
print(A[np.array([0, 2, 4])])
# The same thing can be accomplished by indexing with a Python list
print(A[[0, 2, 4]])
• Example
import numpy as np
a = np.arange(1, 10)
print(a)
indices = np.array([2, 3, 4])
print(a[indices])
Fancy Indexing
Example:
import numpy as np
x = np.arange(10, 1, -1)
print(x)
print(x[np.array([3, 3, 1, 8])])
Example:
import numpy as np
x = np.array([[1, 2], [3, 4], [5, 6]])
print(x[[0, 1, 2], [0, 1, 0]])
Example:
import numpy as np
y = np.arange(35).reshape(5, 7)
print(y)
print(y[np.array([0, 2, 4]), np.array([0, 1, 2])])
Fancy Indexing-advanced indexing
import numpy as np
x = np.array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
rows = np.array([[0, 0],
[3, 3]], dtype=np.intp)
columns = np.array([[0, 2],
[0, 2]], dtype=np.intp)
print(x[rows, columns])
• Sorting Numpy arrays
• Sorting means putting elements in an ordered sequence.
• Ordered sequence is any sequence that has an order corresponding to elements,
like numeric or alphabetical, ascending or descending.
• The NumPy ndarray object has a function called sort(), that will sort a specified
array.
• Example

import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))
• Example
import numpy as np

arr = np.array(['banana', 'cherry', 'apple'])

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)

You might also like