'2020/07'에 해당되는 글 4건

  1. 2020.07.28 [Python][AI] Perceptron 예제
  2. 2020.07.20 [Python] fisher-yates shuffle
  3. 2020.07.16 강아지 나이 계산기
  4. 2020.07.10 [Python] 소인수 분해
Coding/Python 삽질기2020. 7. 28. 23:56

 

import numpy as np

def sigmoid(x):
    return 1/(1+np.exp(-x))


def DeltaSGD(W, X, D):
    alpha = 0.9
    dwSum = np.array([0] * 3)
    
    idx = 0
    for k in X:
        a1 = np.dot(k, W)
        y = sigmoid(a1)
        
        e = D[idx] - y
        delta = y * (1-y) * e
        
        dw = alpha * delta * k
        W += dw
        idx+=1

        
def Train(epoch, x, y, W1):
    for ep in range (epoch):
        DeltaSGD(W1, x, y)

        
def main():
    W1 = np.random.randn(3)
    x = np.array([[0, 0, 1], [0, 1, 1], [1, 1, 1], [1, 0, 0]])
    y = np.array([1, 1, 1, 0])

    Train(10000, x, y, W1)
    
    for k in x:
        a1 = np.dot(k, W1)
        z1 = sigmoid(a1)
        print(z1)    
        
        
if __name__ == '__main__':       
    main()  

 

0.9962960119369554
0.9999259998523653
0.9929612087250037
0.010332095384834877

'Coding > Python 삽질기' 카테고리의 다른 글

[Python][AI] XOR 예제  (0) 2020.08.03
[Python] fisher-yates shuffle  (0) 2020.07.20
강아지 나이 계산기  (0) 2020.07.16
Posted by chobocho
Coding/Python 삽질기2020. 7. 20. 23:27
import random

number = [1, 2, 3, 4, 5, 7, 8]

def shuffle(number):
   for i in range (len(number)-1, 0, -1):
       j = random.randint(0, i)
       number[j], number[i] = number[i], number[j]
    
shuffle(number)

 동작을 보면 아래와 같이 변화 한다

[1, 2, 3, 4, 5, 7, 8]
[1, 8, 3, 4, 5, 7, 2]
[1, 8, 3, 4, 5, 7, 2]
[1, 8, 3, 4, 5, 7, 2]
[1, 8, 3, 4, 5, 7, 2]
[3, 8, 1, 4, 5, 7, 2]
[3, 8, 1, 4, 5, 7, 2]

* 참고: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

'Coding > Python 삽질기' 카테고리의 다른 글

[Python][AI] Perceptron 예제  (0) 2020.07.28
강아지 나이 계산기  (0) 2020.07.16
[Python] 소인수 분해  (0) 2020.07.10
Posted by chobocho
Coding/Python 삽질기2020. 7. 16. 00:19

강아지 나이를 인간 나이로 바꿔주면 위와 같은 그래프가 나온다고 한다.

강아지 나이 와 인간 나이 변환표

소스코드: https://github.com/chobocho/dog_age

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] fisher-yates shuffle  (0) 2020.07.20
[Python] 소인수 분해  (0) 2020.07.10
[ChoboTimer] Version 0.17 Release  (0) 2020.04.22
Posted by chobocho
Coding/Python 삽질기2020. 7. 10. 00:07
import math

def get_prime_factor(number):
    result = []

    if number < 2:
        return result

    while number % 2 == 0:
        result.append('2')
        number /= 2

    for i in range (3, int(math.sqrt(number)) + 1, 2):
        if number % i == 0:
            result.append(str(i))
            number /= i

    if number > 2:
        result.append(str(int(number)))

    return result

def testPrimeFactor():
    assert len(get_prime_factor(-1)) == 0
    assert "2x2x3" == 'x'.join(get_prime_factor(12))
    assert "2x2x5x5" == 'x'.join(get_prime_factor(100))
    assert "67x337x2797x14251" == 'x'.join(get_prime_factor(900000001213))

if __name__ == '__main__':
    testPrimeFactor()

'Coding > Python 삽질기' 카테고리의 다른 글

강아지 나이 계산기  (0) 2020.07.16
[ChoboTimer] Version 0.17 Release  (0) 2020.04.22
[Python] venv 사용  (0) 2020.04.15
Posted by chobocho