RASPBERRY PI COURSE GUIDE
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
NumPy
Numpy is a general-purpose array-processing package. 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.
Besides its obvious scientific uses, Numpy can also be used as an efficient multi-
dimensional container of generic data.
An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed
by using square brackets and can be initialized by using nested Python Lists.
Creating a Numpy Array:
Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining
the size of the Array. Arrays can also be created with the use of various data types such
as lists, tuples, etc. The type of the resultant array is deduced from the type of the
elements in the sequences.
Note: Type of array can be explicitly defined while creating the array.
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
# Python program for
# Creation of Arrays
import numpy as np
# Creating a rank 1 Array
arr = [Link]([1, 2, 3])
print("Array with Rank 1: \n",arr)
# Creating a rank 2 Array
arr = [Link]([[1, 2, 3],
[4, 5, 6]])
print("Array with Rank 2: \n", arr)
# Creating an array from tuple
arr = [Link]((1, 3, 2))
print("\nArray created using "
"passed tuple:\n", arr)
Output:
Array with Rank 1:
[1 2 3]
Array with Rank 2:
[[1 2 3]
[4 5 6]]
Array created using passed tuple:
[1 3 2]
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
Basic Array Operations
In numpy, arrays allow a wide range of operations which can be performed on a particular
array or a combination of Arrays. These operation include some basic Mathematical
operation as well as Unary and Binary operations
.
# Python program to demonstrate
# basic operations on single array
import numpy as np
# Defining Array 1
a = [Link]([[1, 2],
[3, 4]])
# Defining Array 2
b = [Link]([[4, 3],
[2, 1]])
# Adding 1 to every element
print ("Adding 1 to every element:", a + 1)
# Subtracting 2 from each element
print ("\nSubtracting 2 from each element:", b - 2)
# sum of array elements
# Performing Unary operations
print ("\nSum of all array "
"elements: ", [Link]())
# Adding two arrays
# Performing Binary operations
print ("\nArray sum:\n", a + b)
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
Output:
Adding 1 to every element:
[[2 3]
[4 5]]
Subtracting 2 from each element:
[[ 2 1]
[ 0 -1]]
Sum of all array elements: 10
Array sum:
[[5 5]
[5 5]]
Different Methods:
[Link]()
[Link]()
[Link]()
[Link] will tell you the number of axes, or dimensions, of the array.
[Link] will tell you the total number of elements of the array. This is the product of
the elements of the array’s shape.
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
[Link] will display a tuple of integers that indicate the number of elements
stored along each dimension of the array. If, for example, you have a 2D array with 2
rows and 3 columns, the shape of your array is (2,3).
arr2d = [Link]([[1,2,3,3],[4,5,6,2],[7,8,9,4]])
print("Shape", [Link])
print(arr2d)
Output:
Shape (3, 4)
[[1 2 3 3]
[4 5 6 2]
[7 8 9 4]]
[Link]
Return evenly spaced values within a given interval.
Values are generated within the half-open interval [start, stop) (in other words, the
interval including start but excluding stop). For integer arguments the function is
equivalent to the Python built-in range function, but returns an ndarray rather than a list.
Syntax: [Link](start, stop, step)
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
[Link]
[Link](low, high=None, size=None)
Return random integers from low (inclusive) to high (exclusive).
Return random integers from the “discrete uniform” distribution of the specified dtype in
the “half- open” interval [low, high). If high is None (the default), then results are from [0,
low).
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
Different Operations:
NumPy Indexing of Matrix
Matrices and Arrays can be indexed using the standard Python x[obj] syntax, where x is
the array and obj the selection. There are different kinds of indexing available
depending on obj: basic indexing, advanced indexing and field access.
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
Most of the above examples show the use of indexing when referencing data in a
matrix.
Slicing of Array in NumPy
In Python, 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
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
• If we don't pass step its considered 1
Slicing of Matrix in NumPy
It provides a high-performance multidimensional array object and tools for working with
these arrays. For a Multidimensional array, you can use reshape() method along with
arrange() method.
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]
thingsRoam Academy Contact: +92-308-1222240 [Link]
Email: academy@[Link]