Note

Numpy (30) 본문

Numpy

Numpy (30)

알 수 없는 사용자 2022. 8. 22. 19:10
728x90

How to compute the softmax score?

# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
sepallength = np.array([float(row[0]) for row in iris])

# Solution
def softmax(x):
    """Compute softmax values for each sets of scores in x.
    https://stackoverflow.com/questions/34968722/how-to-implement-the-softmax-function-in-python"""
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum(axis=0)

print(softmax(sepallength))

# output
[ 0.002  0.002  0.001  0.001  0.002  0.003  0.001  0.002  0.001  0.002
   0.003  0.002  0.002  0.001  0.004  0.004  0.003  0.002  0.004  0.002
   0.003  0.002  0.001  0.002  0.002  0.002  0.002  0.002  0.002  0.001
   0.002  0.003  0.002  0.003  0.002  0.002  0.003  0.002  0.001  0.002
   0.002  0.001  0.001  0.002  0.002  0.002  0.002  0.001  0.003  0.002
   0.015  0.008  0.013  0.003  0.009  0.004  0.007  0.002  0.01   0.002
   0.002  0.005  0.005  0.006  0.004  0.011  0.004  0.004  0.007  0.004
   0.005  0.006  0.007  0.006  0.008  0.01   0.012  0.011  0.005  0.004
   0.003  0.003  0.004  0.005  0.003  0.005  0.011  0.007  0.004  0.003
   0.003  0.006  0.004  0.002  0.004  0.004  0.004  0.007  0.002  0.004
   0.007  0.004  0.016  0.007  0.009  0.027  0.002  0.02   0.011  0.018
   0.009  0.008  0.012  0.004  0.004  0.008  0.009  0.03   0.03   0.005
   0.013  0.004  0.03   0.007  0.011  0.018  0.007  0.006  0.008  0.018
   0.022  0.037  0.008  0.007  0.006  0.03   0.007  0.008  0.005  0.013
   0.011  0.013  0.004  0.012  0.011  0.011  0.007  0.009  0.007  0.005]

'Numpy' 카테고리의 다른 글

Numpy (32)  (0) 2022.08.24
Numpy (31)  (0) 2022.08.23
Numpy (29)  (0) 2022.08.21
Numpy (28)  (0) 2022.08.20
Numpy (27)  (0) 2022.08.19
Comments