Exercise.1 - 50: Standardization(3)¶
Data index를 이용하여 standardization을 하는 방법에 대해 복습합니다.
정규분포 -> 표준정규분포 변경하는 작업
scores = [10, 20, 30]
n_student = len(scores)
score_sum, score_square_sum = 0, 0
# 표준편차
for idx in range(n_student):
score_sum += scores[idx]
score_square_sum += scores[idx] ** 2
mean = score_sum / n_student
square_of_mean = mean**2
mean_of_square = score_square_sum / n_student
variance = mean_of_square - square_of_mean
std = variance**0.5
print(f'scores = {scores}')
print(f'mean = {mean}')
print(f'variance = {variance}')
print(f'std = {std}')
# Standardization: (원소-평균)/표준편차
print('--- Standardization ---')
for student_idx in range(n_student):
scores[student_idx] = (scores[student_idx] - mean) / std
# 표준편차
score_sum, score_square_sum = 0, 0
for idx in range(n_student):
score_sum += scores[idx]
score_square_sum += scores[idx] ** 2
mean = score_sum / n_student
square_of_mean = mean**2
mean_of_square = score_square_sum / n_student
variance = mean_of_square - square_of_mean
std = variance**0.5
print(f'scores = {scores}')
print(f'mean = {mean}')
print(f'variance = {variance}')
print(f'std = {std}')
scores = [10, 20, 30] mean = 20.0 variance = 66.66666666666669 std = 8.16496580927726 --- Standardization --- scores = [-1.224744871391589, 0.0, 1.224744871391589] mean = 0.0 variance = 0.9999999999999999 std = 1.0
Exercise.1 - 51: 분산과 표준편차(4)¶
Data index를 통해 두 List에 대한 분산과 표준편차를 구하는 방법에 대해 익혀봅니다.
math_scores , english_scores = [50, 60, 70], [30, 40, 50]
n_student = len(math_scores)
math_sum, english_sum = 0, 0
math_square_sum, english_square_sum = 0, 0
for student_idx in range(n_student):
math_sum += math_scores[student_idx]
english_sum += english_scores[student_idx]
math_square_sum += math_scores[student_idx] ** 2
english_square_sum += english_scores[student_idx] ** 2
math_mean = math_sum / n_student
english_scores_mean = english_sum / n_student
math_variance = math_square_sum / n_student - math_mean ** 2
english_variance = english_square_sum / n_student - english_scores_mean ** 2
math_std = math_variance ** 0.5
english_std = english_variance ** 0.5
print(f'mean/std of Math: {math_mean:.2f}/{math_std:.2f}')
print(f'mean/std of English: {english_scores_mean:.2f}/{english_std:.2f}')
mean/std of Math: 60.00/8.16 mean/std of English: 40.00/8.16
Exercise.1 - 52: Standardization (4)¶
Data index를 통해 두 List에 대해 standardization을 하는 방법에 대해 복습합니다.
math_scores , english_scores = [50, 60, 70], [30, 40, 50]
n_student = len(math_scores)
math_sum, math_mean, english_sum, english_mean = 0, 0, 0, 0
math_square_sum, english_square_sum = 0, 0
# variance and standard deviation
for student_idx in range(n_student):
math_sum += math_scores[student_idx]
english_sum += english_scores[student_idx]
math_square_sum += math_scores[student_idx] ** 2
english_square_sum += english_scores[student_idx] ** 2
math_mean = math_sum / n_student
english_mean = english_sum / n_student
math_variance = math_square_sum / n_student - math_mean ** 2
english_variance = english_square_sum / n_student - english_mean ** 2
math_std = math_variance ** 0.5
english_std = english_variance ** 0.5
print(f'math_scores : {math_scores}')
print(f'english_scores : {english_scores}')
print(f'math_std : {math_std}')
print(f'english_std : {english_std}')
# (원소-평균)/표준편차
print('--- standard deviation ---')
math_sum, math_mean, english_sum, english_mean = 0, 0, 0, 0
math_square_sum, english_square_sum = 0, 0
for student_idx in range(n_student):
math_scores[student_idx] = (math_scores[student_idx] - math_mean) / math_std
english_scores[student_idx] = (english_scores[student_idx] - english_mean) / english_std
# variance and standard deviation
for student_idx in range(n_student):
math_sum += math_scores[student_idx]
english_sum += english_scores[student_idx]
math_square_sum += math_scores[student_idx] ** 2
english_square_sum += english_scores[student_idx] ** 2
math_mean = math_sum / n_student
english_mean = english_sum / n_student
math_variance = math_square_sum / n_student - math_mean ** 2
english_variance = english_square_sum / n_student - english_mean ** 2
math_std = math_variance ** 0.5
english_std = english_variance ** 0.5
print(f'math_scores : {math_scores}')
print(f'english_scores : {english_scores}')
print(f'math_std : {math_std}')
print(f'english_std : {english_std}')
math_scores : [50, 60, 70] english_scores : [30, 40, 50] math_std : 8.164965809277252 english_std : 8.164965809277264 --- standard deviation --- math_scores : [6.123724356957951, 7.348469228349542, 8.573214099741133] english_scores : [3.6742346141747655, 4.898979485566354, 6.1237243569579425] math_std : 0.9999999999999964 english_std : 0.9999999999999982
Exercise.1 - 53: Hadamard Product(4)¶
For loop을 이용해 vector operation을 하는 방법에 대해 배웁니다.
v1 = [1, 2, 3, 4, 5]
v2 = [10, 20, 30, 40, 50]
# method.1
v3 = []
for dim_idx in range(len(v1)):
v3.append(v1[dim_idx] * v2[dim_idx])
print(v3)
# method.2
v3 = []
for _ in range(len(v1)):
v3.append(0)
for dim_idx in range(len(v2)):
v3[dim_idx] = v1[dim_idx] * v2[dim_idx]
print(v3)
[10, 40, 90, 160, 250] [10, 40, 90, 160, 250]
Exercise.1 - 54: Vector Norm(3)¶
For loop을 이용해 vector norm을 구하는 방법에 대해 복습합니다.
v1 = [1, 2, 3]
square_sum = 0
for dim_val in v1:
square_sum += dim_val ** 2
norm = square_sum ** 0.5
print(f'norm of v1 is {norm}')
norm of v1 is 3.7416573867739413
Exercise.1 - 55: Making Unit Vectors(3)¶
For loop을 이용해 unit vector를 만드는 방법에 대해 복습합니다.
v1 = [1, 2, 3]
square_sum = 0
for dim_val in v1:
square_sum += dim_val ** 2
norm = square_sum ** 0.5
print(f'v1: {v1}')
print(f'norm: {norm}')
print('--- making unit vector ---')
for idx in range(len(v1)):
v1[idx] = v1[idx] / norm
square_sum = 0
for dim_val in v1:
square_sum += dim_val ** 2
norm = square_sum ** 0.5
print(f'v1: {v1}')
print(f'norm: {norm}')
v1: [1, 2, 3] norm: 3.7416573867739413 --- making unit vector --- v1: [0.2672612419124244, 0.5345224838248488, 0.8017837257372732] norm: 1.0
Exercise.1 - 56: Dot Product(3)¶
For loop을 이용해 dot product를 연산하는 방법에 대해 복습합니다.
v1, v2 = [1, 2, 3], [3, 4, 5]
dot_prod = 0
for dim_idx in range(len(v1)):
dot_prod += v1[dim_idx] * v2[dim_idx]
print(f'dot product of v1 and v2: {dot_prod}')
dot product of v1 and v2: 26
Exercise.1 - 57: Euclidean Distance (3)¶
For loop을 이용해 Euclidean distance를 연산하는 방법에 대해 복습합니다.
v1, v2 = [1, 2, 3], [3, 4, 5]
diff_square_sum = 0
for i in range(len(v1)):
diff_square_sum += (v1[i] - v2[i]) ** 2
e_distance = diff_square_sum ** 0.5
print(f'euclidean distance between v1 and v2: {e_distance}')
euclidean distance between v1 and v2: 3.4641016151377544
Exercise.1 - 58: Mean Squared Error(3)¶
For loop을 이용해 MSE를 연산하는 방법에 대해 복습합니다.
((예측값 - 실제값)제곱의 합)의 평균
predictions = [10, 20, 30]
labels = [10, 25, 40]
n_data = len(predictions)
diff_square_sum = 0
for data_idx in range(n_data):
diff_square_sum += (predictions[data_idx] - labels[data_idx]) ** 2
mes = diff_square_sum / n_data
print(f'MES: {mes}')
MES: 41.666666666666664
Exercise.1 - 59: 숫자 빈도 구하기¶
For loop을 이용해 숫자의 출현 빈도를 구하는 방법을 배웁니다.
loop variable index로 사용하는 예제를 접해봅니다.
numbers = [0, 2, 4, 2, 1, 4, 3, 1, 2, 3, 4, 1, 2, 3, 4]
number_cnt = [0, 0, 0, 0, 0]
for num in numbers:
number_cnt[num] += 1
print(number_cnt)
[1, 3, 4, 3, 4]
Exercise.1 - 61: 합격/불합격 알려주기¶
If~ else ~의 사용법을 익혀봅니다.
score = 40
cutoff = 50
if score > cutoff:
print("You got it!")
else:
print("You didn't get it!")
You didn't get it!
Exercise.1 - 62: 초를 분초로 표현하기¶
If ~ else ~의 사용법을 익혀봅니다.
seconds = 30
if seconds >= 60:
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
else:
hours = 0
minutes = 0
print(f"{hours:02}:{minutes:02}:{seconds:02}")
00:00:30
Exercise.1 - 64: 홀수/짝수 구하기¶
If~ else ~의 사용법을 익혀봅니다.
number = 123
if number % 2 == 0:
print("even")
else:
print("odd")
odd
Exercise.1 - 65: 두 수 비교하기¶
If~ elff ~ else ~의 사용법을 익혀봅니다.
num1, num2 = 10, 9
if num1 > num2:
print("num1 is greater than num2")
elif num1 < num2:
print("num1 is less than num2")
else:
print("num1 is equal to num2")
num1 is greater than num2
Exercise.1 - 66: 성적으로 평점으로 바꾸기¶
If~elif ~ else ~의 사용법을 익혀봅니다.
score = 41
if score > 80:
grade = 'A'
elif score > 60:
grade = 'B'
elif score > 40:
grade = 'C'
else:
grade = 'F'
print(f'Grade: {grade}')
Grade: C
Exercise.1 - 67: 합격/불합격 알려주기(2)¶
For loop와 if statement를 함께 사용하는 방법을 익혀봅니다.
scores = [20, 50, 10, 60, 70]
cutoff = 49
for score in scores:
if score > cutoff:
print('Pass')
else:
print('Try again')
Try again Pass Try again Pass Pass
Exercise.1 - 68: 성적으로 평점으로 바꾸기(2)¶
For loop와 if statement를 함께 사용하는 방법을 익혀봅니다.
scores = [20, 50, 10, 60, 90, 39, 40, 41, 59, 60, 61, 79, 80, 81]
grades = []
for score in scores:
if score > 80:
grades.append("A")
elif score > 60:
grades.append("B")
elif score > 40:
grades.append("C")
else:
grades.append("F")
print(grades)
['F', 'C', 'F', 'C', 'A', 'F', 'F', 'C', 'C', 'C', 'B', 'B', 'B', 'A']
Exercise.1 - 69: 합격/불합격 학생들의 평균 구하기¶
For loop와 if statement를 함께 사용하는 방법을 익혀봅니다.
scores = [20, 50, 10, 60, 90]
cutoff = 50
p_score_sum, n_p = 0, 0
np_score_sum, n_np = 0, 0
for score in scores:
if score > cutoff:
p_score_sum += score
n_p += 1
else:
np_score_sum += score
n_np += 1
p_score_avg = p_score_sum / n_p
np_score_avg = np_score_sum / n_np
print(f'mean of passed scores : {p_score_avg}')
print(f'mean of failed scores : {np_score_avg}')
mean of passed scores : 75.0 mean of failed scores : 26.666666666666668
Exercise.1 - 70: 홀수/짝수 구하기(2)¶
For loop와 if statement를 함께 사용하는 방법을 익혀봅니다.
numbers = []
for num in range(20):
numbers.append(num)
numbers.append(3.14)
print(numbers)
for num in numbers:
if num % 2 == 0:
print('Even Number')
elif num % 2 == 1:
print('Odd Number')
else:
print('---- Not a Integer -----')
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 3.14] Even Number Odd Number Even Number Odd Number Even Number Odd Number Even Number Odd Number Even Number Odd Number Even Number Odd Number Even Number Odd Number Even Number Odd Number Even Number Odd Number Even Number Odd Number ---- Not a Integer -----
Exercise.1 - 71: n배수들의 합 구하기¶
For loop와 if statement를 함께 사용하는 방법을 익혀봅니다.
multiple_of = 3
numbers = []
for num in range(100):
numbers.append(num)
sum_multiple_of_n = 0
for num in numbers:
if num % multiple_of == 0:
sum_multiple_of_n += num
print(sum_multiple_of_n)
1683
Exercise.1 - 72: 최댓값, 최솟값 구하기¶
For loop와 if statement를 사용하여 최댓값과 최솟값을 구하는 과정에 대해 배워봅니다.
scores = [60, 40, 70, 20, 30]
M, m = 0, 100
for score in scores:
if score > M:
M = score
if score < m:
m = score
print(f'Max value: {M}')
print(f'Min value: {m}')
Max value: 70 Min value: 20
Exercise.1 - 73: 최댓값, 최솟값 구하기(2)¶
Exercise.0 - 72에서 문제가 발생할 수 있는 부분을 생각해보고, 이를 해결하는 방법을 배워봅니다. 프로그래밍을 할 때 사소한 부분에서 버그가 발생할 수 있음을 이해합니다.
scores = [60, 40, 70, 20, 30]
# method.1
M, m = scores[0], scores[0]
for score in scores:
if score > M:
M = score
elif score < m:
m = score
print(f'Max value: {M}')
print(f'Min value: {m}')
# method.2
M, m = None, None
for score in scores:
if M == None or score > M:
M = score
if m == None or score < m:
m = score
print(f'Max value: {M}')
print(f'Min value: {m}')
Max value: 70 Min value: 20 Max value: 70 Min value: 20
Exercise.1 - 74: Min-max Normalization¶
최댓값, 최솟값을 이용하여 data를 normalization하는 방법에 대해 배워봅니다.
정규화 방법중 하나
정규화 예시) Normalization, Standardization, Regularization
scores: list[float] = [-20, 60, 40, 70, 120]
M, m = max(scores), min(scores)
print(f'M = {M}, m = {m}')
for score_idx in range(len(scores)):
scores[score_idx] = (scores[score_idx] - m) / (M - m)
print(f'scores after normalization: {scores}')
M, m = max(scores), min(scores)
print(f'scores : {scores}')
print(f'M = {M}, m = {m}')
M = 120, m = -20 scores after normalization: [0.0, 0.5714285714285714, 0.42857142857142855, 0.6428571428571429, 1.0] scores : [0.0, 0.5714285714285714, 0.42857142857142855, 0.6428571428571429, 1.0] M = 1.0, m = 0.0
Exercise.1 - 75: 최댓값, 최솟값의 위치 구하기¶
최댓값, 최솟값을 구하는 방식을 확장하여 최댓값, 최솟값의 위치를 구하는 방법을 배워봅니다.
scores = [60, -20, 40, 120, 70]
M, m = None, None
M_idx, m_idx = 0, 0
for score_idx in range(len(scores)):
score = scores[score_idx]
if M == None or score > M:
M = score
M_idx = score_idx
if m == None or score < m:
m = score
m_idx = score_idx
print(f'M/M_idx: {M} {M_idx}')
print(f'm/m_idx: {m} {m_idx}')
M/M_idx: 120 3 m/m_idx: -20 1
Exercise.1 - 76: Sorting(선택정렬)¶
최댓값, 최댓값의 위치를 이용하여 List를 정렬하는 방법에 대해 배워봅니다.
scores = [40, 20, 30, 10, 50]
sorted_scores = []
for _ in range(len(scores)):
M, M_idx = scores[0], 0
for score_idx in range(len(scores)):
if scores[score_idx] > M:
M, M_idx = scores[score_idx], score_idx
print(f'M/M_idx = {M}/{M_idx}')
tmp_scores = []
for score_idx in range(len(scores)):
if score_idx == M_idx:
sorted_scores.append(scores[score_idx])
else:
tmp_scores.append(scores[score_idx])
scores = tmp_scores
print(f'remaning scores: {scores}')
print(f'sorted scores: {sorted_scores}')
'새싹 > TIL' 카테고리의 다른 글
[핀테커스] 230922 python 수학 실습 (0) | 2023.09.22 |
---|---|
[핀테커스] 230921 python 수학 실습 (1) | 2023.09.21 |
[핀테커스] 230919 python 수학 실습 (1) | 2023.09.19 |
[핀테커스] 230918 데이터 시각화 라이브러리 실습 (1) | 2023.09.18 |
[핀테커스] 230915 미니프로젝트 (0) | 2023.09.15 |