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

Numpy

NumPy is a fundamental library for numerical and scientific computing in Python, offering efficient array objects that are significantly faster than traditional Python lists. It supports various operations including linear algebra, array creation, and manipulation, with features like multidimensional arrays and random number generation. The document provides examples of creating, accessing, and performing operations on NumPy arrays.

Uploaded by

yiu873209
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)
6 views

Numpy

NumPy is a fundamental library for numerical and scientific computing in Python, offering efficient array objects that are significantly faster than traditional Python lists. It supports various operations including linear algebra, array creation, and manipulation, with features like multidimensional arrays and random number generation. The document provides examples of creating, accessing, and performing operations on NumPy arrays.

Uploaded by

yiu873209
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/ 24

Module 5

Numpy

• Numerical Python (NumPy) is a fundamental library for numerical and scientific


computing in Python
• NumPy has functions for working in domain of linear algebra, fourier transform and
matrices
• NumPy was created in 2005 by Travis Oliphant
• NumPy is an open source project and can be used freely
• In Python lists that serve the purpose of arrays are slow to process
• NumPy provides an array object that is up to 50x faster than traditional Python lists
• The array object in NumPy is called N-dimensional array (ndarray) and it provides a
lot of supporting functions that make working with ndarray very easy
Numpy

• NumPy arrays are stored at one continuous place in memory unlike lists so processes
can access and manipulate them very efficiently
• First NumPy should be imported to a program before it is used
• import numpy as np
• Now the NumPy package is referred to as np instead of numpy
• NumPy arrays have a fixed size at creation unlike Python lists which can grow
dynamically
• The following program create a NumPy ndarray object by using the array() function
Numpy

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

Output
[1 2 3 4 5]
<class 'numpy.ndarray'>
Two Dimensional Array

import numpy as np
x = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
print(x)
print(x.shape)

Output
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]]
(3, 5)
Three Dimensional Array
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6],[7, 8, 9]], [[10, 11, 12],[13, 14, 15], [16, 17, 18]]])
print(x)
print(arr.shape)
Output
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]]
Accessing Array Elements

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

Output
7
Accessing Array Element in 3D Array

import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2] + arr[1, 1, 1]) @ 6 + 11

Output
17
Negative Indexing

import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', arr[1, -1])

Output
10
Array creation

import numpy as np
>>>np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.zeros((3, 6))
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
empty() creates an array without initializing its values to any particular value and
return garbage values
>>> np.empty((2, 3, 2))
Array creation

array([[[4.45057637e-308, 1.78021527e-306],
[8.45549797e-307, 1.37962049e-306],
[1.11260619e-306, 1.78010255e-306]],
[[9.79054228e-307, 4.45057637e-308],
[8.45596650e-307, 9.34602321e-307],
[4.94065646e-322, 1.11261027e-306]]])
• arange() is an array-valued version of the built-in Python range function

>>> np.arange(15)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
Array creation
import numpy as np
a = np.arange(20).reshape(4, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
>>> a.shape
(4, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
Array creation
>>> a.itemsize
8 @ size in bytes
>>> a.size
20
>>> b = np.array([1.2, 3.5, 5.1])
>>> b.dtype
dtype('float64’)
>>> np.ones((3, 6))
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
Array creation

>>> np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
>>> np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Array operations

import numpy as np
arr1 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
arr2 = np.array([[2, 3, 4], [5, 6, 7], [8, 9, 3]])
>>> arr3 = arr1 + arr2
>>> arr3
array([[23, 25, 27],
[29, 31, 33],
[35, 37, 32]])
>>> arr4 = arr1 - arr2
Array operations

>>> arr4
array([[19, 19, 19],
[19, 19, 19],
[19, 19, 26]])
>>> arr6 = np.dot(arr1, arr2)
>>> arr6
array([[336, 402, 307],
[381, 456, 349],
[426, 510, 391]])
Transpose of a Matrix

import numpy as np
a = np.array([[1, 2, 3, 4],[5, 6, 7, 8], [9, 10, 11, 12]])
at =a.transpose()
print(a)
print(at)
Output
[[ 1 2 3 4] [[ 1 5 9]
[ 5 6 7 8] [ 2 6 10]
[ 9 10 11 12]] [ 3 7 11]
[ 4 8 12]]
Indexing and Slicing
• NumPy array indexing is a way to select a subset of data or individual elements
• One-dimensional arrays are similar to Python lists

arr1 = np.arange(10)
>>> arr1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arr1[5:8]
array([5, 6, 7])
>>> arr1[5:8] = 12
>>> arr1
array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
Indexing and Slicing
arr1 = np.arange(15)
>>> arr1
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> arr1[5:7]
array([5, 6])
>>> arr1[5:6] = 12
>>> arr1
array([ 0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 10, 11, 12, 13, 14])
>>> arr_slice = arr1[5:8]
>>> arr_slice
array([12, 6, 7])
Multidimensional Arrays

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])


>>> arr2d[2]
array([7, 8, 9])
>>> print(arr2d[0][2])
3
arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
>>> arr3d[0]
array([[1, 2, 3],
[4, 5, 6]])
Multidimensional Arrays

>>> old_values = arr3d[0].copy()


>>> old_values
array([[1, 2, 3],
[4, 5, 6]])
>>> arr3d[0] = 42
>>> arr3d
array([[[42, 42, 42],
[42, 42, 42]],
[[ 7, 8, 9],
[10, 11, 12]]])
Multidimensional Arrays

>>> arr3d[0] = old_values


>>> arr3d
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
Random Numbers

from numpy import random


x = random.randint(100, size=(3, 5))
print(x)

Output
[[10 27 62 46 1]
[58 2 5 0 74]
[18 80 53 37 3]]
Random Numbers

from numpy import random


x = random.rand(3, 5)
print(x)

Output
[[0.50393499 0.0055657 0.11829967 0.67372635 0.10152183]
[0.78642396 0.38139114 0.95172678 0.80690577 0.82478002]
[0.46092385 0.98753774 0.39051375 0.33470791 0.68467847]]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy