SlideShare a Scribd company logo
1 of 16
Download to read offline
CSE 202
Introduction to Data Science
1. NumPy Library
Dr. Ahmed Ali
 Creating NumPy Arrays
 NumPy Array Attributes
 Indexing
 Slicing
 Sorting
 Copy of Array
 Reshaping
 Concatenate/Split
 Vectorized Operations
 Ufuncs
 Broadcasting
 Masking
Lecture
Outline
 Is one of the two most commonly used libraries for scientific work. The
other library is scipy which is a little bit advanced.
 The other libraries we are going to use in this course are depend on NumPy.
 How to get NumPy? It is very easy, just import in Anaconda. We don’t have
to install anything. (pip install NumPy)
 What are the difference between list and array in python?
List is used in python
Array is used in NumPy.
 One of the advantages of NumPy is that the built-in methods are easy to
use. And they are very fast and efficient in execution because they are encoded
in Fortran an C languages.
 NumPy has so many built-in constants
 Use help command to get more information about any command or method
in NumPy.
NumPy
Library
Creating NumPy
Arrays
Do the following tasks:
1. Create an integer array of 10 elements filled with
all zeros.
2. Create a 3x5 floating-point array filled with ones.
3. Create a 3x5 array filled with pi values.
4. Create a 3x3 array of uniformly distributed
random values between 0 and 1.
5. Create a 3x3 array of random integers in the
interval [0, 10).
6. Create a 3x3 identity matrix.
7. Create an array of 10 elements uniformly dividing
the interval [0,1], i.e, 0, 0.1,..., 0.9, 1.
Numpy array have the following attributes:
1.ndim (the number of dimensions),
2.shape (the size of each dimension), and
3.size (the total size of the array).
For example, do the following:
1.Create a random one-dimensional array (x1),
and check all the above attributes.
2.Create a random two-dimensional array (x2),
and check all the above attributes.
NumPy
Array
Attributes
NumPy array indexing for 1d array is same as
Python's lists. However, NumPy nd-array
indexing is slightly different from Python's nd-
lists. For example:
1.2d-list elements are accessed
as:[index1][index2]
2.2d-array elements are accessed
as:[index1,index2]
where index1 and index2 can be indices or
slices. See the following examples:
Indexing
Slicing refer to selecting/extracting a sub-array from
a given array. As an example for 1d arrays, do the
following tasks:
1.Create a random 1d NumPy array of size 10.
2.Display the first five elements.
3.Display elements from index 5 to end.
4.Display elements from index 4 to 6.
5.Display alternate elements from the first element.
6.Display alternate elements starting from the second
element.
7.Display elements starting from fourth element from
the end and ending at last but one element.
8.Display all element in the reverse order of indices.
Slicing
Slicing
As an example for 2d arrays, do the following
tasks:
1.Create a random 2d NumPy array of size 3x4.
2.Display first two rows, first three columns
3.Display all rows, and every other column
4.Display all rows and columns in the reverse
order
5.Display first column
6.Display first row
7.Display third row elements in order of column
3, then column 1 then column 2
8.Display second and following row elements in
order of column 3, then column 1 then
column 2
Sorting
NumPy .sort() method sorts an array in-place.
The sorting is done row/column wise, and
order/preference can be provided too. As an
example, do the following:
1. Create a 1d random array of size 6, and sort it.
Print the corresponding original indices in the
sorted array.
2. Create a random 2d NumPy array of size 3x4. Sort
row wise.
3. Create a random 2d NumPy array of size 3x4. Sort
column wise.
Copy of
Array
Arrays are mutable too. Thus, the
assignment "=" is by reference. Use
".copy()" to copy the entire NumPy
array. For example, see the code in
the following cells:
Reshaping Numpy .reshape method gives a new
shape to an existing array without
changing its data. As an exmaple, do the
following task:
1.Create a 1d random array of size 10.
2.Reshape the 1d array into size 5x2.
3.Reshape the 1d array into size 2x5.
 Concatenate means joining, and Numpy's .concatenate() function is used to join
two or more arrays of the same shape along a specified axis. Numpy's .vstack()
(.hstack()) can be used for row wise
 (column wise) joining. Numpy's split/vsplit/hsplit methods are for
breaking/splitting the array, opposite to the concatenation.
 For example, see the code in the following cells:
Concatenate
/Split
 Vectorized operations are perhaps the most crucial factor to the wide usage of
Numpy library. Vectorized operations simply put are those operations that can be
done on arrays without using loops.
 For example, do the following:
1. 1. Create a random id array, and add, subtract,multiply and divide all the
elements with 5.
2. 2. Take the square of all elements.
3. 3. Find remainder w.r.t 2 for all elements.
4. 4. Transform the array as: , where is the random 1d array.
Vectorized
Operations
 A universal function (or ufunc for short) is a vectorized wrapper for a function
that takes a fixed number of specific inputs and produces a fixed number of
specific outputs.
 For example, see the following cell:
Ufuncs
 Vectorization involving multiple arrays can be done. Vectorization follows
broadcasting rules. The key broadcasting rule is: In order to broadcast, the size of
the trailing axes for both arrays in an
 operation must either be the same size or one of them must be one. In rough
sense, what it means is:
 1. If two arrays of same dimensions have the same shape, then corresponding
elements can be broadcasted.
 2. If the arrays do not have the same dimensions, prepend the shape of the lower
dimension array with 1s until both arrays have the same dimensions.
 3. In any dimension where one array had size 1 and the other array had size
greater than 1, the first array behaves as if it were copied along that dimension.
 For example, see the following cells:
Broadcasting
 Masked array is an array of booleans that determines for each element of the
associated array whether the value is valid or not. The masked arrays are
obtained using different conditions. Numpy array
 comparison are vectorized too.
 For example, see the following cells:
Masking

More Related Content

Similar to ‏‏Lecture 2.pdf

CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.pptkdr52121
 
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptxChapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptxSovannDoeur
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programmingTaseerRao
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoreambikavenkatesh2
 
lec08-numpy.pptx
lec08-numpy.pptxlec08-numpy.pptx
lec08-numpy.pptxlekha572836
 
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdfLoops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdfinfo309708
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfSmrati Kumar Katiyar
 
NumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer ApplicationNumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer Applicationsharmavishal49202
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfinfo309708
 
UNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptxUNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptxsangeeta borde
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdfMemeMiner
 

Similar to ‏‏Lecture 2.pdf (20)

CAP776Numpy (2).ppt
CAP776Numpy (2).pptCAP776Numpy (2).ppt
CAP776Numpy (2).ppt
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.ppt
 
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptxChapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
 
611+tutorial
611+tutorial611+tutorial
611+tutorial
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
lec08-numpy.pptx
lec08-numpy.pptxlec08-numpy.pptx
lec08-numpy.pptx
 
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdfLoops and ArraysObjectivesArrays are a series of elements consi.pdf
Loops and ArraysObjectivesArrays are a series of elements consi.pdf
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdf
 
NumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer ApplicationNumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer Application
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Arrays
ArraysArrays
Arrays
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
UNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptxUNIT-5_Array in c_part1.pptx
UNIT-5_Array in c_part1.pptx
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 

Recently uploaded

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

‏‏Lecture 2.pdf

  • 1. CSE 202 Introduction to Data Science 1. NumPy Library Dr. Ahmed Ali
  • 2.  Creating NumPy Arrays  NumPy Array Attributes  Indexing  Slicing  Sorting  Copy of Array  Reshaping  Concatenate/Split  Vectorized Operations  Ufuncs  Broadcasting  Masking Lecture Outline
  • 3.  Is one of the two most commonly used libraries for scientific work. The other library is scipy which is a little bit advanced.  The other libraries we are going to use in this course are depend on NumPy.  How to get NumPy? It is very easy, just import in Anaconda. We don’t have to install anything. (pip install NumPy)  What are the difference between list and array in python? List is used in python Array is used in NumPy.  One of the advantages of NumPy is that the built-in methods are easy to use. And they are very fast and efficient in execution because they are encoded in Fortran an C languages.  NumPy has so many built-in constants  Use help command to get more information about any command or method in NumPy. NumPy Library
  • 4. Creating NumPy Arrays Do the following tasks: 1. Create an integer array of 10 elements filled with all zeros. 2. Create a 3x5 floating-point array filled with ones. 3. Create a 3x5 array filled with pi values. 4. Create a 3x3 array of uniformly distributed random values between 0 and 1. 5. Create a 3x3 array of random integers in the interval [0, 10). 6. Create a 3x3 identity matrix. 7. Create an array of 10 elements uniformly dividing the interval [0,1], i.e, 0, 0.1,..., 0.9, 1.
  • 5. Numpy array have the following attributes: 1.ndim (the number of dimensions), 2.shape (the size of each dimension), and 3.size (the total size of the array). For example, do the following: 1.Create a random one-dimensional array (x1), and check all the above attributes. 2.Create a random two-dimensional array (x2), and check all the above attributes. NumPy Array Attributes
  • 6. NumPy array indexing for 1d array is same as Python's lists. However, NumPy nd-array indexing is slightly different from Python's nd- lists. For example: 1.2d-list elements are accessed as:[index1][index2] 2.2d-array elements are accessed as:[index1,index2] where index1 and index2 can be indices or slices. See the following examples: Indexing
  • 7. Slicing refer to selecting/extracting a sub-array from a given array. As an example for 1d arrays, do the following tasks: 1.Create a random 1d NumPy array of size 10. 2.Display the first five elements. 3.Display elements from index 5 to end. 4.Display elements from index 4 to 6. 5.Display alternate elements from the first element. 6.Display alternate elements starting from the second element. 7.Display elements starting from fourth element from the end and ending at last but one element. 8.Display all element in the reverse order of indices. Slicing
  • 8. Slicing As an example for 2d arrays, do the following tasks: 1.Create a random 2d NumPy array of size 3x4. 2.Display first two rows, first three columns 3.Display all rows, and every other column 4.Display all rows and columns in the reverse order 5.Display first column 6.Display first row 7.Display third row elements in order of column 3, then column 1 then column 2 8.Display second and following row elements in order of column 3, then column 1 then column 2
  • 9. Sorting NumPy .sort() method sorts an array in-place. The sorting is done row/column wise, and order/preference can be provided too. As an example, do the following: 1. Create a 1d random array of size 6, and sort it. Print the corresponding original indices in the sorted array. 2. Create a random 2d NumPy array of size 3x4. Sort row wise. 3. Create a random 2d NumPy array of size 3x4. Sort column wise.
  • 10. Copy of Array Arrays are mutable too. Thus, the assignment "=" is by reference. Use ".copy()" to copy the entire NumPy array. For example, see the code in the following cells:
  • 11. Reshaping Numpy .reshape method gives a new shape to an existing array without changing its data. As an exmaple, do the following task: 1.Create a 1d random array of size 10. 2.Reshape the 1d array into size 5x2. 3.Reshape the 1d array into size 2x5.
  • 12.  Concatenate means joining, and Numpy's .concatenate() function is used to join two or more arrays of the same shape along a specified axis. Numpy's .vstack() (.hstack()) can be used for row wise  (column wise) joining. Numpy's split/vsplit/hsplit methods are for breaking/splitting the array, opposite to the concatenation.  For example, see the code in the following cells: Concatenate /Split
  • 13.  Vectorized operations are perhaps the most crucial factor to the wide usage of Numpy library. Vectorized operations simply put are those operations that can be done on arrays without using loops.  For example, do the following: 1. 1. Create a random id array, and add, subtract,multiply and divide all the elements with 5. 2. 2. Take the square of all elements. 3. 3. Find remainder w.r.t 2 for all elements. 4. 4. Transform the array as: , where is the random 1d array. Vectorized Operations
  • 14.  A universal function (or ufunc for short) is a vectorized wrapper for a function that takes a fixed number of specific inputs and produces a fixed number of specific outputs.  For example, see the following cell: Ufuncs
  • 15.  Vectorization involving multiple arrays can be done. Vectorization follows broadcasting rules. The key broadcasting rule is: In order to broadcast, the size of the trailing axes for both arrays in an  operation must either be the same size or one of them must be one. In rough sense, what it means is:  1. If two arrays of same dimensions have the same shape, then corresponding elements can be broadcasted.  2. If the arrays do not have the same dimensions, prepend the shape of the lower dimension array with 1s until both arrays have the same dimensions.  3. In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension.  For example, see the following cells: Broadcasting
  • 16.  Masked array is an array of booleans that determines for each element of the associated array whether the value is valid or not. The masked arrays are obtained using different conditions. Numpy array  comparison are vectorized too.  For example, see the following cells: Masking