Professional Documents
Culture Documents
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 1Dimension, 2
Dimensions, 3Dimensions, etc.
Contiguous Memory
Applications of NumPy
Mathematics MATLAB Replacement)
Numpy 1
Plotting Matplotlib)
Machine Learning
Tutorial
Installing Numpy
Load in NumPy
import numpy as np
import sys
The Basics
Defining a one dimensional array
a = np.array([1,2,3])
a = np.array([1,2,3], dtype='int8')
b = np.array([[9.0,8.0,7.0],[6.0,5.0,4.0]])
print(a.ndim) # Output: 1
Numpy 2
Getting the shape
print(a.itemsize) # Output: 1
print(a.size) # Output: 3
Numpy 3
# Getting the 1st row
c[0,:]
c[0, 1:6:2]
Modifying elements
c[1,5] = 20
Modifying a column/rows
Modifying a column
c[:, 3] = c[:,3] * 10
# 1-Dimensional
np.zeros(5)
# 2-Dimensional
np.zeros([2,3])
np.ones((4,2,2), dtype='int32')
Numpy 4
All n matrix
np.full((2,2), 99)
Full Like
np.full_like(c, 66)
np.random.rand(4,2)
np.random.random_sample(c.shape)
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
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
# Minimum
np.min(stats, axis=1)
# Maximum
np.max(stats)
# Summation
np.sum(stats)
Reorganizing Arrays
Changing Shape
after = before.reshape((2,2))
print(after)
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])
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
# changing datatype
filedata.astype('int32')
https://www.youtube.com/watch?v=GB9ByFAIAH4
Numpy 8