0% found this document useful (0 votes)
1 views

Numpyinpython1713425437 (1)

NumPy is a powerful Python library for working with arrays, offering significant speed advantages over traditional lists. It provides various methods for creating and manipulating multi-dimensional arrays, including indexing and slicing capabilities. The library is open-source and can be easily installed using pip.

Uploaded by

bhargavthanki609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Numpyinpython1713425437 (1)

NumPy is a powerful Python library for working with arrays, offering significant speed advantages over traditional lists. It provides various methods for creating and manipulating multi-dimensional arrays, including indexing and slicing capabilities. The library is open-source and can be easily installed using pip.

Uploaded by

bhargavthanki609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

NUMPY IN PYTHON

Learning objective: To understand the numpy library and its methods.


WHAT IS NUMPY?

• NumPy is a Python library used for working with arrays.


• It also has functions for working in domain of linear algebra, fourier
transform, and matrices.
• NumPy was created in 2005 by Travis Oliphant. It is an open source
project and you can use it freely.
• NumPy stands for Numerical Python.
WHY USE NUMPY?

• In Python we have lists that serve the purpose of arrays, but they are
slow to process.
• NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
• The array object in NumPy is called ndarray
INSTALLATION OF NUMPY

• If you have Python and PIP already installed on a system, then


installation of NumPy is very easy.
• Install it using this command:

pip install numpy


IMPORT NUMPY

• Once NumPy is installed, import it in your applications by adding the


import keyword

• import numpy
NUMPY AS NP

• NumPy is usually imported under the np alias


• Create an alias with the as keyword while importing
• Now the NumPy package can be referred to as np instead of numpy
NUMPY CREATING ARRAYS

Create a NumPy ndarray Object


• NumPy is used to work with arrays. The array object in NumPy is called
ndarray
• We can create a NumPy ndarray object by using the array() function.
• To create an ndarray, we can pass a list, tuple or any array-like object
into the array() method, and it will be converted into an ndarray:
DIMENSIONS IN ARRAYS

0-D Arrays
• 0-D arrays, or Scalars, are the elements in an array. Each value in an
array is a 0-D array.
DIMENSIONS IN ARRAYS

1-D Arrays
• An array that has 0-D arrays as its elements is called uni-dimensional
or 1-D array.
• These are the most common and basic arrays.
DIMENSIONS IN ARRAYS

2-D Arrays
• An array that has 1-D arrays as its elements is called a 2-D array.
• These are often used to represent matrix or 2nd order tensors.
NUMBER OF DIMENSIONS

• NumPy Arrays provides the ndim attribute that returns an integer that tells us
how many dimensions the array have.
METHODS FOR CREATING NUMPY ARRAY

• NumPy provides several methods for creating arrays with different


shapes, sizes, and initial values.
1. numpy.array(): This method creates an array from a Python list or
tuple. It infers the data type from the input elements unless explicitly
specified.
2. numpy.zeros(): This method creates an array filled with zeros of the
specified shape.
METHODS FOR CREATING NUMPY ARRAY

3. numpy.ones(): Similar to numpy.zeros(), but creates an array filled


with ones.
METHODS FOR CREATING NUMPY ARRAY

4. numpy.empty(): This method creates an array without initializing its


values. The content of the array is not predictable and depends on
the state of the memory.
METHODS FOR CREATING NUMPY ARRAY

5. numpy.full(): This method creates an array filled with a specified


value.
METHODS FOR CREATING NUMPY ARRAY

6. numpy.arange(): This method creates an array with evenly spaced


values within a specified range.
METHODS FOR CREATING NUMPY ARRAY

7. numpy.linspace(): This method creates an array with evenly spaced


values over a specified interval.
NUMPY ARRAY INDEXING

• Array indexing is the same as accessing an array element.


• You can access an array element by referring to its index number.
• The indexes in NumPy arrays start with 0, meaning that the first
element has index 0, and the second has index 1 etc.
NUMPY ARRAY INDEXING
NUMPY ARRAY INDEXING
NUMPY ARRAY INDEXING

Access 2-D Arrays


• To access elements from 2-D arrays we can use comma separated
integers representing the dimension and the index of the element.
NUMPY ARRAY INDEXING

Access 2-D Arrays


• Write the code for accessing the element on the 2nd row, 5th column
NUMPY ARRAY SLICING

• Slicing in python means taking elements from one given index to


another given index.
• We pass slice instead of index like this: [start:end].
• We can also define the step, like this: [start:end:step].
• If we don't pass start its considered 0
• If we don't pass end its considered length of array in that dimension
• If we don't pass step its considered 1
NUMPY ARRAY SLICING

The result includes the start index, but excludes the end index.
NUMPY ARRAY SLICING

Slice elements from index 4 to the end of the array


NUMPY ARRAY SLICING

Slice elements from the beginning to index 4


NUMPY ARRAY SLICING

Slicing 2-D Arrays


NUMPY JOINING ARRAY

• Joining means putting contents of two or more arrays in a single array.


• In SQL we join tables based on a key, whereas in NumPy we join arrays
by axes.
• We pass a sequence of arrays that we want to join to
the concatenate() function, along with the axis. If axis is not explicitly
passed, it is taken as 0.
NUMPY JOINING ARRAY
NUMPY JOINING ARRAY
NUMPY JOINING ARRAY
NUMPY JOINING ARRAY

You might also like