Created
March 30, 2024 21:47
-
-
Save normanlmfung/d395c6fc0aae56ba804d21dd72ac8600 to your computer and use it in GitHub Desktop.
python_numpy_syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
import numpy as np | |
import math | |
from datetime import datetime | |
from tabulate import tabulate | |
# Illustration 1. Array initialization: ones/zeros/arrange/linspace | |
np.arange(0, 10, 2) # array([0, 2, 4, 6, 8]) | |
np.zeros(10) | |
np.ones(10) | |
''' | |
Two rows of zero's: | |
array( | |
[ | |
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], | |
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]] | |
) | |
''' | |
np.zeros((2, 10)) | |
np.linspace(0, 100, 11) # array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.]) | |
''' | |
Three rows of random numbers: | |
array([[-0.99363132, -1.06340081, -0.63807592, 1.06158869, -0.15740415], | |
[ 1.09372848, -2.07485402, 0.70063301, 1.67463863, -0.39733411], | |
[ 2.32379695, -0.99970063, -0.55914676, -2.00343542, -0.36605912]]) | |
''' | |
np.random.seed(48) | |
np.random.randn(3,5) | |
np.random.randint(1,100, (2,3)) # Random number between 1-100, in 2 rows, each row 3 elements. | |
# Illustration 2. Reshape | |
arr = np.random.randint(0, 100, 10) # Generate 10 random numbers with values between 0-100. Example: array([54, 92, 12, 85, 26, 22, 2, 61, 21, 7]) | |
arr.reshape(2,5) # Split the ten numbers into two rows: Each row five elements. | |
''' | |
array([[35, 50, 64, 67, 3], | |
[92, 15, 32, 2, 49]]) | |
''' | |
# Illustration 3. min/max/argmin/argmax - argmin and argmax are index of min and max elements | |
arr = np.random.randint(0, 50, 10) | |
print(f"max: {arr.max()}, index: {arr.argmax()}, min: {arr.min()}, index: {arr.argmin()}") | |
# Illustration 4. Slicing | |
arr = np.random.randint(0, 100, 10) # List of ten numbers with values between 0-100 | |
arr[7:] # Take from 7th element on | |
# Illustration 5. Filtering | |
''' | |
arr | |
array([92, 30, 94, 60, 56, 57, 30, 10, 48, 68]) | |
filter - an array of True or False | |
array([ True, False, True, True, True, True, False, False, False, True]) | |
arr[filter] | |
array([92, 94, 60, 56, 57, 68]) | |
''' | |
arr = np.random.randint(0, 100, 10) | |
filter = arr>50 | |
arr[filter] | |
# Illustration 6. Array Operations | |
arr1 = np.random.randint(0, 100, 10) | |
arr2 = np.random.randint(0, 100, 10) | |
arr3 = arr1 + arr2 | |
arr3 = arr3/100 # Instead of looping elements, vectorized operations much faster. | |
arr3 | |
# Illustration 7. Sum across rows or columns | |
# 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]) | |
arr = np.arange(0, 25) | |
''' | |
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]]) | |
''' | |
arr = arr.reshape(5,5) | |
''' | |
Sum across the rows | |
array([50, 55, 60, 65, 70]) | |
''' | |
arr.sum(axis=0) | |
''' | |
Sum across the columns | |
array([ 10, 35, 60, 85, 110]) | |
''' | |
arr.sum(axis=1) | |
# Illustration 8. statistics: min/max/mean/var/std | |
arr = np.random.randint(0, 100, 1000) # Generate 1000 numbers, values between 0-100 | |
print(f"max: {arr.max()}, min: {arr.min()}, mean: {arr.mean()}, variance: {arr.var()}, standard deviation: {arr.std()}") | |
arr | |
# Illustration 9. Broadcasting | |
arr1 = np.array([[1, 2, 3]]) # Shape: (1, 3) | |
arr2 = np.array([[4], [5], [6]]) # Shape: (3, 1) | |
''' | |
array([[4+1, 4+2, 4+3], | |
[5+1, 5+2, 5+3], | |
[6+1, 6+2, 6+3]]) | |
array([[5, 6, 7], | |
[6, 7, 8], | |
[7, 8, 9]]) | |
''' | |
arr3 = arr1 + arr2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment