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

numpy

NumPy is a Python library designed for efficient array manipulation, providing performance improvements over traditional Python lists. It supports operations in linear algebra, Fourier transforms, and matrices, and is widely used in data science. NumPy arrays, known as ndarrays, are stored in contiguous memory locations, making them faster and more efficient for processing.

Uploaded by

vasantha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

numpy

NumPy is a Python library designed for efficient array manipulation, providing performance improvements over traditional Python lists. It supports operations in linear algebra, Fourier transforms, and matrices, and is widely used in data science. NumPy arrays, known as ndarrays, are stored in contiguous memory locations, making them faster and more efficient for processing.

Uploaded by

vasantha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

NumPy

• NumPy is a Python library.


• NumPy is used for working with arrays.
• NumPy is short for "Numerical Python".
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 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, it provides a lot of supporting
functions that make working very easy.
• Arrays are very frequently used in data
science, where speed and resources are
very important.
Why is NumPy Faster Than Lists?

• NumPy arrays are stored at one continuous


place in memory unlike lists, so processes
can access and manipulate them very
efficiently.
• This behavior is called locality of reference
in computer science.
• This is the main reason why NumPy is
faster than lists.
• Also it is optimized to work with latest CPU
architectures.
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:
C:\Users\Your Name>pip install numpy
• If this command fails, then use a
python distribution that already has
NumPy installed like, Anaconda,
Spyder etc.
characteristics of ndarrays

• Dimensionality of the ndarray


• Size of the ndarray
• Shape of the ndarray
• Number of rows in the ndarray
• Type of data stored in the ndarray
Import NumPy

Once NumPy is installed, import it in your


applications by adding the import keyword:
import numpy

Example
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
NumPy as np

NumPy is usually imported under the np


alias.
import numpy as np
Now the NumPy package can be referred
to as np instead of numpy.

Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
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
.
Sample program on array
Dimensions in Arrays

A dimension in arrays is one level of


array depth (nested 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.
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.
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.
3-D arrays
An array that has 2-D arrays (matrices) as its
elements is called 3-D array.
These are often used to represent a 3rd order
tensor.
Check Number of Dimensions?

NumPy Arrays provides the ndim attribute that


returns an integer that tells us how many dimensions
the array have.
Higher Dimensional Arrays

An array can have any number of dimensions.


When the array is created, you can define the
number of dimensions by using the ndmin
argument.
Creating ndarrays
array = np.array([[0,1,2],[2,3,4]]) array = np.eye(3)
[[0 1 2] [[1. 0. 0.]
[2 3 4]] [0. 1. 0.]
[0. 0. 1.]]
array = np.zeros((2,3))
array = np.arange(0, 10, 2)
[[0. 0. 0.]
[0, 2, 4, 6, 8]
[0. 0. 0.]]
array = np.random.randint(0, 10,
array = np.ones((2,3)) (3,3))
[[1. 1. 1.] [[6 4 3]
[1. 1. 1.]] [1 5 6]
[9 8 5]]
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.
• To access elements from 2-D arrays we can use comma
separated integers representing the dimension and the index of
the element.
• Think of 2-D arrays like a table with rows and columns, where
the dimension represents the row and the index represents the
column.
To access elements from 3-D arrays we can use
comma separated integers representing the
dimensions and the index of the element.
Example Explained
arr[0, 1, 2] prints the value 6.
And this is why:
The first number represents the first dimension, which contains two arrays:
[[1, 2, 3], [4, 5, 6]]
and:
[[7, 8, 9], [10, 11, 12]]
Since we selected 0, we are left with the first array:
[[1, 2, 3], [4, 5, 6]]
The second number represents the second dimension, which also contains two
arrays:
[1, 2, 3]
and:
[4, 5, 6]
Since we selected 1, we are left with the second array:
[4, 5, 6]
The third number represents the third dimension, which contains three values:
4
5
6
Since we selected 2, we end up with the third value:
6
Use negative indexing to access an array from
the end.
Data Tyes in python
Slicing arrays

• Slicing arrays is almost the


same as slicing lists, except you
can specify multiple
dimensions.

You might also like