0% found this document useful (0 votes)
5 views24 pages

Numpy

NumPy is a fundamental package for high-performance numerical computing in Python, providing efficient storage and operations on dense data buffers. It includes features like multidimensional arrays (ndarray), mathematical functions for array operations, and tools for reading/writing data. The document outlines various functions for creating arrays, performing numerical operations, and saving/loading data, emphasizing the efficiency and capabilities of NumPy for data processing tasks.

Uploaded by

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

Numpy

NumPy is a fundamental package for high-performance numerical computing in Python, providing efficient storage and operations on dense data buffers. It includes features like multidimensional arrays (ndarray), mathematical functions for array operations, and tools for reading/writing data. The document outlines various functions for creating arrays, performing numerical operations, and saving/loading data, emphasizing the efficiency and capabilities of NumPy for data processing tasks.

Uploaded by

HANNAH ADAM
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INTRODUCTION

TO
NUMPY
What is NumPy?
• NumPy (short for Numerical Python) provides an efficient interface to
store and operate on dense data buffers.

• NumPy is the fundamental package supported for presenting and


computing data with high performance in Python.

• In some ways, NumPy arrays are like Python’s built-in list type, but
NumPy arrays provide much more efficient storage and data
operations as the arrays grow larger in size.
Here are some of the things you’ll find in
NumPy
• ndarray, an efficient multidimensional array providing fast array-
oriented arithmetic operations and flexible broadcasting capabilities.

• Mathematical functions for fast operations on entire arrays of data


without having to write loops.

• Tools for reading/writing array data to disk and working with


memory-mapped files.
NumPy arrays
• An array can be used to contain values of a data object in an
experiment or simulation step, pixels of an image, or a signal recorded
by a measurement device.
• For example, the latitude of the Eiffel Tower, Paris is 48.858598 and
the longitude is 2.294495. It can be presented in a NumPy array object
as p:
import numpy as np
p = [Link]([48.858598, 2.294495])
p
Output: array([48.858598, 2.294495])
Continue…
• There are two requirements of a NumPy array:
a fixed size at creation and
a uniform, fixed data type, with a fixed size in memory
[Link] # getting dimension of array p
1
[Link] # getting size of each array dimension
(2,)
len(p) # getting dimension length of array p
2
[Link] # getting data type of array p
dtype('float64')
Array creation
• There are various functions provided to create an array object.

• They are very useful for us to create and store data in a


multidimensional array in different situations.

• The easiest way to create an array is to use the array function.

• This accepts any sequence-like object (including other arrays) and


produces a new NumPy array containing the passed data.
Function (empty, empty_like)
Description

• Create a new array of the given shape and type, without initializing
elements.
[Link]([3,2], dtype=np.float64)
array([[0., 0.], [0., 0.], [0., 0.]])
a = [Link]([[1, 2], [4, 3]])
np.empty_like(a)
array([[0, 0], [0, 0]])
Function (ones, ones_like)
Description

• Create a new array with the given shape and type, filled with 1s for all
elements.
[Link](5)
array([1., 1., 1., 1., 1.])
x = [Link]([[0,1,2], [3,4,5]])
np.ones_like(x)
array([[1, 1, 1],[1, 1, 1]])
Function (arrange)
Description

• Create an array with even spaced values in a given interval.

[Link](2, 5)
array([2, 3, 4])
[Link](4, 12, 5)
array([4, 9])
Function (fromstring)
Description
• Create 1-D array from a string or text

[Link]('3.14 2.17', dtype=[Link], sep=' ')


array([3.14, 2.17])
Numerical operations on arrays
• After getting familiar with creating and accessing ndarrays, the next
step, applying some mathematical operations to array data without
writing any for loops, of course, with higher performance.

a = [Link](4)
a*2
array([2., 2., 2., 2.])
a+3
array([4., 4., 4., 4.])
Array functions
• Many helpful array functions are supported in NumPy for analyzing
data.
• We will list some part of them that are common in use.

• Firstly, the transposing function is another kind of reshaping form that


returns a view on the original data array without copying anything:
Continue…

a = [Link]([[0, 5, 10], [20, 25, 30]])


[Link](3, 2)
array([[0, 5], [10, 20], [25, 30]])
a.T
array([[0, 20], [5, 25], [10, 30]])
Functions (sin, cos, tan)
• Description: Trigonometric functions
Example

a = [Link]([0.,30., 45.])
[Link](a * [Link] / 180)
array([0., 0.5, 0.7071678])
(around, round, rint, fix, floor, ceil, trunc)
Description:
• Rounding elements of an array to the given or nearest number

a = [Link]([0.34, 1.65])
[Link](a)
array([0., 2.])
(greater, greater_equal, less, less_equal,
equal, not_equal)
• Description:
• Perform elementwise comparison: >, >=, <, <=, ==, !=.
X1 = [0,1,4]
X2= [0,4,10]
[Link](x1, x2)
array([[False, False, False],
[True, True, True]], dtype = bool)
Data processing using arrays
• With the NumPy package, one can easily solve many kinds of data
processing tasks without writing complex loops.

• It is very helpful for us to control our code as well as the performance


of the program.

• In this part, we want to introduce some mathematical and statistical


functions.
Function (sum)
Description
• Calculate the sum of all the elements in an array or along the axis.

Example

a = [Link]([[2,4], [3,5]])
[Link](a, axis=0)
array([5, 9])
Function (prod)
Description
• Compute the product of array elements over the given axis.

a = [Link]([[2,4], [3,5]])
[Link](a, axis=1)
array([8, 15])
Function (diff)
Description
• Calculate the discrete difference along the given axis.

a = [Link]([[2,4], [3,5]])
[Link](a, axis=0)
array([[1,1]])
Function (std, var)
Description:
• Return standard deviation and variance of arrays.

a = [Link]([[2,4], [3,5]])
[Link](a)
1.1180339
[Link](a)
1.25
Saving an array
• Arrays are saved by default in an uncompressed raw binary format,
with the file extension .npy by the [Link] function:

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


[Link]('[Link]', a)
Continue…
• If we want to store several arrays into a single file in an uncompressed
.npz format, we can use the [Link] function, as shown in the
following example.

a = [Link](4)
b = [Link](7)
[Link]('[Link]', arr0=a, arr1=b)
Loading an array
• You have two common functions such as [Link] and [Link],
which correspond to the saving functions, for loading an array:

[Link]('[Link]')
array([[0, 1, 2], [3, 4, 5]])
[Link]('[Link]', delimiter=',')
array([0., 1., 2., 3.])

You might also like