SlideShare a Scribd company logo
1 of 10
Introduction to numpy
• NumPy (Numerical Python) is an open source Python library that’s
used in almost every field of science and engineering. It’s the
universal standard for working with numerical data in Python, and it’s
at the core of the scientific Python and PyData ecosystems. NumPy
users include everyone from beginning coders to experienced
researchers doing state-of-the-art scientific and industrial research
and development. The NumPy API is used extensively in Pandas,
SciPy, Matplotlib, scikit-learn, scikit-image and most other data
science and scientific Python packages.
Installing NumPy
• To install NumPy, we strongly recommend using a scientific Python distribution. If you’re looking for the full
instructions for installing NumPy on your operating system, see Installing NumPy.
• If you already have Python, you can install NumPy with:
• conda install numpy
• or
• pip install numpy
• If you don’t have Python yet, you might want to consider using Anaconda. It’s the easiest way to get started.
The good thing about getting this distribution is the fact that you don’t need to worry too much about
separately installing NumPy or any of the major packages that you’ll be using for your data analyses, like
pandas, Scikit-Learn, etc.
How to import NumPy
• To access NumPy and its functions import it in your Python code like
this:
• import numpy as np
• We shorten the imported name to np for better readability of code
using NumPy. This is a widely adopted convention that you should
follow so that anyone working with your code can easily understand
it.
Reading the example code
• If you aren’t already comfortable with reading tutorials that contain a lot of code, you might not know how
to interpret a code block that looks like this:
• a = np.arange(6)
• a2 = a[np.newaxis, :]
• a2.shape
• (1, 6)
• If you aren’t familiar with this style, it’s very easy to understand. If you see >>>, you’re looking at input, or
the code that you would enter. Everything that doesn’t have >>> in front of it is output, or the results of
running your code. This is the style you see when you run python on the command line, but if you’re using
IPython, you might see a different style. Note that it is not part of the code and will cause an error if typed or
pasted into the Python shell. It can be safely typed or pasted into the IPython shell; the >>> is ignored.
What’s the difference between a Python list
and a NumPy array?
• NumPy gives you an enormous range of fast and efficient ways of creating
arrays and manipulating numerical data inside them. While a Python list
can contain different data types within a single list, all of the elements in a
NumPy array should be homogeneous. The mathematical operations that
are meant to be performed on arrays would be extremely inefficient if the
arrays weren’t homogeneous.
• Why use NumPy?
• NumPy arrays are faster and more compact than Python lists. An array
consumes less memory and is convenient to use. NumPy uses much less
memory to store data and it provides a mechanism of specifying the data
types. This allows the code to be optimized even further.
What is an array?
• An array is a central data structure of the NumPy library. An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element. It has a grid of elements
that can be indexed in various ways. The elements are all of the same type, referred to as the array dtype.
• An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers. The rank of the array is the number of dimensions. The shape of the array is a tuple of integers giving the size
of the array along each dimension.
• One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional data.
• For example:
• a = np.array([1, 2, 3, 4, 5, 6])
• or:
• a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
• We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array,
you’ll be accessing element “0”.
• print(a[0])
• [1 2 3 4]
More information about arrays
• You might occasionally hear an array referred to as a “ndarray,” which
is shorthand for “N-dimensional array.” An N-dimensional array is
simply an array with any number of dimensions. You might also hear
1-D, or one-dimensional array, 2-D, or two-dimensional array, and so
on. The NumPy ndarray class is used to represent both matrices and
vectors. A vector is an array with a single dimension (there’s no
difference between row and column vectors), while a matrix refers to
an array with two dimensions. For 3-D or higher dimensional arrays,
the term tensor is also commonly used.
How to create a basic array
• This section covers np.array(), np.zeros(), np.ones(), np.empty(),
np.arange(), np.linspace(), dtype
• To create a NumPy array, you can use the function np.array().
• All you need to do to create a simple array is pass a list to it. If you
choose to, you can also specify the type of data in your list. You can
find more information about data types here.
Adding, removing, and sorting elements
• In addition to sort, which returns a sorted copy of an array, you can use:
• argsort, which is an indirect sort along a specified axis,
• lexsort, which is an indirect stable sort on multiple keys,
• searchsorted, which will find elements in a sorted array, and
• partition, which is a partial sort.
• To read more about sorting an array, see: sort.

More Related Content

Similar to Introduction to numpy.pptx

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
SovannDoeur
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptx
PremaGanesh1
 

Similar to Introduction to numpy.pptx (20)

Python
PythonPython
Python
 
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
 
Python for ML.pptx
Python for ML.pptxPython for ML.pptx
Python for ML.pptx
 
Session 2
Session 2Session 2
Session 2
 
Pa2 session 1
Pa2 session 1Pa2 session 1
Pa2 session 1
 
NumPy
NumPyNumPy
NumPy
 
Numpy.pdf
Numpy.pdfNumpy.pdf
Numpy.pdf
 
Data Analyzing And Visualization Using Python.pptx
Data Analyzing And Visualization Using Python.pptxData Analyzing And Visualization Using Python.pptx
Data Analyzing And Visualization Using Python.pptx
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptx
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
 
Scientific Python
Scientific PythonScientific Python
Scientific Python
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptx
 
CDAT - cdms numpy arrays - Introduction
CDAT - cdms numpy arrays - IntroductionCDAT - cdms numpy arrays - Introduction
CDAT - cdms numpy arrays - Introduction
 
numpy.pdf
numpy.pdfnumpy.pdf
numpy.pdf
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
DS LAB MANUAL.pdf
DS LAB MANUAL.pdfDS LAB MANUAL.pdf
DS LAB MANUAL.pdf
 
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
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 

Introduction to numpy.pptx

  • 2. • NumPy (Numerical Python) is an open source Python library that’s used in almost every field of science and engineering. It’s the universal standard for working with numerical data in Python, and it’s at the core of the scientific Python and PyData ecosystems. NumPy users include everyone from beginning coders to experienced researchers doing state-of-the-art scientific and industrial research and development. The NumPy API is used extensively in Pandas, SciPy, Matplotlib, scikit-learn, scikit-image and most other data science and scientific Python packages.
  • 3. Installing NumPy • To install NumPy, we strongly recommend using a scientific Python distribution. If you’re looking for the full instructions for installing NumPy on your operating system, see Installing NumPy. • If you already have Python, you can install NumPy with: • conda install numpy • or • pip install numpy • If you don’t have Python yet, you might want to consider using Anaconda. It’s the easiest way to get started. The good thing about getting this distribution is the fact that you don’t need to worry too much about separately installing NumPy or any of the major packages that you’ll be using for your data analyses, like pandas, Scikit-Learn, etc.
  • 4. How to import NumPy • To access NumPy and its functions import it in your Python code like this: • import numpy as np • We shorten the imported name to np for better readability of code using NumPy. This is a widely adopted convention that you should follow so that anyone working with your code can easily understand it.
  • 5. Reading the example code • If you aren’t already comfortable with reading tutorials that contain a lot of code, you might not know how to interpret a code block that looks like this: • a = np.arange(6) • a2 = a[np.newaxis, :] • a2.shape • (1, 6) • If you aren’t familiar with this style, it’s very easy to understand. If you see >>>, you’re looking at input, or the code that you would enter. Everything that doesn’t have >>> in front of it is output, or the results of running your code. This is the style you see when you run python on the command line, but if you’re using IPython, you might see a different style. Note that it is not part of the code and will cause an error if typed or pasted into the Python shell. It can be safely typed or pasted into the IPython shell; the >>> is ignored.
  • 6. What’s the difference between a Python list and a NumPy array? • NumPy gives you an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them. While a Python list can contain different data types within a single list, all of the elements in a NumPy array should be homogeneous. The mathematical operations that are meant to be performed on arrays would be extremely inefficient if the arrays weren’t homogeneous. • Why use NumPy? • NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.
  • 7. What is an array? • An array is a central data structure of the NumPy library. An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element. It has a grid of elements that can be indexed in various ways. The elements are all of the same type, referred to as the array dtype. • An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers. The rank of the array is the number of dimensions. The shape of the array is a tuple of integers giving the size of the array along each dimension. • One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional data. • For example: • a = np.array([1, 2, 3, 4, 5, 6]) • or: • a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) • We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array, you’ll be accessing element “0”. • print(a[0]) • [1 2 3 4]
  • 8. More information about arrays • You might occasionally hear an array referred to as a “ndarray,” which is shorthand for “N-dimensional array.” An N-dimensional array is simply an array with any number of dimensions. You might also hear 1-D, or one-dimensional array, 2-D, or two-dimensional array, and so on. The NumPy ndarray class is used to represent both matrices and vectors. A vector is an array with a single dimension (there’s no difference between row and column vectors), while a matrix refers to an array with two dimensions. For 3-D or higher dimensional arrays, the term tensor is also commonly used.
  • 9. How to create a basic array • This section covers np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace(), dtype • To create a NumPy array, you can use the function np.array(). • All you need to do to create a simple array is pass a list to it. If you choose to, you can also specify the type of data in your list. You can find more information about data types here.
  • 10. Adding, removing, and sorting elements • In addition to sort, which returns a sorted copy of an array, you can use: • argsort, which is an indirect sort along a specified axis, • lexsort, which is an indirect stable sort on multiple keys, • searchsorted, which will find elements in a sorted array, and • partition, which is a partial sort. • To read more about sorting an array, see: sort.