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

Numpy

The document provides an overview of the Numpy library in Python, highlighting its applications in scientific computing, data processing, and machine learning. It details common attributes of Numpy arrays, methods for creating and manipulating arrays, and various array operations such as addition, subtraction, and matrix multiplication. Additionally, it includes examples of array indexing and reshaping techniques.
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)
22 views25 pages

Numpy

The document provides an overview of the Numpy library in Python, highlighting its applications in scientific computing, data processing, and machine learning. It details common attributes of Numpy arrays, methods for creating and manipulating arrays, and various array operations such as addition, subtraction, and matrix multiplication. Additionally, it includes examples of array indexing and reshaping techniques.
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
You are on page 1/ 25

Tien-Lam Pham

Ph-D
Falculty of Computer Science
Van-Quyen Nguyen
1. INTRODUCTION

Numpy is a Python library for scienti>ic computations

Visualization ML
DL

IDE
Scientific computing
Data processing/
analysis Natural Language
processing
2. COMMON ATTRIBUTES

dtype Data type


nbytes Number of bytes used to store the data
ndim Number of dimensions
shape A tuple of number of elements in each dimension
size Total number elements
2. COMMON ATTRIBUTES

dtype Data type

dtype Variants Description

int int8, int16, int32, int64, … Intergers

uint uint8, uint16, uint64, … Unsigned intergers

bool Bool Boolean

>loat Lloat16, Lloat32, Lloat64, … Floating-point numbers

Complex-valued Lloat
complex complex64, complex128, …
number
3. UPDATE AN ELEMENT

index 0 1 2

data = 1 2 3

data[0] = 8

data = 8 2 3
4. CREATE NUMPY ARRAY

From list
arr_np = np.array(python_list)
4. CREATE NUMPY ARRAY

From common fuctions

zeros() ones() full()

0 0 0 1 1 1 8 8 8
0 0 0 1 1 1 8 8 8
4. CREATE NUMPY ARRAY

From common fuctions

arange() eye() random()


arr1 = 0 1 2 3 1 0 0
0 1 0
arr2 = 0 5 10 0 0 1
4. CREATE NUMPY ARRAY

numpy.hstack()

arr1 = 0 1 2 3

arr2 = 4 5 6 7

hstack((arr1, arr2))
result = 0 1 2 3 4 5 6 7
4. CREATE NUMPY ARRAY

numpy.vstack()

arr1 = 0 1 2 3

arr2 = 4 5 6 7

vstack((arr1, arr2))

0 1 2 3
result =
4 5 6 7
4. CREATE NUMPY ARRAY

numpy.concatenate()

arr1 = 0 1 2 3

arr2 = 4 5 6 7

concatenate((arr1, arr2), axis = 0)


0 1 2 3
result =
4 5 6 7

concatenate((arr1, arr2), axis = 1)

result = 0 1 2 3 4 5 6 7
4. CREATE NUMPY ARRAY

numpy.array_split()
numpy.split()

1 2 3 4 5 6

aray_split(arr, 3)

newarr[0] 1 2

newarr[1] 3 4

newarr[2] 5 6
4. CREATE NUMPY ARRAY

where() fuction >latten() fuction

0 1
arr = 0 1 2 3 arr =
4 5

arr<2 = T T F F
out = 0 1 4 5
out = 0 1 4 6
4. CREATE NUMPY ARRAY

reshape() fuction

0 1 2 3
arr =
4 5 6 7

0 1
out = 2 3

4 5

6 7
5. ARRAY INDEXING

arr[for_axis_0, for_axis_1, …]
“:“ get all the elements
th th
“a:b“ get the elements from a to (b -1)

arr arr[0,1] arr[1:3] arr[0:2, 0] arr[:, 1:]


0 1
0 0 1 0 1 0 1 0 1 0 1

1 2 3 2 3 2 3 2 3 2 3

2 4 5 4 5 4 5 4 5 4 5

3 6 7 6 7 6 7 6 7 6 7
5. ARRAY INDEXING

Excercise
5. ARRAY INDEXING

Excercise
5. ARRAY INDEXING

Excercise
5. NUMPY ARRAY OPERATION

Addition Subtraction

arr1 0 1 2 3 arr1 4 5 3 0
+ -
arr2 4 5 6 7 arr2 0 1 2 3
= =
4 6 8 10 4 4 1 -3
5. NUMPY ARRAY OPERATION

Multiplication Devision

arr1 0 1 2 3 arr1 4 5 3 1
* /
arr2 4 5 6 7 arr2 1 1 2 3
= =
0 5 12 21 4 5 1.5 0.333
5. NUMPY ARRAY OPERATION

Matrix Multiplication

X Y out

1 1 2 0 1 = 10 14
1 2 3 2 3 16 22

4 5
5. NUMPY ARRAY OPERATION

Transpose

T
10 14 = 10 16
16 22 14 22
5. NUMPY ARRAY OPERATION
5. NUMPY ARRAY OPERATION
5. NUMPY ARRAY OPERATION

You might also like