티스토리 뷰

beginner/파이썬 기초

NumPy_함수

johh 2019. 2. 13. 13:19
2019.02.13-3

Numpy 항목별 적용하는 함수(ufunc)

https://docs.scipy.org/doc/numpy-1.15.1/reference/ufuncs.html
설명x, 밑에보면 다양한 함수들이 나와있다.

136p

In [7]:
import numpy as np
np.floor(1.1), np.ceil(1.1), np.floor(1.0), np.ceil(1.0)
Out[7]:
(1.0, 2.0, 1.0, 1.0)
In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
a = np.arange(9).reshape(3,3)
a
Out[2]:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [37]:
np.square(a)
Out[37]:
array([[ 0,  1,  4],
       [ 9, 16, 25],
       [36, 49, 64]], dtype=int32)
In [38]:
a ** 2
Out[38]:
array([[ 0,  1,  4],
       [ 9, 16, 25],
       [36, 49, 64]], dtype=int32)
In [39]:
np.sqrt(a)
Out[39]:
array([[0.        , 1.        , 1.41421356],
       [1.73205081, 2.        , 2.23606798],
       [2.44948974, 2.64575131, 2.82842712]])
In [40]:
a ** 0.5
Out[40]:
array([[0.        , 1.        , 1.41421356],
       [1.73205081, 2.        , 2.23606798],
       [2.44948974, 2.64575131, 2.82842712]])
In [41]:
np.exp(a)
Out[41]:
array([[1.00000000e+00, 2.71828183e+00, 7.38905610e+00],
       [2.00855369e+01, 5.45981500e+01, 1.48413159e+02],
       [4.03428793e+02, 1.09663316e+03, 2.98095799e+03]])
In [42]:
np.log(a)
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log
  """Entry point for launching an IPython kernel.
Out[42]:
array([[      -inf, 0.        , 0.69314718],
       [1.09861229, 1.38629436, 1.60943791],
       [1.79175947, 1.94591015, 2.07944154]])
In [43]:
np.add(a, 1)
Out[43]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [8]:
np.multiply(a,2)
Out[8]:
array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16]])
In [12]:
np.maximum(a,5)  # a와 5중에서 큰 값을 적용
Out[12]:
array([[5, 5, 5],
       [5, 5, 5],
       [6, 7, 8]])
In [13]:
b = np.random.randn(5,5)
b
Out[13]:
array([[ 1.84040151,  0.58797762,  0.44700583, -1.04478567, -0.31753599],
       [-0.58139898, -0.7652754 , -0.13110135,  0.44198006, -1.074118  ],
       [-0.9413967 , -0.12910911, -0.7348941 , -0.73010516, -0.44198176],
       [ 0.33013289,  0.63181851, -1.71341018, -1.80935023, -0.83327047],
       [-0.95068979, -1.13730934,  1.76726291, -0.40009534,  0.18189717]])
In [14]:
np.maximum(b,0)
Out[14]:
array([[1.84040151, 0.58797762, 0.44700583, 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.44198006, 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.33013289, 0.63181851, 0.        , 0.        , 0.        ],
       [0.        , 0.        , 1.76726291, 0.        , 0.18189717]])
In [44]:
np.abs(b)
Out[44]:
array([[1.84040151, 0.58797762, 0.44700583, 1.04478567, 0.31753599],
       [0.58139898, 0.7652754 , 0.13110135, 0.44198006, 1.074118  ],
       [0.9413967 , 0.12910911, 0.7348941 , 0.73010516, 0.44198176],
       [0.33013289, 0.63181851, 1.71341018, 1.80935023, 0.83327047],
       [0.95068979, 1.13730934, 1.76726291, 0.40009534, 0.18189717]])
In [45]:
np.sign(b)
Out[45]:
array([[ 1.,  1.,  1., -1., -1.],
       [-1., -1., -1.,  1., -1.],
       [-1., -1., -1., -1., -1.],
       [ 1.,  1., -1., -1., -1.],
       [-1., -1.,  1., -1.,  1.]])
In [46]:
c = np.arange(100)
plt.plot(c)
Out[46]:
[<matplotlib.lines.Line2D at 0xf119e78eb8>]
In [47]:
c = np.arange(10)
d = np.power(c,2)
plt.plot(c)
plt.plot(d)
Out[47]:
[<matplotlib.lines.Line2D at 0xf119f57b38>]
In [17]:
c=np.arange(10)
d=np.log(c+1)
plt.plot(c)
plt.plot(d)
Out[17]:
[<matplotlib.lines.Line2D at 0xf119fe8ef0>]
In [22]:
c=np.arange(0.0001,10,0.0001)
d=np.log(c)
plt.plot(c)
plt.plot(d)
Out[22]:
[<matplotlib.lines.Line2D at 0xf117d85f60>]
In [48]:
c = np.arange(0.01, 1, 0.01)
d = 1/c
plt.plot(c)
plt.plot(d)
Out[48]:
[<matplotlib.lines.Line2D at 0xf119fa3668>]
In [24]:
c = np.arange(-10,10)           # 어떤 값보다 클때만 신호를 주고 싶을때
d = np.maximum(c,0)

plt.plot(c)
plt.plot(d)
Out[24]:
[<matplotlib.lines.Line2D at 0xf117df90b8>]
In [27]:
rng = np.arange(-5,5,0.1)
sigmoid = 1/(1+np.exp(-rng))

plt.plot(rng, sigmoid)
Out[27]:
[<matplotlib.lines.Line2D at 0xf119d1c5c0>]
In [26]:
rng = np.arange(-5,5,0.1)
sigmoid = 1/(1+np.exp(-rng))

plt.plot(rng, sigmoid)
plt.hlines([0,0.5,1],-5,5, linestyles=':')
plt.vlines([0],0,1, linestyles='--')
Out[26]:
<matplotlib.collections.LineCollection at 0xf117e5fac8>

-5~5 사이에 0.1 간격으로 숫자를 뽑아낸다.

In [35]:
np.arange(-5,5,0.1) #간격 0.1
Out[35]:
array([-5.00000000e+00, -4.90000000e+00, -4.80000000e+00, -4.70000000e+00,
       -4.60000000e+00, -4.50000000e+00, -4.40000000e+00, -4.30000000e+00,
       -4.20000000e+00, -4.10000000e+00, -4.00000000e+00, -3.90000000e+00,
       -3.80000000e+00, -3.70000000e+00, -3.60000000e+00, -3.50000000e+00,
       -3.40000000e+00, -3.30000000e+00, -3.20000000e+00, -3.10000000e+00,
       -3.00000000e+00, -2.90000000e+00, -2.80000000e+00, -2.70000000e+00,
       -2.60000000e+00, -2.50000000e+00, -2.40000000e+00, -2.30000000e+00,
       -2.20000000e+00, -2.10000000e+00, -2.00000000e+00, -1.90000000e+00,
       -1.80000000e+00, -1.70000000e+00, -1.60000000e+00, -1.50000000e+00,
       -1.40000000e+00, -1.30000000e+00, -1.20000000e+00, -1.10000000e+00,
       -1.00000000e+00, -9.00000000e-01, -8.00000000e-01, -7.00000000e-01,
       -6.00000000e-01, -5.00000000e-01, -4.00000000e-01, -3.00000000e-01,
       -2.00000000e-01, -1.00000000e-01, -1.77635684e-14,  1.00000000e-01,
        2.00000000e-01,  3.00000000e-01,  4.00000000e-01,  5.00000000e-01,
        6.00000000e-01,  7.00000000e-01,  8.00000000e-01,  9.00000000e-01,
        1.00000000e+00,  1.10000000e+00,  1.20000000e+00,  1.30000000e+00,
        1.40000000e+00,  1.50000000e+00,  1.60000000e+00,  1.70000000e+00,
        1.80000000e+00,  1.90000000e+00,  2.00000000e+00,  2.10000000e+00,
        2.20000000e+00,  2.30000000e+00,  2.40000000e+00,  2.50000000e+00,
        2.60000000e+00,  2.70000000e+00,  2.80000000e+00,  2.90000000e+00,
        3.00000000e+00,  3.10000000e+00,  3.20000000e+00,  3.30000000e+00,
        3.40000000e+00,  3.50000000e+00,  3.60000000e+00,  3.70000000e+00,
        3.80000000e+00,  3.90000000e+00,  4.00000000e+00,  4.10000000e+00,
        4.20000000e+00,  4.30000000e+00,  4.40000000e+00,  4.50000000e+00,
        4.60000000e+00,  4.70000000e+00,  4.80000000e+00,  4.90000000e+00])
In [36]:
np.linspace(-5,5,101) #개수 101개
Out[36]:
array([-5. , -4.9, -4.8, -4.7, -4.6, -4.5, -4.4, -4.3, -4.2, -4.1, -4. ,
       -3.9, -3.8, -3.7, -3.6, -3.5, -3.4, -3.3, -3.2, -3.1, -3. , -2.9,
       -2.8, -2.7, -2.6, -2.5, -2.4, -2.3, -2.2, -2.1, -2. , -1.9, -1.8,
       -1.7, -1.6, -1.5, -1.4, -1.3, -1.2, -1.1, -1. , -0.9, -0.8, -0.7,
       -0.6, -0.5, -0.4, -0.3, -0.2, -0.1,  0. ,  0.1,  0.2,  0.3,  0.4,
        0.5,  0.6,  0.7,  0.8,  0.9,  1. ,  1.1,  1.2,  1.3,  1.4,  1.5,
        1.6,  1.7,  1.8,  1.9,  2. ,  2.1,  2.2,  2.3,  2.4,  2.5,  2.6,
        2.7,  2.8,  2.9,  3. ,  3.1,  3.2,  3.3,  3.4,  3.5,  3.6,  3.7,
        3.8,  3.9,  4. ,  4.1,  4.2,  4.3,  4.4,  4.5,  4.6,  4.7,  4.8,
        4.9,  5. ])

'beginner > 파이썬 기초' 카테고리의 다른 글

NumPy_정렬  (0) 2019.02.15
NumPy_파일입출력  (0) 2019.02.14
NumPy_사칙연산  (0) 2019.02.13
NumPy_랜덤-2  (0) 2019.02.13
NumPy_랜덤-1  (0) 2019.02.12
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함