Professional Documents
Culture Documents
Unit-2-MCA275-PPT-Part-1
Unit-2-MCA275-PPT-Part-1
Unit-2-MCA275-PPT-Part-1
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S
Scrapy: It is an open-source library that is used for extracting data from websites. It
provides very fast web crawling and high-level screen scraping. It can also be used for
data mining and automated testing of data.
Scikit-learn: It is a famous Python library to work with complex data. Scikit-learn is an
open-source library that supports machine learning. It supports variously supervised
and unsupervised algorithms like linear regression, classification, clustering, etc. This
library works in association with Numpy and SciPy.
PyGame: This library provides an easy interface to the Standard Directmedia Library
(SDL) platform-independent graphics, audio, and input libraries. It is used for
developing video games using computer graphics and audio libraries along with Python
programming language.
PyTorch: PyTorch is the largest machine learning library that optimizes tensor
computations. It has rich APIs to perform tensor computations with strong GPU
acceleration. It also helps to solve application issues related to neural networks.
PyBrain: The name “PyBrain” stands for Python Based Reinforcement Learning,
Artificial Intelligence, and Neural Networks library. It is an open-source library built
for beginners in the field of Machine Learning. It provides fast and easy-to-use
algorithms for machine learning tasks. It is so flexible and easily understandable and
that’s why is really helpful for developers that are new in research fields.
Libraries in Python
Example:
>>> sqrt(25)
5.0
>>> csqrt(25)
Libraries in Python
The variable m now points to the math module object which means we can't
say math.pi anymore:
>>> m.pi
3.141592653589793
>>> m.sqrt(25)
5.0
Libraries in Python
NumPy stands for numeric python which is a python package for the
computation and processing of the multidimensional and single
dimensional array elements.
Travis Oliphant created NumPy package in 2005 by injecting the features
of the ancestor module Numeric into another module Numarray.
It is an extension module of Python which is mostly written in C. It provides
various functions which are capable of performing the numeric
computations with a high speed.
NumPy provides various powerful data structures, implementing multi-
dimensional arrays and matrices. These data structures are used for the
optimal computations regarding arrays and matrices.
Need of NumPy
NumPy provides a convenient and efficient way to handle the vast amount of
data. NumPy is also very convenient with Matrix multiplication and data
reshaping. NumPy is fast which makes it reasonable to work with a large set
of data.
Libraries in Python
Arrays are very frequently used in data science, where speed and
resources are very important.
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:
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Output: [1, 2, 3, 4, 5]
Libraries in Python
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 = np.array((1, 2, 3, 4, 5))
print(arr)
Output: [1 2 3 4 5]
Arrays in NumPy:
It is a table of elements (usually numbers), all of the same type, indexed
by a tuple of positive integers.
In NumPy, dimensions are called axes. The number of axes is rank.
NumPy array class is called ndarray. It is also known by the alias array.
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
nested array: are arrays that have arrays as their elements.
Libraries in Python
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
#Create a 0-D array with value 42
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.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Libraries in Python
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
NumPy has a whole sub module dedicated towards matrix operations called
numpy.mat
Example
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
Output:
[[1 2 3]
Libraries in Python
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 = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
Output:
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
Libraries in Python
Example:
Check how many dimensions the arrays 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]]) output:
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) 0
print(a.ndim) 1
print(b.ndim) 2
print(c.ndim) 3
print(d.ndim)
Libraries in Python
When the array is created, you can define the number of dimensions by using
the ndmin argument.
Example
Create an array with 5 dimensions and verify that it has 5 dimensions:
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)
output:
[[[[[1 2 3 4]]]]]
Libraries in Python
NumPy arrays
The NumPy array - an n-dimensional data structure - is the central object of
the NumPy package.
A one-dimensional NumPy array can be thought of as a vector, a two-
dimensional array as a matrix (i.e., a set of vectors), and a three-dimensional
array as a tensor (i.e., a set of matrices)
Need more than three dimensions?
It's entirely possible to have arrays with many dimensions, including so many
dimensions that it's no longer humanly possible to conceptualize them .
Libraries in Python
Shape of an Array
The shape of an array is the number of elements in each dimension.
Example:
In this example, we are creating a two-dimensional array that has the rank of 2 as it has 2 axes.
The first axis(dimension) is of length 2, i.e., the number of rows, and the second axis(dimension)
is of length 3, i.e., the number of columns. The overall shape of the array can be represented as
(2, 3)
import numpy as np
# Creating array object
arr = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
Output:
# Printing type of arr object Array is of type: <class 'numpy.ndarray'>
print("Array is of type: ", type(arr)) No. of dimensions: 2
# Printing array dimensions (axes) Shape of array: (2, 3)
print("No. of dimensions: ", arr.ndim) Size of array: 6
Array stores elements of type: int64
# Printing shape of array
print("Shape of array: ", arr.shape)
Reshaping arrays
Reshaping means changing the shape of an array.
Convert the following 1-D array with 12 elements into a 3-D array.
The outermost dimension will have 2 arrays that contains 3 arrays, each with
2 elements: Output:
import numpy as np [[[ 1 2]
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) [ 3 4]
newarr = arr.reshape(2, 3, 2) [ 5 6]]
print(newarr)
[[ 7 8]
# Reshaping 3X4 array to 2X2X3 array
[ 9 10]
arr = np.array([[1, 2, 3, 4], [11 12]]]
[5, 2, 4, 2],
[1, 2, 0, 1]])
newarr = arr.reshape(2, 2, 3)
Example1:
import numpy as np
Output:
[1 2 3 4 5 6]
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
Example2: Output:
import numpy as np [[ 1 2 9 5 6]
arr1 = np.array([[1, 2], [3, 4]]) [ 3 4 12 7 8]]
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)
Libraries in Python
If the array has less elements than required, it will adjust from the end accordingly.
Example
Output:
Split the array in 4 parts:
[array([1, 2]), array([3, 4]), array([5]), array([6])]
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 4)
print(newarr)
Libraries in Python
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
Output:
newarr = np.array_split(arr, 3) [1 2]
print(newarr[0]) [3 4]
[5 6]
print(newarr[1])
print(newarr[2])
Libraries in Python
Searching Arrays:
You can search an array for a certain value, and return the indexes that
get a match.
Example2:
Output:
import numpy as np (array([1, 3, 5, 7]),)
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
x = np.where(arr%2 == 0)
Libraries in Python
Sorting Arrays:
Sorting means putting elements in an ordered sequence.
The NumPy ndarray object has a function called sort(), that will sort a specified
array. Sorting a 2-D Array
If you use the sort() method on a 2-
Example1: D array, both arrays will be sorted:
import numpy as np
arr = np.array([3, 2, 0, 1]) import numpy as np
print(np.sort(arr)) arr = np.array([[3, 2, 4], [5, 0, 1]])
You can also sort arrays of strings, print(np.sort(arr))
Output:
or any other data type:
[[2 3 4]
Example2:
[0 1 5]]
import numpy as np
Output:
arr = np.array(['banana', 'cherry', 'apple'])
['apple' 'banana' 'cherry']
print(np.sort(arr))