0% found this document useful (0 votes)
2 views25 pages

Numpy

NumPy is a Python library for efficient array manipulation, providing functionalities for linear algebra and data science applications. It features an array object called ndarray, which is significantly faster than traditional Python lists. The document also covers creating, indexing, slicing arrays, and the various data types supported by NumPy.

Uploaded by

divyayadav
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)
2 views25 pages

Numpy

NumPy is a Python library for efficient array manipulation, providing functionalities for linear algebra and data science applications. It features an array object called ndarray, which is significantly faster than traditional Python lists. The document also covers creating, indexing, slicing arrays, and the various data types supported by NumPy.

Uploaded by

divyayadav
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

NumPy

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 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, it provides a lot of
supporting functions that make working with ndarray very easy.
• Arrays are very frequently used in data science, where speed and
resources are very important.
• Data Science: is a branch of computer science where we
study how to store, use and analyze data for deriving
information from it.
import numpy as np

arr = [Link]([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.
import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)

print(type(arr))
• Use a tuple to create a NumPy array:
import numpy as np

arr = [Link]((1, 2, 3, 4, 5))

print(arr)
Creating a 0-D array

import numpy as np

arr = [Link](42)

print(arr)
1-D Arrays

import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)
2-D Arrays

import numpy as np

arr = [Link]([[1, 2, 3], [4, 5, 6]])

print(arr)
Check Number of Dimensions
import numpy as np

a = [Link](42)
b = [Link]([1, 2, 3, 4, 5])
c = [Link]([[1, 2, 3], [4, 5, 6]])
d = [Link]([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print([Link])
print([Link])
print([Link])
print([Link])
NumPy Array Indexing
Access Array Elements
• 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.
import numpy as np

arr = [Link]([1, 2, 3, 4])

print(arr[0])
import numpy as np

arr = [Link]([1, 2, 3, 4])

print(arr[2] + arr[3])
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.
• Think of 2-D arrays like a table with rows and columns, where
the dimension represents the row and the index represents
the column.
• Access the element on the first row, second column:
import numpy as np

arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])

print('2nd element on 1st row: ', arr[0, 1])


import numpy as np

arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])

print('5th element on 2nd row: ', arr[1, 4])


Negative Indexing
• Use negative indexing to access an array from the end.
• Print the last element from the 2nd dim:

import numpy as np

arr = [Link]([[1,2,3,4,5], [6,7,8,9,10]])

print('Last element from 2nd dim: ', arr[1, -1])


NumPy Array Slicing

Slicing arrays
• 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
Slice elements from index 1 to index 5 from the following
array:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])
• The result includes the start index, but excludes the end
index.
Slice elements from index 4 to the end of the array:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[4:])
Slice elements from the beginning to index 4 (not included):
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[:4])
Negative Slicing
Use the minus operator to refer to an index from the end.
Slice from the index 3 from the end to index 1 from the end:
import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])
Return every other element from index 1 to index 5:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5:2])
Return every other element from the entire array:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])

print(arr[::2])
NumPy Data Types
Data Types in Python
• strings - used to represent text data, the text is given under
quote marks. e.g. "ABCD"
• integer - used to represent integer numbers. e.g. -1, -2, -3
• float - used to represent real numbers. e.g. 1.2, 42.42
• boolean - used to represent True or False.
• complex - used to represent complex numbers. e.g. 1.0 +
2.0j, 1.5 + 2.5j
Data Types in NumPy
NumPy has some extra data types, and refer to data types with one
character, like i for integers, u for unsigned integers etc.
Below is a list of all data types in NumPy and the characters used to
represent them.
• i - integer
• b - boolean
• u - unsigned integer
• f - float
• c - complex float
• m - timedelta
• M - datetime
• O - object
• S - string
• U - unicode string
V - fixed chunk of memory for other type ( void )
Checking the Data Type of an Array
• The NumPy array object has a property called dtype that
returns the data type of the array:

Get the data type of an array object:


import numpy as np

arr = [Link]([1, 2, 3, 4])

print([Link])
• Get the data type of an array containing strings:
import numpy as np

arr = [Link](['apple', 'banana', 'cherry'])

print([Link])

You might also like