SlideShare a Scribd company logo
Data Pre processing
SY Btech Sem:III
What is Data Preprocessing?
• Data preprocessing is a process of preparing
the raw data and making it suitable for a
machine learning model. It is the first and
crucial step while creating a machine learning
model.
Why do we need Data Preprocessing?
• data generally contains noises, missing values,
unusable format
• tasks for cleaning the data and making it
suitable for a machine learning model
• increasing the accuracy and efficiency of a
machine learning model.
Steps in Data Pre processing
• Getting the dataset
• Importing libraries
• Importing datasets
• Finding Missing Data
• Encoding Categorical Data
• Splitting dataset into training and test set
• Feature scaling
Python Libraries for Data Preprocessing
• NumPy
• Pandas
• Matplotlib
NumPy: Numerical Python
• NumPy is 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 we can use it
freely.
Import NumPy
• import numpy
• import numpy as np
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
import numpy as np
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
Create a NumPy ndarray Object
• The array object in NumPy is called ndarray.
• We can create a NumPy ndarray object by
using the array() function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Dimensions in Arrays
• 0-D Arrays
• 1-D Arrays
import numpy as np
arr = np.array(42)
print(arr)
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Array cont…
• 2-D Arrays
• 3-D arrays
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
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)
NumPy Array Indexing
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
Cont…
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])
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])
Cont…
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
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])
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
15
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
16
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
17
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
18
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
19
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
20
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
21
Arrays, creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like,
np.ones_like
• np.random.random
22
Arrays, danger zone
• Must be dense, no holes.
• Must be one type
• Cannot combine arrays of different shape
23
Slicing arrays
• taking elements from one given index to
another given index.
• [start:end]
• [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
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[:4])
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4])
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 1:4])
Data Types in NumPy
• strings - used to represent text data, the text is
given under quote marks. e.g. "ABCD"
• integer - used to represent integer numbers. e.g. -
1, -2, -3
• float - used to represent real numbers. e.g. 1.2,
42.42
• boolean - used to represent True or False.
• complex - used to represent complex numbers.
e.g. 1.0 + 2.0j, 1.5 + 2.5j
Cont…
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='i4')
print(arr)
print(arr.dtype)
import numpy as np
arr = np.array([1.1, 2.1, 3.1])
newarr = arr.astype(int)
print(newarr)
print(newarr.dtype)
NumPy Array Shape/Reshape
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2)
print(newarr)
NumPy Array Iterating
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
print(x)
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]])
for x in arr:
for y in x:
for z in y:
print(z)
Iterating Arrays Using nditer()
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in np.nditer(arr):
print(x)
import numpy as np
arr = np.array([1, 2, 3])
for idx, x in np.ndenumerate(arr):
print(idx, x)
Joining NumPy Arrays
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)
Joining Arrays Using Stack Functions
• Stacking Along Rows
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
print(arr)
Stacking Along Columns
• Stacking Along Height (depth)
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.dstack((arr1, arr2))
print(arr)
Splitting NumPy Arrays
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)
NumPy Searching Arrays
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
x = np.where(arr%2 == 0)
print(x)
Sorting Arrays
import numpy as np
arr = np.array([3, 2, 0, 1])
print(np.sort(arr))
import numpy as np
arr = np.array(['banana', 'cherry', 'apple'])
print(np.sort(arr))
Random Numbers in NumPy
• What is a Random Number?
– Random means something that can not be
predicted logically.
• Generate Random Number
from numpy import random
x = random.randint(100)
print(x)
Generate Random Float
• Generate Random Array
– x = random.randint(100, size=(3, 5))
– x = random.rand(3, 5)
– x = random.choice([3, 5, 7, 9])
from numpy import random
x = random.rand()
print(x)
from numpy import random
x=random.randint(100, size=(5))
print(x)

More Related Content

PPTX
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
PPTX
NUMPY [Autosaved] .pptx
coolmanbalu123
 
PPTX
NumPy.pptx
DrJasmineBeulahG
 
PPTX
NUMPY-2.pptx
MahendraVusa
 
PPTX
1.NumPy is a Python library used for wor
DrAtulZende
 
PDF
Essential numpy before you start your Machine Learning journey in python.pdf
Smrati Kumar Katiyar
 
PDF
Numpy - Array.pdf
AnkitaArjunDevkate
 
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
NUMPY [Autosaved] .pptx
coolmanbalu123
 
NumPy.pptx
DrJasmineBeulahG
 
NUMPY-2.pptx
MahendraVusa
 
1.NumPy is a Python library used for wor
DrAtulZende
 
Essential numpy before you start your Machine Learning journey in python.pdf
Smrati Kumar Katiyar
 
Numpy - Array.pdf
AnkitaArjunDevkate
 

Similar to Data Preprocessing Introduction for Machine Learning (20)

PPTX
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
tony8553004135
 
PDF
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
DineshThallapelly
 
PPTX
Presentation1.pptxvghvfhgvbn nbnmbnbjkbjkbjib
RahinTamboli
 
PPTX
Numpy in python, Array operations using numpy and so on
SherinRappai
 
PDF
Concept of Data science and Numpy concept
Deena38
 
PPTX
Numpy
Jyoti shukla
 
PDF
Introduction to NumPy
Huy Nguyen
 
PDF
Introduction to NumPy (PyData SV 2013)
PyData
 
PPTX
python_programming_NumPy_Pandas_Notes.pptx
sunilsoni446112
 
PPTX
Numpy.pptx
sureshR340784
 
PPTX
NumPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPpy.pptx
imaieabhinaw
 
PPTX
numpy code and examples with attributes.pptx
swathis752031
 
DOCX
Numpy in Python.docx
manohar25689
 
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
PDF
Scientific Computing with Python - NumPy | WeiYuan
Wei-Yuan Chang
 
PDF
Numpy questions with answers and practice
basicinfohub67
 
PPTX
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
PPT
PPS-UNIT5.ppt
Sivasankar Chandrasekaran
 
PPTX
NumPy.pptx
EN1036VivekSingh
 
PPTX
arraycreation.pptx
sathya930629
 
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
tony8553004135
 
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
DineshThallapelly
 
Presentation1.pptxvghvfhgvbn nbnmbnbjkbjkbjib
RahinTamboli
 
Numpy in python, Array operations using numpy and so on
SherinRappai
 
Concept of Data science and Numpy concept
Deena38
 
Introduction to NumPy
Huy Nguyen
 
Introduction to NumPy (PyData SV 2013)
PyData
 
python_programming_NumPy_Pandas_Notes.pptx
sunilsoni446112
 
Numpy.pptx
sureshR340784
 
NumPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPpy.pptx
imaieabhinaw
 
numpy code and examples with attributes.pptx
swathis752031
 
Numpy in Python.docx
manohar25689
 
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
Scientific Computing with Python - NumPy | WeiYuan
Wei-Yuan Chang
 
Numpy questions with answers and practice
basicinfohub67
 
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
NumPy.pptx
EN1036VivekSingh
 
arraycreation.pptx
sathya930629
 
Ad

More from sonali sonavane (11)

PPTX
Introduction To Pandas:Basics with syntax and examples.pptx
sonali sonavane
 
PPTX
Understanding_Copyright_Presentation.pptx
sonali sonavane
 
PPTX
Python chart plotting using Matplotlib.pptx
sonali sonavane
 
PPTX
SQL: Data Definition Language(DDL) command
sonali sonavane
 
PPTX
SQL Data Manipulation language and DQL commands
sonali sonavane
 
PPTX
Random Normal distribution using python programming
sonali sonavane
 
PPTX
program to create bell curve of a random normal distribution
sonali sonavane
 
PPTX
Data Preprocessing: One Hot Encoding Method
sonali sonavane
 
PPTX
Data Preprocessing:Feature scaling methods
sonali sonavane
 
PPTX
Data Preprocessing:Perform categorization of data
sonali sonavane
 
PPTX
NBA Subject Presentation08 march 24_A Y 2023-24.pptx
sonali sonavane
 
Introduction To Pandas:Basics with syntax and examples.pptx
sonali sonavane
 
Understanding_Copyright_Presentation.pptx
sonali sonavane
 
Python chart plotting using Matplotlib.pptx
sonali sonavane
 
SQL: Data Definition Language(DDL) command
sonali sonavane
 
SQL Data Manipulation language and DQL commands
sonali sonavane
 
Random Normal distribution using python programming
sonali sonavane
 
program to create bell curve of a random normal distribution
sonali sonavane
 
Data Preprocessing: One Hot Encoding Method
sonali sonavane
 
Data Preprocessing:Feature scaling methods
sonali sonavane
 
Data Preprocessing:Perform categorization of data
sonali sonavane
 
NBA Subject Presentation08 march 24_A Y 2023-24.pptx
sonali sonavane
 
Ad

Recently uploaded (20)

PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PDF
High Ground Student Revision Booklet Preview
jpinnuck
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Understanding operators in c language.pptx
auteharshil95
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
High Ground Student Revision Booklet Preview
jpinnuck
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 

Data Preprocessing Introduction for Machine Learning

  • 1. Data Pre processing SY Btech Sem:III
  • 2. What is Data Preprocessing? • Data preprocessing is a process of preparing the raw data and making it suitable for a machine learning model. It is the first and crucial step while creating a machine learning model.
  • 3. Why do we need Data Preprocessing? • data generally contains noises, missing values, unusable format • tasks for cleaning the data and making it suitable for a machine learning model • increasing the accuracy and efficiency of a machine learning model.
  • 4. Steps in Data Pre processing • Getting the dataset • Importing libraries • Importing datasets • Finding Missing Data • Encoding Categorical Data • Splitting dataset into training and test set • Feature scaling
  • 5. Python Libraries for Data Preprocessing • NumPy • Pandas • Matplotlib
  • 6. NumPy: Numerical Python • NumPy is 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 we can use it freely.
  • 7. Import NumPy • import numpy • import numpy as np import numpy arr = numpy.array([1, 2, 3, 4, 5]) print(arr) import numpy as np arr = numpy.array([1, 2, 3, 4, 5]) print(arr)
  • 8. Create a NumPy ndarray Object • The array object in NumPy is called ndarray. • We can create a NumPy ndarray object by using the array() function. import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr))
  • 9. Dimensions in Arrays • 0-D Arrays • 1-D Arrays import numpy as np arr = np.array(42) print(arr) import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr)
  • 10. Array cont… • 2-D Arrays • 3-D arrays import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(arr)
  • 11. 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)
  • 12. NumPy Array Indexing import numpy as np arr = np.array([1, 2, 3, 4]) print(arr[0]) import numpy as np arr = np.array([1, 2, 3, 4]) print(arr[2] + arr[3])
  • 13. Cont… 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]) 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])
  • 14. Cont… import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) print(arr[0, 1, 2]) 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])
  • 15. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 15
  • 16. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 16
  • 17. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 17
  • 18. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 18
  • 19. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 19
  • 20. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 20
  • 21. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 21
  • 22. Arrays, creation • np.ones, np.zeros • np.arange • np.concatenate • np.astype • np.zeros_like, np.ones_like • np.random.random 22
  • 23. Arrays, danger zone • Must be dense, no holes. • Must be one type • Cannot combine arrays of different shape 23
  • 24. Slicing arrays • taking elements from one given index to another given index. • [start:end] • [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
  • 25. import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5]) import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:]) import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[:4])
  • 26. import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1]) import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5:2])
  • 27. import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[1, 1:4]) import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[0:2, 1:4])
  • 28. Data Types in NumPy • strings - used to represent text data, the text is given under quote marks. e.g. "ABCD" • integer - used to represent integer numbers. e.g. - 1, -2, -3 • float - used to represent real numbers. e.g. 1.2, 42.42 • boolean - used to represent True or False. • complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j
  • 29. Cont… import numpy as np arr = np.array([1, 2, 3, 4], dtype='i4') print(arr) print(arr.dtype) import numpy as np arr = np.array([1.1, 2.1, 3.1]) newarr = arr.astype(int) print(newarr) print(newarr.dtype)
  • 30. NumPy Array Shape/Reshape import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(arr.shape) import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(2, 3, 2) print(newarr)
  • 31. NumPy Array Iterating import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: print(x) import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: for y in x: for z in y: print(z)
  • 32. Iterating Arrays Using nditer() import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) for x in np.nditer(arr): print(x) import numpy as np arr = np.array([1, 2, 3]) for idx, x in np.ndenumerate(arr): print(idx, x)
  • 33. Joining NumPy Arrays import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.concatenate((arr1, arr2)) print(arr) import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) arr = np.concatenate((arr1, arr2), axis=1) print(arr)
  • 34. Joining Arrays Using Stack Functions • Stacking Along Rows import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.stack((arr1, arr2), axis=1) print(arr) import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.hstack((arr1, arr2)) print(arr)
  • 35. Stacking Along Columns • Stacking Along Height (depth) import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.vstack((arr1, arr2)) print(arr) import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.dstack((arr1, arr2)) print(arr)
  • 36. Splitting NumPy Arrays import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np.array_split(arr, 3) print(newarr)
  • 37. NumPy Searching Arrays import numpy as np arr = np.array([1, 2, 3, 4, 5, 4, 4]) x = np.where(arr == 4) print(x) import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) x = np.where(arr%2 == 0) print(x)
  • 38. Sorting Arrays import numpy as np arr = np.array([3, 2, 0, 1]) print(np.sort(arr)) import numpy as np arr = np.array(['banana', 'cherry', 'apple']) print(np.sort(arr))
  • 39. Random Numbers in NumPy • What is a Random Number? – Random means something that can not be predicted logically. • Generate Random Number from numpy import random x = random.randint(100) print(x)
  • 40. Generate Random Float • Generate Random Array – x = random.randint(100, size=(3, 5)) – x = random.rand(3, 5) – x = random.choice([3, 5, 7, 9]) from numpy import random x = random.rand() print(x) from numpy import random x=random.randint(100, size=(5)) print(x)