전체 글
-
무인도 여행코딩 테스트/Level 2 2023. 1. 28. 13:22
https://school.programmers.co.kr/learn/courses/30/lessons/154540 def solution(maps): def solve(start_y, start_x): que = [(start_y, start_x)] food = 0 while que: pos_y, pos_x = que.pop() if (pos_y, pos_x) not in visited: visited.add((pos_y, pos_x)) food += int(maps[pos_y][pos_x]) for diff_x, diff_y in ((1, 0), (-1, 0), (0, 1), (0, -1)): new_x, new_y = pos_x + diff_x, pos_y + diff_y if 0
-
지형 이동코딩 테스트/Level 4 2023. 1. 25. 11:38
https://school.programmers.co.kr/learn/courses/30/lessons/62050 queue를 이용. from heapq import heappop, heappush def solution(land, height): answer = 0 que = [(0, 0, 0)] visited = set() while que: p, y, x = heappop(que) if (y, x) in visited: continue visited.add((y, x)) answer += p for dy, dx in ((0, 1), (0, -1), (1, 0), (-1, 0)): next_y, next_x = y + dy, x + dx if not (len(land) > next_y >= 0 and..
-
파이썬 날코딩: XOR 문제ML 2023. 1. 21. 23:13
논리 연산으로.. def AND(x1, x2): return x1 and x2 def NAND(x1, x2): return not (x1 and x2) def OR(x1, x2): return x1 or x2 def XOR(x1, x2): return AND(NAND(x1, x2), OR(x1, x2)) for each in ((False, False), (True, False), (False, True), (True, True)): print(each, XOR(*each)) 가중치와 바이어스를 이용해.. def MLP(x1, x2, w, b): if w * x1 + w * x2 + b > 0: return 1 return 0 def AND(x1, x2): return MLP(x1, x2, 1, -1) ..
-
파이썬 날코딩: 경사하강법(gradient descent), 다중 선형 회귀ML 2023. 1. 21. 22:01
경사하강법은 가설 함수의 기울기(가중치)와 절편(편향)을 찾는 중 하나. 이를 옵티마이저라고 한다. 순수 파이썬으로.. x1 = [2, 4, 6, 8] x2 = [0, 4, 2, 3] y = [81, 93, 91, 97] a1 = a2 = b = 0 lr = 0.01 epochs = 2001 for i in range(epochs): pred_y = [a1 * each_x1 + a2 * each_x2 + b for each_x1, each_x2 in zip(x1, x2)] error = [each_y - each_pred for each_y, each_pred in zip(y, pred_y)] a1_diff = 2 / len(x1) * sum(-each_x1 * each_error for each_x1,..
-
파이썬 날코딩: 경사하강법(gradient descent), 선형 회귀ML 2023. 1. 21. 21:43
경사하강법은 가설 함수의 기울기(가중치)와 절편(편향)을 찾는 중 하나. 이를 옵티마이저라고 한다. 단순 선형 회귀 순수 파이썬으로.. x = (2, 4, 6, 8) y = (81, 93, 91, 97) a = b = 0 lr = 0.03 epochs = 2001 for i in range(epochs): pred_y = [a * each_x + b for each_x in x] error = [each_y - each_pred_y for each_y, each_pred_y in zip(y, pred_y)] a_diff = 2 / len(x) * sum(-each_x * each_error for each_x, each_error in zip(x, error)) b_diff = 2 / len(x) * s..
-
파이썬 날코딩: 평균제곱오차(mean square error: MSE)ML 2023. 1. 21. 21:23
모두의 딥러닝을 읽고 있습니다. 책의 내용을 numpy를 쓰지 않고 코딩해 보았습니다. x = (2, 4, 6, 8) y = (81, 93, 91, 97) fake_a = 3 fake_b = 76 pred_y = [fake_a * each_x + fake_b for each_x in x] for each_x, each_y, each_pred in zip(x, y, pred_y): print(f'공부시간 {each_x}, 실제점수 {each_y}, 예측점수 {each_pred}') mse = 1 / len(y) * sum((each_y - each_pred) ** 2 for each_y, each_pred in zip(y, pred_y)) print(mse) 넘파이로... import numpy as np..
-
파이썬 날코딩: 최소제곱법(method of least squares)ML 2023. 1. 21. 20:38
모두의 딥러닝을 읽고 있습니다. 책의 내용을 numpy를 쓰지 않고 코딩해 보았습니다. # 최소제곱법 x = [2, 4, 6, 8] y = [81, 93, 91, 97] mean_x = sum(x) / len(x) mean_y = sum(y) / len(y) print(f'x의 평균: {mean_x}') print(f'y의 평균: {mean_y}') dividend = sum((each_x - mean_x) * (each_y - mean_y) for each_x, each_y in zip(x, y)) divisor = sum((each_x - mean_x) ** 2 for each_x in x) print(f'분자: {dividend}') print(f'분모: {divisor}') a = dividend..
-
인사고과코딩 테스트/Level 3 2023. 1. 20. 15:15
https://school.programmers.co.kr/learn/courses/30/lessons/152995 def solution(scores): wh_sum = sum(scores[0]) max_s1 = answer = 0 for s in sorted(scores, key=lambda x: (-x[0], x[1])): if max_s1 wh_sum: answer += 1 elif s == scores[0]: return -1 return answer + 1 테스트 1 〉통과 (0.00ms, 10.2MB) 테스트 2 〉통과 (0.01ms, 10.2MB) 테스트 3 〉통과 (0.01ms, 10.2MB) 테스트 4 〉통과 (0.01ms, 10.1MB) 테스트 5 〉통과 (0.01ms, 10.1MB)..