새싹/TIL

[핀테커스] 230922 python 수학 실습

jykim23 2023. 9. 22. 19:55

Exercise.1 - 89: Hadamard Product (5)

In [ ]:
vectors = [
            [1, 11, 21],
            [2, 12, 22],
            [3, 13, 23],
            [4, 14, 24]
           ]

cnt_col = len(vectors[0])
cnt_row = len(vectors)

hadamard_vector = [1 for _ in range(cnt_row)]

for idx_row in range(cnt_row):
    for row in vectors[idx_row]:
        hadamard_vector[idx_row] *= row

print(f'hadamard vector: {hadamard_vector}')
hadamard vector: [231, 528, 897, 1344]

Exercise.1 - 90: Vector Norm(4)

In [ ]:
vectors = [
            [1, 11, 21],
            [2, 12, 22],
            [3, 13, 23],
            [4, 14, 24]
           ]
cnt_col = len(vectors[0])
cnt_row = len(vectors)

# norm_of_vectors = []
# for idx_col in range(cnt_col):
#     sqaure_sum = 0
#     for idx_row in range(cnt_row):
#         sqaure_sum += vectors[idx_row][idx_col] ** 2
#     norm_of_vectors.append(sqaure_sum ** 0.5)

norm_of_vectors = [sum([
    vectors[idx_row][idx_col]**2 for idx_row in range(cnt_row) # 제곱의 합
])**0.5 for idx_col in range(cnt_col)] # ** 1/2


print(f'norm of vectors = {norm_of_vectors}')
norm of vectors = [5.477225575051661, 25.099800796022265, 45.05552130427524]

Exercise.1 - 91: Macking Unit Vectors(4)

In [ ]:
vectors = [
            [1, 11, 21],
            [2, 12, 22],
            [3, 13, 23],
            [4, 14, 24]
           ]
cnt_col = len(vectors[0])
cnt_row = len(vectors)

norm_of_vectors = [
    sum([ # 제곱의 합
    vectors[idx_row][idx_col]**2 for idx_row in range(cnt_row) # 제곱 
])**0.5 for idx_col in range(cnt_col)] # ( 제곱의 합 ) ** 1/2
print(f'norm of vectors = {norm_of_vectors}')

for idx_col in range(cnt_col):
    for idx_row in range(cnt_row):
        vectors[idx_row][idx_col] = vectors[idx_row][idx_col] / norm_of_vectors[idx_col]
print(f'unit vectors = {vectors}')

norm_of_vectors = [
    sum([ # 제곱의 합
    vectors[idx_row][idx_col]**2 for idx_row in range(cnt_row) # 제곱 
])**0.5 for idx_col in range(cnt_col)] # ( 제곱의 합 ) ** 1/2
print(f'norm of vectors = {norm_of_vectors}')
norm of vectors = [5.477225575051661, 25.099800796022265, 45.05552130427524]
unit vectors = [[0.18257418583505536, 0.4382504900892777, 0.46609159969939906], [0.3651483716701107, 0.4780914437337575, 0.4882864377803228], [0.5477225575051661, 0.5179323973782373, 0.5104812758612466], [0.7302967433402214, 0.5577733510227171, 0.5326761139421703]]
norm of vectors = [0.9999999999999999, 1.0, 1.0]

Exercise.1 - 92: Dot product (4)

In [ ]:
vectors = [
    [1, 11],
    [2, 12],
    [3, 13],
    [4, 14]
]
cnt_col = len(vectors[0])
cnt_row = len(vectors)

dot_prod = sum([v1 * v2 for v1, v2 in vectors])
print(f'dot product: {dot_prod}')
dot product: 130

Exercise.1 - 93: Euclidean Distance(4)

In [ ]:
vectors = [
    [1, 11],
    [2, 12],
    [3, 13],
    [4, 14]
]
cnt_col = len(vectors[0])
cnt_row = len(vectors)


# sqaure_of_diff = [(v1 - v2) ** 2 for v1, v2 in vectors]
euclidena_dis = sum(
    [(v1 - v2) ** 2 for v1, v2 in vectors]
) ** 0.5
print(f'euclidean distance: {euclidena_dis}')
euclidean distance: 20.0

Exercise.1 - 94: 과목별 최고점, 최우수 학생구하기

In [ ]:
scores = [
    [10, 40, 20],
    [50, 20, 60],
    [70, 40, 30],
    [30, 80, 40]
]

cnt_student = len(scores)
cnt_class = len(scores[0])

max_scores = []
max_score_indeces = []

for idx_class in range(cnt_class):
    max_score = 0
    max_score_index = 0
    for idx_student in range(cnt_student):
        if scores[idx_student][idx_class] > max_score:
            max_score = scores[idx_student][idx_class]
            max_score_index = idx_student
    max_scores.append(max_score)
    max_score_indeces.append(max_score_index)
print(f'max score, max score index: {max_scores}, {max_score_indeces}')
max score, max score index: [70, 80, 60], [2, 3, 1]

Exercise.1 - 95: One-hot Encoding

In [ ]:
labels = [0, 1, 2, 1, 0, 3]

n_label = len(labels)
n_class = len(set(labels))
print(f'n_label = {n_label}, n_class = {n_class}')

for label in labels:
    if label > n_class:
        n_class = label

one_hot_mat = []
for label in labels:
    one_hot_vec = []
    for _ in range(n_class):
        one_hot_vec.append(0)
    one_hot_vec[label] = 1

    one_hot_mat.append(one_hot_vec)

# print(f'one hot mat = {one_hot_mat}')
print(f'<< one hot mat >>', *one_hot_mat, sep='\n')
n_label = 6, n_class = 4
<< one hot mat >>
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 1, 0, 0]
[1, 0, 0, 0]
[0, 0, 0, 1]

Exercise.1 - 96: Accuracy 구하기(2)

In [ ]:
predictions = [
    [1, 0, 0, 0], 
    [0, 0, 1, 0],
    [0, 0, 1, 0], 
    [1, 0, 0, 0],
    [1, 0, 0, 0], 
    [0, 0, 0, 1]
]
labels = [0, 1, 2, 1, 0, 3]
print('--- predictions ---', *predictions, sep='\n')

print('--- one hot encoding ---')
n_label = len(labels)
n_class = len(set(labels))
print(f'n_label = {n_label}, n_class = {n_class}')


one_hot_mat = []
for label in labels:
    one_hot_vec = []
    for _ in range(n_class):
        one_hot_vec.append(0)
    one_hot_vec[label] = 1
    one_hot_mat.append(one_hot_vec)
print('--- init one hot mat ---', *one_hot_mat, sep='\n')

print('--- Accuracy ---')
class_cnts, correct_cnt, confusion_vec = [], [], []

for _ in range(n_class):
    class_cnts.append(0)
    correct_cnt.append(0)
    confusion_vec.append(None)

for pred_idx in range(len(predictions)):
    pred = predictions[pred_idx]
    one_hot_vec = one_hot_mat[pred_idx]
    label = labels[pred_idx]
    print(f'pred, one_hot_vec, label: {pred}, {one_hot_vec}, {label}')
    class_cnts[label] += 1
    if pred == one_hot_vec:
        print('--- correct! ---')
        correct_cnt[label] += 1
    print(f'class_cnts, correct_cnt: {class_cnts}, {correct_cnt}')
    
for class_idx in range(n_class):
    confusion_vec[class_idx] = correct_cnt[class_idx] / class_cnts[class_idx]

print(f'class_cnts: {class_cnts}')
print(f'correct_cnt: {correct_cnt}')
print(f'confusion vector: {confusion_vec}')

print(f'accuracy: {sum(correct_cnt) / sum(class_cnts)}')
--- predictions ---
[1, 0, 0, 0]
[0, 0, 1, 0]
[0, 0, 1, 0]
[1, 0, 0, 0]
[1, 0, 0, 0]
[0, 0, 0, 1]
--- one hot encoding ---
n_label = 6, n_class = 4
--- init one hot mat ---
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 1, 0, 0]
[1, 0, 0, 0]
[0, 0, 0, 1]
--- Accuracy ---
pred, one_hot_vec, label: [1, 0, 0, 0], [1, 0, 0, 0], 0
--- correct! ---
class_cnts, correct_cnt: [1, 0, 0, 0], [1, 0, 0, 0]
pred, one_hot_vec, label: [0, 0, 1, 0], [0, 1, 0, 0], 1
class_cnts, correct_cnt: [1, 1, 0, 0], [1, 0, 0, 0]
pred, one_hot_vec, label: [0, 0, 1, 0], [0, 0, 1, 0], 2
--- correct! ---
class_cnts, correct_cnt: [1, 1, 1, 0], [1, 0, 1, 0]
pred, one_hot_vec, label: [1, 0, 0, 0], [0, 1, 0, 0], 1
class_cnts, correct_cnt: [1, 2, 1, 0], [1, 0, 1, 0]
pred, one_hot_vec, label: [1, 0, 0, 0], [1, 0, 0, 0], 0
--- correct! ---
class_cnts, correct_cnt: [2, 2, 1, 0], [2, 0, 1, 0]
pred, one_hot_vec, label: [0, 0, 0, 1], [0, 0, 0, 1], 3
--- correct! ---
class_cnts, correct_cnt: [2, 2, 1, 1], [2, 0, 1, 1]
class_cnts: [2, 2, 1, 1]
correct_cnt: [2, 0, 1, 1]
confusion vector: [1.0, 0.0, 1.0, 1.0]
accuracy: 0.6666666666666666

Exercise.1 - 97: Matrix Addition

In [ ]:
mat1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

mat2 = [
    [11, 12, 13],
    [14, 15, 16],
    [17, 18, 19]
]

cnt_col = len(mat1[0])
cnt_row = len(mat1)

add_mat = [
    [mat1[idx_row][idx_col]+mat2[idx_row][idx_col] 
    for idx_row in range(cnt_row)] 
    for idx_col in range(cnt_col)
]
print(f'Matrix Addition: {add_mat}')

        
Matrix Addition: [[12, 18, 24], [14, 20, 26], [16, 22, 28]]

Exercise.1 - 98: Matrix-Vector Multiplication

In [ ]:
mat = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
vec = [10, 20, 30]

cnt_col = len(mat[0])
cnt_row = len(mat)

mul_vec = [
    sum([mat[idx_row][idx_col] * vec[idx_col] 
     for idx_col in range(cnt_col)]) 
     for idx_row in range(cnt_row)
]

print(f'multiplication of mat and vec: {mul_vec}')
multiplication of mat and vec: [140, 320, 500]

Exercise.1 - 99: Matrix-Matrix Multiplication

In [ ]:
mat1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

mat2 = [
    [11, 12, 13],
    [14, 15, 16],
    [17, 18, 19]
]

cnt_col = len(mat1[0])
cnt_row = len(mat1)

mul_mat = [[None for i in range(cnt_col)] for j in range(cnt_row)]
print(f'mul_mat = ', *mul_mat, sep='\n')


for idx_row1 in range(cnt_row): 
    mat1_vec = mat1[idx_row1]
    mul_vec = [] # init 
    for idx_row2 in range(cnt_row):
        mat2_vec = []
        for idx_col in range(cnt_col): #convert to mat2[*][idx_row2]
            mat2_vec.append(mat2[idx_col][idx_row2])
        print(f'mat1_vec = {mat1_vec}')
        print(f'mat2_vec = {mat2_vec}')

        
        mul_vec.append(sum([mat1_vec[idx_c] * mat2_vec[idx_c] 
                           for idx_c in range(cnt_col)]))

    mul_mat[idx_row1] = mul_vec # insert into mul_mat
    print(f'mul_vec: {mul_vec}')
print(f'<<< mul_mat >>>', *mul_mat, sep='\n')

    
mul_mat = 
[None, None, None]
[None, None, None]
[None, None, None]
mat1_vec = [1, 2, 3]
mat2_vec = [11, 14, 17]
mat1_vec = [1, 2, 3]
mat2_vec = [12, 15, 18]
mat1_vec = [1, 2, 3]
mat2_vec = [13, 16, 19]
mul_vec: [90, 96, 102]
mat1_vec = [4, 5, 6]
mat2_vec = [11, 14, 17]
mat1_vec = [4, 5, 6]
mat2_vec = [12, 15, 18]
mat1_vec = [4, 5, 6]
mat2_vec = [13, 16, 19]
mul_vec: [216, 231, 246]
mat1_vec = [7, 8, 9]
mat2_vec = [11, 14, 17]
mat1_vec = [7, 8, 9]
mat2_vec = [12, 15, 18]
mat1_vec = [7, 8, 9]
mat2_vec = [13, 16, 19]
mul_vec: [342, 366, 390]
<<< mul_mat >>>
[90, 96, 102]
[216, 231, 246]
[342, 366, 390]

Exercise.1 - 100: Transposed Matrix

In [ ]:
mat = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
cnt_col = len(mat[0])
cnt_row = len(mat)

tmmat = [[mat[col][row] for col in range(cnt_col)] for row in range(cnt_row)]
print(tmmat)
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]