RYAN'S WEBSITE

Just something small and tiny.

Held by Ryan Ruoshui Yan.


Here is the world of a nerdy guy.

Visited:

NumPy Notes

Basic

# Import
import numpy as np

# Create an array
matrix = np.array([1, 2, 3, 4])
# Important: Not "matrix = np.array(1, 2, 3, 4)"
# 2D array:
matrix2 = np.array([[1,2], [3,4]])
# Note: "matrix2[1][0]" or "matrix2[1, 0]" gives 3, not 2. Vertical first, then horizontal
# Note: Get second row:
# matrix2[1, :]
# or
# matrix2[1][:]
# or
# matrix2[1]

# Create an array with range
np.arange(begin, end, step)

# Create an array with all 0s
np.zeros([3,4])
# Important: Not "np.zeros(3,4)"

# Create an array with all 1s
np.ones([3,4])
# Important: Not "np.ones(3,4)"

# Create identical matrix
np.eye(10, 10)

# Reshape array
matrix.reshape(2,2) # Total size of new array must be unchanged

# Type of array
type(matrix) # numpy.ndarray

# Transpose
matrix.T

# Dimension
matrix.shape

# Rank(?)
matrix.ndim

# Type of item(Default: 64, int64/float64)
matrix.dtype

# Size
matrix.size

# Size of item (Default: 8 = 64/8)
matrix.itemsize

# Change value of an item
matrix[1] = 0

# Change to different type
# Add dtype=np.int16 as parameter
# or:
matrix = matrix.astype(np.uint8)

# Random
np.random.random([...])

# Sum
matrix.sum()

# Min & Max
matrix.min()
matrix.max()

# Filter
a[a>5]
# Gets array([6, 7, 8, 9])
# Note: Diff from "a>5"!!!

Concatenate

a = np.arange(10)
b = np.arange(10,20)
np.concatenate([a,b]) # Not "a+b"!!!

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))
print(np.concatenate((a, b.T), axis=1))

Operations

# Initialize
a = np.array([20, 30, 40, 50])
b = np.arange(4)

# Arithmetic operators on array apply ELEMENTWISE
# Infix: + - * / ** <
# np.exp()
# np.sqrt()
# np.add() # Same as +

# Dot Product (Inner Product)
a.dot(b) # Not "a*b"
# or
np.dot(a,b)

# Cross Product
np.cross(np.array([1, 2, 3]), np.array([4, 5, 6]))

# Linear algebraic multiplication
np.matmul(a, b) # Not "a*b"

Not quite understood

# 1. Diff btw [] and ()?

# 2. What does parameter "axis" do?
RECENT

OpenCV Notes

import cv2import numpy as np# Show imagedef imshow(img): cv2.imshow("windows name", img) cv2.waitKey(0) # Need to add .waitKey() for the image to stay on the screen# Read an image using .imread()img = cv2.imread('pie.png')# Show imageimsh...…

MORE
EARLIER

Visual Computing Notes

Disclaimer: The notes below are fully/partially NOT created by myself. They are from slides and/or wikipedia and/or textbook. The purpose of this post is simply to learn and review for the course. If you think something is inappropriate, please co...…

MORE