0% found this document useful (0 votes)
20 views15 pages

Num Py

5th sem python notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

Num Py

5th sem python notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NumPy Introduction

What is NumPy?
NumPy is a Python library used for working with
arrays.
It also has functions for working in domain of
linear algebra, fourier transform, and matrices.
NumPy was created in 2005 by Travis Oliphant. It
is an open source project and you can use it freely.
NumPy stands for Numerical Python.

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.

Which Language is NumPy written in?


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++.

Import NumPy
Once NumPy is installed, import it in your
applications by adding the import keyword:
import numpy
Now NumPy is imported and ready to use.
Example
import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)

import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)
NumPy Creating Arrays

Create a NumPy ndarray Object


NumPy is used to work with arrays. The array
object in NumPy is called ndarray.
We can create a NumPy ndarray object by using
the array() function.
Example
import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)

print(type(arr))

To create an ndarray, we can pass a list, tuple or


any array-like object into the array() method, and
it will be converted into an ndarray:
Example
Use a tuple to create a NumPy array:
import numpy as np

arr = [Link]((1, 2, 3, 4, 5))


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.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np

arr = [Link]([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.
These are often used to represent matrix or 2nd
order tensors.
NumPy has a whole sub module dedicated
towards matrix operations called [Link]
Example
Create a 2-D array containing two arrays with the
values 1,2,3 and 4,5,6:
import numpy as np

arr = [Link]([[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.
These are often used to represent a 3rd order
tensor.
Example
Create a 3-D array with two 2-D arrays, both
containing two arrays with the values 1,2,3 and
4,5,6:
import numpy as np

arr = [Link]([[[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.

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
Get the first element from the following array:
import numpy as np

arr = [Link]([1, 2, 3, 4])

print(arr[3])
Example
Get the second element from the following array.
import numpy as np

arr = [Link]([1, 2, 3, 4])

print(arr[1])

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 dimension represents the row
and the index represents the column.
Example
Access the element on the first row, second
column:
import numpy as np

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

print('2nd element on 1st row: ', arr[0, 1])


Slicing arrays
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
Slice elements from index 1 to index 5 from the
following array:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])

Slice elements from index 4 to the end of the


array:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7])


print(arr[4:])

Slice elements from the beginning to index 4 (not


included):
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[:4])

STEP
Use the step value to determine the step of the
slicing:
Example
Return every other element from index 1 to index
5:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[Link])
Shape of an Array
The shape of an array is the number of elements
in each dimension.

Get the Shape of an Array


NumPy arrays have an attribute called shape that
returns a tuple with each index having the
number of corresponding elements.
Example
Print the shape of a 2-D array:
import numpy as np

arr = [Link]([[1, 2, 3, 4], [5, 6, 7, 8]])

print([Link])

Reshape From 1-D to 2-D


Example
Convert the following 1-D array with 12 elements
into a 2-D array.
The outermost dimension will have 4 arrays, each
with 3 elements:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = [Link](4, 3)

print(newarr)

Joining NumPy Arrays


Joining means putting contents of two or more
arrays in a single array.
In SQL we join tables based on a key, whereas in
NumPy we join arrays by axes.
We pass a sequence of arrays that we want to
join to the concatenate() function, along with the
axis. If axis is not explicitly passed, it is taken as
0.
Example
Join two arrays
import numpy as np

arr1 = [Link]([1, 2, 3])

arr2 = [Link]([4, 5, 6])


arr = [Link]((arr1, arr2))

print(arr)

Join two 2-D arrays along rows (axis=1):


import numpy as np

arr1 = [Link]([[1, 2], [3, 4]])

arr2 = [Link]([[5, 6], [7, 8]])

arr = [Link]((arr1, arr2), axis=1)

print(arr)

Splitting NumPy Arrays


Splitting is reverse operation of Joining.
Joining merges multiple arrays into one and
Splitting breaks one array into multiple.
We use array_split() for splitting arrays, we pass
it the array we want to split and the number of
splits.
Example
Split the array in 3 parts:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6])

newarr = np.array_split(arr, 3)

print(newarr)

Searching Arrays
You can search an array for a certain value, and
return the indexes that get a match.
To search an array, use the where() method.
Example
Find the indexes where the value is 4:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 4, 4])

x = [Link](arr == 4)

print(x)
Example
Find the indexes where the values are even:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8])

x = [Link](arr%2 == 0)

print(x)

Sorting 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
Sort the array:
import numpy as np
arr = [Link]([3, 2, 0, 1])

print([Link](arr))

Note: This method returns a copy of the array,


leaving the original array unchanged.

You might also like