SlideShare a Scribd company logo
1 of 29
Chapter 4
NumPy Basics Arrays
and Vectorized Computation
Part I
Outline
1
4.1 The NumPy ndarray
A Multidimensional Array Object
4.2 Universal Functions
Fast Element-Wise Array Functions
4.3 Array-Oriented Programming with Arrays
4.4 File Input and Output with Arrays
4.5 Linear Algebra
4.6 Pseudorandom Number Generation
4.7 Example
Random Walks
4 NumPy Basics
1
 NumPy, short for Numerical Python, is one of the most important
foundational packages for numerical computing in Python.
 Here are some of the things you’ll find in NumPy:
• ndarray, a multidimensional array for fast array-oriented arithmetic
operations and flexible broadcasting capabilities.
• Mathematical functions for fast operations on entire arrays of data
without having to write loops.
• Tools for reading/writing array data to disk and working with memory-
mapped files.
• Linear algebra, random number generation, and Fourier transform
capabilities
• A C API for connecting NumPy with libraries written in C, C++, or
FORTRAN.
1
 NumPy is so important for numerical computations in Python is
because it is designed for efficiency on large arrays of data.
 There are a number of reasons for this:
• NumPy internally stores data in a contiguous block of memory,
independent of other built-in Python objects.
• NumPy operations perform complex computations on entire arrays
without the need for Python for loops.
• NumPy-based algorithms are generally 10 to 100 times faster (or more)
than their pure Python counterparts and use significantly less memory.
4 NumPy Basics (cont.)
1
 consider a NumPy array of one million integers, and the
equivalent Python list:
Now let’s multiply each sequence by 2:
4 NumPy Basics (cont.)
4.1 The NumPy ndarray
1
 N-dimensional array object, or ndarray is a fast, flexible container
for large datasets in Python.
 Arrays enable you to perform mathematical operations on whole
blocks of data using similar syntax to the equivalent operations
between scalar elements.
 To give you example:
1
We then write mathematical operations with data:
 In the first example, all of the elements have been multiplied by 10.
 In the second, the corresponding values in each “cell” in the array
have been added to each other.
4.1 The NumPy ndarray (cont.)
1
 An ndarray is a generic multidimensional container for homogeneous
data; that is, all of the elements must be the same type.
 Every array has a shape, a tuple indicating the size of each dimension,
and a dtype, an object describing the data type of the array:
4.1 The NumPy ndarray (cont.)
1
Creating ndarrays
 The easiest way to create an array is to use the array function.
 Multi-dimensional array
4.1 The NumPy ndarray (cont.)
1
 Since data2 was a list of lists, the NumPy array arr2 has two
dimensions with shape inferred from the data. We can confirm
this by inspecting the ndim and shape attributes:
4.1 The NumPy ndarray (cont.)
Creating ndarrays
1
 There are a number of other functions for creating new arrays.
 As examples, zeros and ones create arrays of 0s or 1s, respectively.
 empty creates an array without initializing its values to any particular value.
4.1 The NumPy ndarray (cont.)
1
4.1 The NumPy ndarray (cont.)
1
Data Types for ndarrays
The data type or dtype is a special object containing the information the
ndarray needs to interpret a chunk of memory as a particular type of data:
4.1 The NumPy ndarray (cont.)
1
4.1 The NumPy ndarray (cont.)
1
You can explicitly convert or cast an array from one dtype to another using
ndarray’s astype method:
If you have an array of strings representing numbers, you can use astype to
convert them to numeric form:
4.1 The NumPy ndarray (cont.)
1
Arithmetic with NumPy array
4.1 The NumPy ndarray (cont.)
 Arrays are important because they enable you to express batch operations
on data without writing any for loops.
1
4.1 The NumPy ndarray (cont.)
Basic Indexing and Slicing
NumPy array indexing is a rich topic, as there are many ways you may want to
select a subset of your data or individual elements.
1
4.1 The NumPy ndarray (cont.)
 With higher dimensional arrays, you have many more options.
Basic Indexing and Slicing
 multidimensional arrays
1
4.1 The NumPy ndarray (cont.)
Basic Indexing and Slicing
1
4.1 The NumPy ndarray (cont.)
Boolean Indexing
use here the randn function in numpy.random to generate some random
normally distributed data:
1
4.1 The NumPy ndarray (cont.)
Boolean Indexing
1
4.1 The NumPy ndarray (cont.)
Fancy Indexing
Fancy indexing is a term adopted by NumPy to describe indexing using
integer arrays. Suppose we had an 8 × 4 array:
1
4.1 The NumPy ndarray (cont.)
Transposing Array and Swapping Axes
Transposing is a special form of reshaping that similarly returns a view on
the underlying data without copying anything.
 Arrays have the transpose method and also the special T attribute
 computing the inner matrix product using np.dot
1
4.2 Universal Functions
 A universal function, or ufunc, is a function that performs element-wise
operations on data in ndarrays.
 Many ufuncs are simple element-wise transformations, like sqrt or exp
1
4.2 Universal Functions (cont.)
 Others, such as add or maximum, take two arrays (thus, binary ufuncs) and
return a single array as the result:
 Here, numpy.maximum computed the element-wise maximum of the
elements in x and y.
1
4.3 Universal Functions (cont.)
While not common, a ufunc can return multiple arrays. modf is one example,
a vectorized version of the built-in Python divmod; it returns the fractional
and integral parts of a floating-point array:
1
4.2 Universal Functions (cont.)
1
4.2 Universal Functions (cont.)
1
END PART I

More Related Content

What's hot

What's hot (20)

AVL Tree
AVL TreeAVL Tree
AVL Tree
 
NUMPY
NUMPY NUMPY
NUMPY
 
All-Reduce and Prefix-Sum Operations
All-Reduce and Prefix-Sum Operations All-Reduce and Prefix-Sum Operations
All-Reduce and Prefix-Sum Operations
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
Sorting
SortingSorting
Sorting
 
Python ppt
Python pptPython ppt
Python ppt
 
Code generator
Code generatorCode generator
Code generator
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMS
 
L attribute in compiler design
L  attribute in compiler designL  attribute in compiler design
L attribute in compiler design
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Hashing
HashingHashing
Hashing
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 

Similar to Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx

Similar to Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx (20)

Data Analysis in Python-NumPy
Data Analysis in Python-NumPyData Analysis in Python-NumPy
Data Analysis in Python-NumPy
 
NumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer ApplicationNumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer Application
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptx
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Introduction to numpy.pptx
Introduction to numpy.pptxIntroduction to numpy.pptx
Introduction to numpy.pptx
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
NumPy
NumPyNumPy
NumPy
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
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
 
‏‏Lecture 2.pdf
‏‏Lecture 2.pdf‏‏Lecture 2.pdf
‏‏Lecture 2.pdf
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
CAP776Numpy (2).ppt
CAP776Numpy (2).pptCAP776Numpy (2).ppt
CAP776Numpy (2).ppt
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.ppt
 
Kaggle tokyo 2018
Kaggle tokyo 2018Kaggle tokyo 2018
Kaggle tokyo 2018
 
Python
PythonPython
Python
 
numpy.pdf
numpy.pdfnumpy.pdf
numpy.pdf
 
Python for ML.pptx
Python for ML.pptxPython for ML.pptx
Python for ML.pptx
 
getting started with numpy and pandas.pptx
getting started with numpy and pandas.pptxgetting started with numpy and pandas.pptx
getting started with numpy and pandas.pptx
 
Numpy
NumpyNumpy
Numpy
 

Recently uploaded

Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx

  • 1. Chapter 4 NumPy Basics Arrays and Vectorized Computation Part I
  • 2. Outline 1 4.1 The NumPy ndarray A Multidimensional Array Object 4.2 Universal Functions Fast Element-Wise Array Functions 4.3 Array-Oriented Programming with Arrays 4.4 File Input and Output with Arrays 4.5 Linear Algebra 4.6 Pseudorandom Number Generation 4.7 Example Random Walks
  • 3. 4 NumPy Basics 1  NumPy, short for Numerical Python, is one of the most important foundational packages for numerical computing in Python.  Here are some of the things you’ll find in NumPy: • ndarray, a multidimensional array for fast array-oriented arithmetic operations and flexible broadcasting capabilities. • Mathematical functions for fast operations on entire arrays of data without having to write loops. • Tools for reading/writing array data to disk and working with memory- mapped files. • Linear algebra, random number generation, and Fourier transform capabilities • A C API for connecting NumPy with libraries written in C, C++, or FORTRAN.
  • 4. 1  NumPy is so important for numerical computations in Python is because it is designed for efficiency on large arrays of data.  There are a number of reasons for this: • NumPy internally stores data in a contiguous block of memory, independent of other built-in Python objects. • NumPy operations perform complex computations on entire arrays without the need for Python for loops. • NumPy-based algorithms are generally 10 to 100 times faster (or more) than their pure Python counterparts and use significantly less memory. 4 NumPy Basics (cont.)
  • 5. 1  consider a NumPy array of one million integers, and the equivalent Python list: Now let’s multiply each sequence by 2: 4 NumPy Basics (cont.)
  • 6. 4.1 The NumPy ndarray 1  N-dimensional array object, or ndarray is a fast, flexible container for large datasets in Python.  Arrays enable you to perform mathematical operations on whole blocks of data using similar syntax to the equivalent operations between scalar elements.  To give you example:
  • 7. 1 We then write mathematical operations with data:  In the first example, all of the elements have been multiplied by 10.  In the second, the corresponding values in each “cell” in the array have been added to each other. 4.1 The NumPy ndarray (cont.)
  • 8. 1  An ndarray is a generic multidimensional container for homogeneous data; that is, all of the elements must be the same type.  Every array has a shape, a tuple indicating the size of each dimension, and a dtype, an object describing the data type of the array: 4.1 The NumPy ndarray (cont.)
  • 9. 1 Creating ndarrays  The easiest way to create an array is to use the array function.  Multi-dimensional array 4.1 The NumPy ndarray (cont.)
  • 10. 1  Since data2 was a list of lists, the NumPy array arr2 has two dimensions with shape inferred from the data. We can confirm this by inspecting the ndim and shape attributes: 4.1 The NumPy ndarray (cont.) Creating ndarrays
  • 11. 1  There are a number of other functions for creating new arrays.  As examples, zeros and ones create arrays of 0s or 1s, respectively.  empty creates an array without initializing its values to any particular value. 4.1 The NumPy ndarray (cont.)
  • 12. 1 4.1 The NumPy ndarray (cont.)
  • 13. 1 Data Types for ndarrays The data type or dtype is a special object containing the information the ndarray needs to interpret a chunk of memory as a particular type of data: 4.1 The NumPy ndarray (cont.)
  • 14. 1 4.1 The NumPy ndarray (cont.)
  • 15. 1 You can explicitly convert or cast an array from one dtype to another using ndarray’s astype method: If you have an array of strings representing numbers, you can use astype to convert them to numeric form: 4.1 The NumPy ndarray (cont.)
  • 16. 1 Arithmetic with NumPy array 4.1 The NumPy ndarray (cont.)  Arrays are important because they enable you to express batch operations on data without writing any for loops.
  • 17. 1 4.1 The NumPy ndarray (cont.) Basic Indexing and Slicing NumPy array indexing is a rich topic, as there are many ways you may want to select a subset of your data or individual elements.
  • 18. 1 4.1 The NumPy ndarray (cont.)  With higher dimensional arrays, you have many more options. Basic Indexing and Slicing  multidimensional arrays
  • 19. 1 4.1 The NumPy ndarray (cont.) Basic Indexing and Slicing
  • 20. 1 4.1 The NumPy ndarray (cont.) Boolean Indexing use here the randn function in numpy.random to generate some random normally distributed data:
  • 21. 1 4.1 The NumPy ndarray (cont.) Boolean Indexing
  • 22. 1 4.1 The NumPy ndarray (cont.) Fancy Indexing Fancy indexing is a term adopted by NumPy to describe indexing using integer arrays. Suppose we had an 8 × 4 array:
  • 23. 1 4.1 The NumPy ndarray (cont.) Transposing Array and Swapping Axes Transposing is a special form of reshaping that similarly returns a view on the underlying data without copying anything.  Arrays have the transpose method and also the special T attribute  computing the inner matrix product using np.dot
  • 24. 1 4.2 Universal Functions  A universal function, or ufunc, is a function that performs element-wise operations on data in ndarrays.  Many ufuncs are simple element-wise transformations, like sqrt or exp
  • 25. 1 4.2 Universal Functions (cont.)  Others, such as add or maximum, take two arrays (thus, binary ufuncs) and return a single array as the result:  Here, numpy.maximum computed the element-wise maximum of the elements in x and y.
  • 26. 1 4.3 Universal Functions (cont.) While not common, a ufunc can return multiple arrays. modf is one example, a vectorized version of the built-in Python divmod; it returns the fractional and integral parts of a floating-point array: