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

Numpy Note

The document discusses NumPy arrays and operations on arrays. It shows how to create arrays with specific values using functions like np.zeros, np.ones, and np.full. It also demonstrates indexing, slicing, and basic operations on arrays like addition through broadcasting. Key array attributes like shape and different ways to access elements or subarrays are presented.

Uploaded by

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

Numpy Note

The document discusses NumPy arrays and operations on arrays. It shows how to create arrays with specific values using functions like np.zeros, np.ones, and np.full. It also demonstrates indexing, slicing, and basic operations on arrays like addition through broadcasting. Key array attributes like shape and different ways to access elements or subarrays are presented.

Uploaded by

Haing p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Numpy Note

March 22, 2021

In [1]: import numpy as np

1 Creating Numpy Array


In [4]: A = np.array([[[ 0, 1, 2, 3, 4, 5, 6, 7]],

[[ 8, 9, 10, 11, 12, 13, 14, 15]],

[[16, 17, 18, 19, 20, 21, 22, 23]],

[[24, 25, 26, 27, 28, 29, 30, 31]]])

In [22]: B = np.arange(48)
B

Out[22]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,


17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47])

In [23]: C = np.zeros((4,4))
C

Out[23]: array([[0., 0., 0., 0.],


[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

In [25]: D = np.ones((5, 5))


D

Out[25]: array([[1., 1., 1., 1., 1.],


[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])

In [27]: E = np.full((6, 6), 10)


E

1
Out[27]: array([[10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10]])

In [29]: F = np.eye(10)
F

Out[29]: array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])

2 Array Shape
In [9]: A.shape

Out[9]: (4, 1, 8)

In [20]: B = B.reshape(1, 6, 8)

3 Array Axis
Imagine collapsing the chosen axis

In [12]: A.max()

Out[12]: 31

In [13]: A.max(axis = 0)

Out[13]: array([[24, 25, 26, 27, 28, 29, 30, 31]])

In [14]: A.max(axis = 1)

Out[14]: array([[ 0, 1, 2, 3, 4, 5, 6, 7],


[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31]])

In [15]: A.max(axis = 2)

2
Out[15]: array([[ 7],
[15],
[23],
[31]])

4 Indexing and Slicing


4.0.1 Single Element
In [161]: a = np.arange(12).reshape(3, 4)
a

Out[161]: array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [162]: a[1, 2]

Out[162]: 6

In [163]: a[-1, -1]

Out[163]: 11

4.0.2 Subarray
Index Array Form: array[array, array]
Output: 1D array of values with indices (x, y)
Note: return a copy of the subarray

In [176]: a = np.arange(12).reshape(3, 4)
a

Out[176]: array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [177]: # (0,0), (0,3), (2,0), (2,3)


b = a[[0, 0, 2, 2], [0, 3, 0, 3]]
b

Out[177]: array([ 0, 3, 8, 11])

In [178]: # when shapes doesn't match attempt broadcast


# essentially b = a[[1, 2], [2, 2]]
b = a[[1,2], [2]]
b

Out[178]: array([ 6, 10])

3
Slices Form: array[slice, slice]
Output: Array slice with same dimension
Note: return the view of the subarray. use copy() if want a copy

In [192]: a = np.arange(12).reshape(3, 4)
a

Out[192]: array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [193]: b = a[:2, 1:3]


b

Out[193]: array([[1, 2],


[5, 6]])

Index Array with Slices Form: array[slice/array, slice/array]


Output: Array with same dimension Note: return a copy

In [197]: a = np.arange(12).reshape(3, 4)
a

Out[197]: array([[ 0, 1, 2, 3],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

In [198]: b = a[[0, 2], 1:3]


b

Out[198]: array([[ 1, 2],


[ 9, 10]])

5 Broadcasting and Vectorization


Broadcasting Rule: for each dimension, either equal or one is 1

In [18]: A + B

Out[18]: array([[[ 0, 2, 4, 6, 8, 10, 12, 14],


[ 8, 10, 12, 14, 16, 18, 20, 22],
[16, 18, 20, 22, 24, 26, 28, 30],
[24, 26, 28, 30, 32, 34, 36, 38],
[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54]],

[[ 8, 10, 12, 14, 16, 18, 20, 22],


[16, 18, 20, 22, 24, 26, 28, 30],
[24, 26, 28, 30, 32, 34, 36, 38],

4
[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54],
[48, 50, 52, 54, 56, 58, 60, 62]],

[[16, 18, 20, 22, 24, 26, 28, 30],


[24, 26, 28, 30, 32, 34, 36, 38],
[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54],
[48, 50, 52, 54, 56, 58, 60, 62],
[56, 58, 60, 62, 64, 66, 68, 70]],

[[24, 26, 28, 30, 32, 34, 36, 38],


[32, 34, 36, 38, 40, 42, 44, 46],
[40, 42, 44, 46, 48, 50, 52, 54],
[48, 50, 52, 54, 56, 58, 60, 62],
[56, 58, 60, 62, 64, 66, 68, 70],
[64, 66, 68, 70, 72, 74, 76, 78]]])

You might also like