Understanding Numpy: A Guide to Python's Array Operations, Study notes of Advanced Computer Programming

An overview of Numpy, a Python library for creating and manipulating arrays, including data types, properties, operations, and broadcasting. It also covers the use of Boolean masks and the dot product.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

elmut
elmut 🇺🇸

4.6

(16)

285 documents

1 / 77

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
What is Numpy?
12
Numpy allows users to build multi-
dimensional arrays and high-level
mathematical functions.
Fast numerical computations
Takes up lesser storage than lists
High-level math functions
In [20]: import numpy as np
In [21]: harray = np.array(range(100))
In [22]: type(harray)
Out[22]: numpy.ndarray
In [23]: harray.shape
Out[23]: (100,)
In [24]: harray.size
Out[24]: 100
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d

Partial preview of the text

Download Understanding Numpy: A Guide to Python's Array Operations and more Study notes Advanced Computer Programming in PDF only on Docsity!

What is

Numpy

Numpy

allows users to build multi

dimensional arrays and high

level

mathematical functions.

Fast numerical computations

Takes up lesser storage than lists

High

level math functions

In [

]: import

numpy

as

np

In [

]:

harray

np.array

range

In [

]: type

harray

Out[

]:

numpy.ndarray

In [

]:

harray.shape

Out[

]:

In [

]:

harray.size

Out[

]:

Why do we need

Numpy

MEMORY EFFICIENT

1

In [

]:

harlist

= [

1 , 2 , 3 , 4 , 5 , 6 ]

In [

]:

numpyarray

np.array

harlist,dtype

'int8'

In [

]:

#helper function to

calculate size of python list

size(

harlist

Out[

]:

In [

]:

numpyarray.nbytes

Out[

]:

Densely packed in memory

Data type pre

defined

Optimized for access

Jake

Vanderplas

post on

numpy

and python

Why is

Numpy

so fast?

Data as rows & cols

Data as rows & cols

FAST

2

(implemented in C)Use universal functions

No type conversion

No type checking

In [

]:

%%time

hadamard

= []

...: for

i

in

range

len

(A)):

mini = []

for

j

in

range

len

(A[

])):

mini.append

(A[

i

][j]*B[

i

][j])

hadamard.append

(mini)

CPU times: user 505

ms

, sys: 13.

ms

, total:

ms

Wall time: 517

ms

In [

]:

%time A*B

CPU times: user 2.

ms

, sys: 3.

ms

, total:

ms

Wall time: 5.

ms

Numpy

benefits

16

Universal operations

Intuitive syntax

No loops needed

MATH

LIKE

3

In [

]:

# Using pythonic loops

hadamard

= []

...: for

i

in

range

len

(A)):

mini = []

for

j

in

range

len

(A[

])):

mini.append

(A[

i

][j]*B[

i

][j])

hadamard.append

(mini)

In [

]:

# Using

Numpy

universal functions

hadamard

= A*B

np.array

Getting Started with

Numpy

Creation

Properties

Operations

Shape

size

axis

np.array

zeros,ones

arange,linspace

vstack,hstack

Indexing & Slicing

Reshape

Broadcasting

Properties & Attributes

The

array

is a central

datastructure

of the

Numpy

library.

Scalar

Vector

Matrix

Tensor

List of

elements

rows

cols

rows

cols

width

A^ X E M S E L P

Your age

list of ages of

(^) PyDS

(^) team

A pandas

DataFrame

A picture of the Great Astronomer!

25

Properties & Attributes

The

ndarray

is a central

datastructure

of the

Numpy

library.

Scalar

Vector

Matrix

Tensor

List of

elements

rows

cols

rows

cols

width

Your age

list of ages of

(^) PyDS

(^) team

A pandas

DataFrame

A picture of the Great Astronomer!

S E L P^ M A^ X E

In [

]:

age_list

[
]

In [

]:

np.array

age_list

Out[

]:

27]) array([25, 22, 24,

In [

]:

a =

np.array

[[
],
[ 5 , 6 , 7 , 8
],
[ 1 , 2 , 4 , 3
],
[ 4 , 5 , 6 , 3
]])

In [

]:

a =

np.array

[[[ (
],[
5 , 6 , 7 , 8 ]
,[
],[
]]
,[[
],[
5 , 6 , 7 , 8 ]
,[
],[
]]
[[ ,
],[
],
[ 1 , 2 , 4 , 3
],[
]],
] )

In [

]:

age =

In [

]:

np.array

(age)

Out[

]:

array(34)

Properties & Attributes

Scalar

Vector

Matrix

Tensor

The

rank

of the array is the number of dimensions.

ndarray.ndim

will tell you the number of axes, or dimensions, of the array.

Rank: 0

Rank: 1

Rank: 2

Rank: 3

0D

1D

List of

elements

rows

cols

rows

cols

channel

2D

3D

SHAPE

Properties & Attributes

Shape:^ Vector

(4,) row

[

[

1D

2D

Row Matrix

Shape:

[

[

2D

Column Matrix

Shape:

Important to Note!

A vector can be converted into a column or row matrix.

It is very easy to get confused with (n,) and (1,n) or (1,n). Remember,

that the main difference lies in the

DIMENSION.

Properties & Attributes

The

ndarray

is a central

datastructure

of the

Numpy

library.

Scalar

Vector

Matrix

Tensor

The

size

of the array is the number of elements present. It is the product of

elements in all dimensions.

ndarray.size

will tell you the total number of

elements of the array.

Size:

Size:

Size:

Size:

row

cols

rows

cols

width

rows

3 x 4

(^) x (^3)

x 4

Properties & Attributes

An

axis

is akin to a dimension.

For a 2

dimensional array, there are 2 axes: vertical and horizontal.

In [

]:

Given the following 2

dimensional array

values =

np.array

([[
],
[
]])

Axis=0 # along each "column"

In [

]:

np.sum

values,axis

Out[

]:

array([ 60,

80, 100, 120])

AXIS = 0

60

80

100

120

Specifying axis = 0 implies

perform operation

vertically

AXIS = 0

2D matrix

Properties & Attributes

In [

]:

Given the following 2

dimensional array

values =

np.array

([[
],
[
], ])

Axis=1 # along each “row"

np.sum

(values, axis=

Out[

]:

array([100, 260])

AXIS = 1

260 100

AXIS = 1

Specifying axis = 1 implies

perform operation

horizontally

2D matrix

An

axis

is akin to a dimension.

For a 2

dimensional array, there are 2 axes: vertical and horizontal.