SlideShare a Scribd company logo
1 of 41
Numpy Library
1
Learning Objective
In this lecture, we will learn what is
NumPy package and which operations
we can perform on numpy arrays.
And what are arrays.
2
3
Why do we need Array? (Why)
Arrays are used when there is a need to use many variables of
the same type. It can be defined as a sequence of objects
which are of the same data type.
For example, to make an array of prices for daily grocery items.
You create an array and add all numbers (prices) in this array.
4
Why do we need array in Python?
Array is used to store multiple data of the same
type. For example, an array of heigths of all
students.
All data will be numerical in this.
5
6
What is an array in Python?
Array is a sequence of same data type elements. The concept is
to keep items of the same data type together.
Important terms that you can understand the concept of an
array from:
Element: Each item stored in an array
Index: A numerical index is assigned to each location of an
element in an array and is used to identify the element.
7
Representation of array in Python?
As you can see the representation of an array,
1. Index starts with 0
2. The length of the array is 10 that can store 10 elements.
8
Creating an Array in Python?
In Python, an array can be created by the “array” module or by
using NumPy package.
When using the array module to create arrays, all the array’s
elements must be of the same numeric type. But NumPy allows
different datatypes within an array.
You’ll first have to import numpy package before using it in your
program. (import numpy as np)
9
Creating an Array in Python?
Arrays can be created using existing data i.e., from lists.
For this purpose, we use array() method provided in numpy.
10
Creating an Array from existing
data?
Numpy provides different methods for creating an array from
different types of existing data.
For example, numpy.asarray() method can be used for creating
array from lists, tuples, list of tuples etc.
11
Creating an Array from existing
data? (Cont.)
Description of different parameters which it takes is as follows:
12
Creating an Array from existing
data? (Cont.)
Code example:
13
Creating an Array from existing
data? (Cont.)
Code example 2 (creating array from tuple):
14
Dimensionality of Arrays in Python?
Arrays can be of multiple dimensions. Let’s look at 2D arrays.
Two-dimensional arrays are basically array within arrays.
Here, the position of a data item is accessed by using two
indices. It is represented as a table of rows and columns of
data items.
15
Dimensionality of Arrays in Python?
Arrays can be of multiple dimensions. Let’s look at 2D arrays.
18
Lists vs Arrays in Python?
• Arrays need to be declared. Lists don't: since they
are built into Python. In previous lectures, you saw that
lists are created by simply enclosing a sequence of
elements into square brackets. Creating an array, on the
other hand, requires a specific function from either
the array module (i.e., array.array()) or NumPy package
(i.e., numpy.array()).
• Arrays can store data very compactly and are more
efficient for storing large amounts of data.
• Arrays are great for numerical operations; lists cannot
directly handle math operations. For example, you can
divide each element of an array by the same number with
just one line of code. If you try the same with a list,
you'll get an error.
19
Attributes of an array?
Numpy’s array class is called ndarray. It is also known by alias
name array.
This class contains some important attributes (or variables) which
can give us information about different characteristics of the
array.
We’ll discuss different attributes of numpy array (ndarray) in next
slides.
20
Attributes of an array? (size)
The ‘size’ attribute gives the total number of elements in the
array.
The total number of elements of the array. This is equal to the
product of the elements of the array’s shape.
21
Attributes of an array? (ndim)
The ‘ndim’ attribute represents the number of dimensions or axes of
the array. The number of dimensions is also referred to as ‘rank’.
For a single dimensional array, it is 1 and for a two-dimensional
array, it is 2.
22
Attributes of an array? (shape)
The ‘shape’ attribute gives the shape of an array. The shape is a
tuple listing the number of elements along each dimension. A
dimension is called an axis.
For a 1D array, shape gives the number of elements in the row.
For a 2D array, it specifies the number of rows and columns in
each row. We can also change the shape using ‘shape’ attribute.
23
Attributes of an array? (dtype)
An attribute describing the data type of the elements in the array.
Recall that NumPy’s ND-arrays are homogeneous: they can only
posses' numbers of a uniform data type.
24
Attributes of an array? (itemsize)
The ‘itemsize’ attribute gives the memory size of the (single) array
element in bytes.
As we know, 1 byte is equal to 8 bits.
25
Attributes of an array? (nbytes)
The ‘nbytes’ attribute gives the total number of bytes occupied by
an array.
The total number of bytes = size of the array * item size of each
element in the array.
26
Attributes of an array? (reshape())
The ‘reshape()’ method is useful to change the shape of an array.
The new array should have the same number of elements as in the
original array.
27
Attributes of an array? (flatten())
The flatten() method is useful to return a copy of the array
collapsed into one dimension.
So if the array is multidimensional, array will be converted to 1D.
28
Arithmetic Operations
• Add two arrays:
• Subtract two arrays:
• Multiply two arrays:
29
Attributes of an array? (zeros)
The zeros() method is useful to return an array of all zeros
Example.
30
Lab Tasks
Solve the Following Programs
1. Write a program to create array from lists.
2. Write a program to create an array from tuple.
3. Write a program to create an array from list of tuples?
4. Create a list and find out the size of the it.
Hint: size= (length of list) * size of items
size of items= sys.getsizeof(4)
1. Create an array with same elements and find the size.
31
Lab Tasks
Solve Following Programs
1. Write a Program that creates array with multiple
dimensions.
2. Write a program to reshape that array.
3. Convert a 3D array into 1D array
4. Multiply two arrays of different dimmensions.
32
Pandas Library
33
What is Pandas
Pandas is a Python library used for working with data sets.
It has functions for analyzing, cleaning, exploring, and
manipulating data.
34
Getting starting with Pandas
35
Series
36
A Pandas Series is like a column in a table.
It is a one-dimensional array holding data of any type.
Labels
37
With the index argument, you can name your own labels.
DataFrames
38
Data sets in Pandas are usually multi-dimensional tables,
called DataFrames.
Series is like a column, a DataFrame is the whole table.
print(myvar.loc[0])
Lab Task
1. Create an array of subjects you are enrolled in.
2. Make the series of the subjects according to the
course code (e.g., Ch-01)
3. Access the subject name through course code
39
Lab Task
1. Create a dataframe of temperatures, mass and heat
energy.
(At least 3 data entries)
40
Self Assessment
1. Read about Plotting in Pandas Library
41

More Related Content

Similar to Learn NumPy package and array operations in Python

Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPyDevashish Kumar
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxAmanRai352102
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.pptkdr52121
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programmingTaseerRao
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
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
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementationLavanyaJ28
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxkalai75
 

Similar to Learn NumPy package and array operations in Python (20)

Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
CAP776Numpy (2).ppt
CAP776Numpy (2).pptCAP776Numpy (2).ppt
CAP776Numpy (2).ppt
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.ppt
 
Array
ArrayArray
Array
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Lesson 11 one dimensional array
Lesson 11 one dimensional arrayLesson 11 one dimensional array
Lesson 11 one dimensional array
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Numpy.pdf
Numpy.pdfNumpy.pdf
Numpy.pdf
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
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
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Numpy
NumpyNumpy
Numpy
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
 
array model.docx
array model.docxarray model.docx
array model.docx
 

More from CruiseCH

Separation process-1 and Mass transfer slides
Separation process-1 and Mass transfer slidesSeparation process-1 and Mass transfer slides
Separation process-1 and Mass transfer slidesCruiseCH
 
5-JI HE DUOSHAO.pptx
5-JI HE DUOSHAO.pptx5-JI HE DUOSHAO.pptx
5-JI HE DUOSHAO.pptxCruiseCH
 
Purpose of Risk Assessment.pptx
Purpose of Risk Assessment.pptxPurpose of Risk Assessment.pptx
Purpose of Risk Assessment.pptxCruiseCH
 
Updated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptxUpdated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptxCruiseCH
 
Week 10.pptx
Week 10.pptxWeek 10.pptx
Week 10.pptxCruiseCH
 
Engr.Jalal Saleem(ENVIRONMENTAL CRISES).pptx
Engr.Jalal Saleem(ENVIRONMENTAL CRISES).pptxEngr.Jalal Saleem(ENVIRONMENTAL CRISES).pptx
Engr.Jalal Saleem(ENVIRONMENTAL CRISES).pptxCruiseCH
 

More from CruiseCH (6)

Separation process-1 and Mass transfer slides
Separation process-1 and Mass transfer slidesSeparation process-1 and Mass transfer slides
Separation process-1 and Mass transfer slides
 
5-JI HE DUOSHAO.pptx
5-JI HE DUOSHAO.pptx5-JI HE DUOSHAO.pptx
5-JI HE DUOSHAO.pptx
 
Purpose of Risk Assessment.pptx
Purpose of Risk Assessment.pptxPurpose of Risk Assessment.pptx
Purpose of Risk Assessment.pptx
 
Updated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptxUpdated Week 06 and 07 Functions In Python.pptx
Updated Week 06 and 07 Functions In Python.pptx
 
Week 10.pptx
Week 10.pptxWeek 10.pptx
Week 10.pptx
 
Engr.Jalal Saleem(ENVIRONMENTAL CRISES).pptx
Engr.Jalal Saleem(ENVIRONMENTAL CRISES).pptxEngr.Jalal Saleem(ENVIRONMENTAL CRISES).pptx
Engr.Jalal Saleem(ENVIRONMENTAL CRISES).pptx
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 

Learn NumPy package and array operations in Python

  • 2. Learning Objective In this lecture, we will learn what is NumPy package and which operations we can perform on numpy arrays. And what are arrays. 2
  • 3. 3
  • 4. Why do we need Array? (Why) Arrays are used when there is a need to use many variables of the same type. It can be defined as a sequence of objects which are of the same data type. For example, to make an array of prices for daily grocery items. You create an array and add all numbers (prices) in this array. 4
  • 5. Why do we need array in Python? Array is used to store multiple data of the same type. For example, an array of heigths of all students. All data will be numerical in this. 5
  • 6. 6
  • 7. What is an array in Python? Array is a sequence of same data type elements. The concept is to keep items of the same data type together. Important terms that you can understand the concept of an array from: Element: Each item stored in an array Index: A numerical index is assigned to each location of an element in an array and is used to identify the element. 7
  • 8. Representation of array in Python? As you can see the representation of an array, 1. Index starts with 0 2. The length of the array is 10 that can store 10 elements. 8
  • 9. Creating an Array in Python? In Python, an array can be created by the “array” module or by using NumPy package. When using the array module to create arrays, all the array’s elements must be of the same numeric type. But NumPy allows different datatypes within an array. You’ll first have to import numpy package before using it in your program. (import numpy as np) 9
  • 10. Creating an Array in Python? Arrays can be created using existing data i.e., from lists. For this purpose, we use array() method provided in numpy. 10
  • 11. Creating an Array from existing data? Numpy provides different methods for creating an array from different types of existing data. For example, numpy.asarray() method can be used for creating array from lists, tuples, list of tuples etc. 11
  • 12. Creating an Array from existing data? (Cont.) Description of different parameters which it takes is as follows: 12
  • 13. Creating an Array from existing data? (Cont.) Code example: 13
  • 14. Creating an Array from existing data? (Cont.) Code example 2 (creating array from tuple): 14
  • 15. Dimensionality of Arrays in Python? Arrays can be of multiple dimensions. Let’s look at 2D arrays. Two-dimensional arrays are basically array within arrays. Here, the position of a data item is accessed by using two indices. It is represented as a table of rows and columns of data items. 15
  • 16.
  • 17.
  • 18. Dimensionality of Arrays in Python? Arrays can be of multiple dimensions. Let’s look at 2D arrays. 18
  • 19. Lists vs Arrays in Python? • Arrays need to be declared. Lists don't: since they are built into Python. In previous lectures, you saw that lists are created by simply enclosing a sequence of elements into square brackets. Creating an array, on the other hand, requires a specific function from either the array module (i.e., array.array()) or NumPy package (i.e., numpy.array()). • Arrays can store data very compactly and are more efficient for storing large amounts of data. • Arrays are great for numerical operations; lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code. If you try the same with a list, you'll get an error. 19
  • 20. Attributes of an array? Numpy’s array class is called ndarray. It is also known by alias name array. This class contains some important attributes (or variables) which can give us information about different characteristics of the array. We’ll discuss different attributes of numpy array (ndarray) in next slides. 20
  • 21. Attributes of an array? (size) The ‘size’ attribute gives the total number of elements in the array. The total number of elements of the array. This is equal to the product of the elements of the array’s shape. 21
  • 22. Attributes of an array? (ndim) The ‘ndim’ attribute represents the number of dimensions or axes of the array. The number of dimensions is also referred to as ‘rank’. For a single dimensional array, it is 1 and for a two-dimensional array, it is 2. 22
  • 23. Attributes of an array? (shape) The ‘shape’ attribute gives the shape of an array. The shape is a tuple listing the number of elements along each dimension. A dimension is called an axis. For a 1D array, shape gives the number of elements in the row. For a 2D array, it specifies the number of rows and columns in each row. We can also change the shape using ‘shape’ attribute. 23
  • 24. Attributes of an array? (dtype) An attribute describing the data type of the elements in the array. Recall that NumPy’s ND-arrays are homogeneous: they can only posses' numbers of a uniform data type. 24
  • 25. Attributes of an array? (itemsize) The ‘itemsize’ attribute gives the memory size of the (single) array element in bytes. As we know, 1 byte is equal to 8 bits. 25
  • 26. Attributes of an array? (nbytes) The ‘nbytes’ attribute gives the total number of bytes occupied by an array. The total number of bytes = size of the array * item size of each element in the array. 26
  • 27. Attributes of an array? (reshape()) The ‘reshape()’ method is useful to change the shape of an array. The new array should have the same number of elements as in the original array. 27
  • 28. Attributes of an array? (flatten()) The flatten() method is useful to return a copy of the array collapsed into one dimension. So if the array is multidimensional, array will be converted to 1D. 28
  • 29. Arithmetic Operations • Add two arrays: • Subtract two arrays: • Multiply two arrays: 29
  • 30. Attributes of an array? (zeros) The zeros() method is useful to return an array of all zeros Example. 30
  • 31. Lab Tasks Solve the Following Programs 1. Write a program to create array from lists. 2. Write a program to create an array from tuple. 3. Write a program to create an array from list of tuples? 4. Create a list and find out the size of the it. Hint: size= (length of list) * size of items size of items= sys.getsizeof(4) 1. Create an array with same elements and find the size. 31
  • 32. Lab Tasks Solve Following Programs 1. Write a Program that creates array with multiple dimensions. 2. Write a program to reshape that array. 3. Convert a 3D array into 1D array 4. Multiply two arrays of different dimmensions. 32
  • 34. What is Pandas Pandas is a Python library used for working with data sets. It has functions for analyzing, cleaning, exploring, and manipulating data. 34
  • 36. Series 36 A Pandas Series is like a column in a table. It is a one-dimensional array holding data of any type.
  • 37. Labels 37 With the index argument, you can name your own labels.
  • 38. DataFrames 38 Data sets in Pandas are usually multi-dimensional tables, called DataFrames. Series is like a column, a DataFrame is the whole table. print(myvar.loc[0])
  • 39. Lab Task 1. Create an array of subjects you are enrolled in. 2. Make the series of the subjects according to the course code (e.g., Ch-01) 3. Access the subject name through course code 39
  • 40. Lab Task 1. Create a dataframe of temperatures, mass and heat energy. (At least 3 data entries) 40
  • 41. Self Assessment 1. Read about Plotting in Pandas Library 41