You are on page 1of 8

🔢

Numpy
What is NumPy?
How are Lists different from Numpy?
Applications of NumPy
Tutorial
Installing Numpy
Load in NumPy
The Basics
Acessing and Modifying specific elements, rows, columns, etc.
Initializing Different types of Arrays
Mathematics
Reorganizing Arrays
Miscellaneous

What is NumPy?
A multidimensional array library, you can store date in 1Dimension, 2
Dimensions, 3Dimensions, etc.

How are Lists different from Numpy?


Lists are very slow and Numpy arrays are pretty fast,

Numpy is faster due to being fixed type.

Faster to read less bytes of memory.

There's no type checking

Contiguous Memory

SIMD Vector Processing

Effective Cache Utilization

It can do much much more than lists

Applications of NumPy
Mathematics MATLAB Replacement)

Numpy 1
Plotting Matplotlib)

Backend Pandas, Connect 4, Digital Photography)

Machine Learning

Tutorial
Installing Numpy

sudo apt update


sudo apt upgrade
sudo apt install python3-pip
pip3 install numpy

Load in NumPy

import numpy as np
import sys

The Basics
Defining a one dimensional array

a = np.array([1,2,3])

Defining a one dimensional array and setting its type to 'int8'

a = np.array([1,2,3], dtype='int8')

Defining a two dimensional array of floats

b = np.array([[9.0,8.0,7.0],[6.0,5.0,4.0]])

Getting dimensions of array

print(a.ndim) # Output: 1

Numpy 2
Getting the shape

print(b.shape) # Output: (2,3)

Getting the type

print(b.dtype) # Output: dtype('float64')

Getting the number of bytes

print(a.itemsize) # Output: 1

Getting the number of elements

print(a.size) # Output: 3

Getting the total number of bytes

print(a.itemsize*a.size) # Output: 8*6 = 48

Acessing and Modifying specific elements, rows, columns, etc.


Getting a specific element

# Initializing the array


c = np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])

# Printing the array


print(c)

# Checking its shape


print(c.shape)

# Get a specific element [r, c]


print(c[1,5]) # 2nd row, 6th column

# Get a specific element [r, c]


print(c[1,-2]) # 2nd row, 6th column (2nd from the right)

Getting a specific row/column

Numpy 3
# Getting the 1st row
c[0,:]

# Getting the 3rd Column


c[:,2]

Getting a little more fancy [startindex:endindex:stepsize]

c[0, 1:6:2]

Modifying elements

c[1,5] = 20

Modifying a column/rows

# Replacing the 4th column with itself * 10


c[:, 3] = c[:,3] * 10

# Changing the first row into 5


c[0,:] = 5

Modifying a column

c[:, 3] = c[:,3] * 10

Initializing Different types of Arrays


All zeros matrix

# 1-Dimensional
np.zeros(5)

# 2-Dimensional
np.zeros([2,3])

All ones matrix and how to specify the type of elements

np.ones((4,2,2), dtype='int32')

Numpy 4
All n matrix

np.full((2,2), 99)

Full Like

np.full_like(c, 66)

Random decimal numbers

np.random.rand(4,2)

Random decimal numbers from a sample

np.random.random_sample(c.shape)

Random Integer values

np.random.randint(7, size=(3,3))

Identity matrix

np.identity(3)

Repeating an array

arr = np.array([1,2,3])
rep = np.repeat(arr, 3, axis=0)

Problem

import numpy as np

arr = np.ones((5,5))

z = np.zeros((arr.shape[0]-2, arr.shape[1]-2))

Numpy 5
z[1:-1, 1:-1] = 9
arr[1:-1, 1:-1] = z

print(arr)

Copying an array

a = np.array([1,2,3])
b = a.copy()

Mathematics
Arithmatics

a = np.array([1,2,3,4])

# Arithmatic
a + 2
a * 2
a - 2
a / 2

# With other Arrays


b = np.array([1,0,1,0])
a + b

# Doing functions on full array


np.sin(a)
np.cos(a)

Linear Algebra

Multiplying Matrices

a = np.ones((2,3))
b = np.full((3,2), 2)

np.matmul(a,b)

Getting Determinant

c = np.identity(3)

np.linalg.det(c)

Numpy 6
Statistics

stats = np.array([1,2,3], [4,5,6)

# Minimum
np.min(stats, axis=1)

# Maximum
np.max(stats)

# Summation
np.sum(stats)

Reorganizing Arrays
Changing Shape

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


print(before)

after = before.reshape((2,2))
print(after)

Vertically and Horizontally stacking vectors

v1 = np.array([1,2,3,4])
v2 = np.array([5,6,7,8])
v3 = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])

# Vertical Stacking
np.vstack([v1,v2])

# Horizontal Stacking
np.hstack([v1,v2])

# Column Stacking
np.column_stack([v1,v3])

Vertically and Horizontally stacking vectors

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

# Vertical Stacking
np.vstack([v1,v2])

Numpy 7
# Horizontal Stacking
np.hstack([v1,v2])

Miscellaneous
Load Data from file

filedata = np.genfromtxt('data.txt', delimiter=',')

# changing datatype
filedata.astype('int32')

Boolean Masking and Advanced Indexing

# checking what data are greater than fifty


filedata > 50

# array of values of only numbers greater than fifty


filedata[filedata > 50]

# What coloumns has an element greater than fifty


np.any(filedata > 50, axis=0)

# You can index with a list in NumPy


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

https://www.youtube.com/watch?v=GB9ByFAIAH4

Numpy 8

You might also like