Python/Pandas&Numpy

Numpy fundamentals 2/2

metamong 2023. 1. 16.

πŸŽ… numpyλŠ” ν•„μˆ˜μ μœΌλ‘œ μ•Œμ•„μ•Ό ν•˜λŠ” library둜, μˆ˜ν•™ μ—°μ‚° κ΄€λ ¨ν•΄μ„œ λ°˜λ“œμ‹œ μ“°μ΄λŠ” library. μ˜›λ‚  ν¬μŠ€νŒ…μ— μ΄μ–΄μ„œ numpy κ΄€λ ¨ λ‹€μ–‘ν•œ κΈ°λŠ₯을 μ΅ν˜€λ³΄μž!

 

 

NumPy intro. + fundamentals 1/2

1. κ°œλ… * NumPy = Numerical Python - Pythonμ—μ„œ λŒ€κ·œλͺ¨ 닀차원 배열을 λ‹€λ£° 수 있게 λ„μ™€μ£ΌλŠ” library * λ°μ΄ν„°μ˜ λŒ€λΆ€λΆ„μ€ 숫자 λ°°μ—΄λ‘œ λ³Ό 수 μžˆκΈ°μ— NumPyλŠ” κΌ­ ν•„μš” - μ΄λ―Έμ§€λ‚˜ μ†Œλ¦¬ λ“± μ‹€μƒν™œ λŒ€λΆ€λΆ„μ΄ 숫

sh-avid-learner.tistory.com


1. numpy λ°°μ—΄ 생성 λ‹€μ–‘ν•œ ν•¨μˆ˜

* λ°°μ—΄ 생성 및 μ΄ˆκΈ°ν™” ν•¨μˆ˜

πŸŽ… zeros() - 주어진 ν˜•νƒœμ™€ νƒ€μž…μ„ κ°–λŠ” 0으둜 μ±„μ›Œμ§„ λ°°μ—΄ λ°˜ν™˜ (μ§€μ •λœ shape 배열을 μƒμ„±ν•˜κ³  λͺ¨λ“  μš”μ†Œλ₯Ό 0으둜 μ΄ˆκΈ°ν™”)

np.zeros(shape,dtype=float,order='C')

 

a=np.zeros(10)
a 
#array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

b=np.zeros((3,3))
b
'''
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
'''

 

πŸŽ… ones() - 주어진 ν˜•νƒœμ™€ νƒ€μž…μ„ κ°–λŠ” 1둜 μ±„μ›Œμ§„ λ°°μ—΄ λ°˜ν™˜ (μ§€μ •λœ shape의 λ°°μ—΄ μƒμ„±ν•˜κ³  λͺ¨λ“  μš”μ†Œ 1둜 μ΄ˆκΈ°ν™”)

np.ones(shape, dtype=None, order='C')

 

c=np.ones(5)
c
#array([1., 1., 1., 1., 1.])

d=np.ones((2,3))
d
'''
array([[1., 1., 1.],
       [1., 1., 1.]])
'''

 

πŸŽ… full() - λͺ¨λ“  μš”μ†Œλ₯Ό μ§€μ •ν•œ κ°’μœΌλ‘œ μ΄ˆκΈ°ν™” (μ§€μ •λœ shape의 배열을 μƒμ„±ν•˜κ³ , λͺ¨λ“  μš”μ†Œλ₯Ό fill_value둜 μ΄ˆκΈ°ν™”)

np.full(shape, fill_value, dtype=None, order='C')

 

e=np.full((3,4),1.5)
e

'''
array([[1.5, 1.5, 1.5, 1.5],
       [1.5, 1.5, 1.5, 1.5],
       [1.5, 1.5, 1.5, 1.5]])
'''

 

πŸŽ… eye() - λ‹¨μœ„ ν–‰λ ¬(정사각행렬) 생성
(* 정사각행렬 - μ£ΌλŒ€κ°μ„ μ˜ μ›μ†Œκ°€ λͺ¨λ‘ 1이고, λ‚˜λ¨Έμ§€ μ›μ†ŒλŠ” λͺ¨λ‘ 0인 μ •μ‚¬κ°ν˜• ν–‰λ ¬)

np.eye(N,M=None, k=0, dtype=<class 'float'>)

 

f = np.eye(3)
f
'''
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
'''

* μƒμ„±ν•œ κ°’μœΌλ‘œ 배열을 μƒμ„±ν•˜λŠ” ν•¨μˆ˜

πŸŽ… arange() - startλΆ€ν„° stop λ―Έλ§ŒκΉŒμ§€ step κ°„κ²©μœΌλ‘œ 데이터λ₯Ό μƒμ„±ν•œ ν›„ 배열을 λ§Œλ“¦. λ²”μœ„ λ‚΄μ—μ„œ 간격을 κΈ°μ€€ κ· λ“± κ°„κ²©μ˜ λ°°μ—΄ μš”μ†Œμ˜ κ°œμˆ˜κ°€ μ•„λ‹Œ, λ°μ΄ν„°μ˜ 간격을 κΈ°μ€€μœΌλ‘œ λ°°μ—΄ 생성

numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)

 

a=np.arange(1,21)
a
#array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

a=np.arange(1,21,5)
a
#array([ 1,  6, 11, 16])

 

πŸŽ… linspace() - startλΆ€ν„° stop의 λ²”μœ„μ—μ„œ num개λ₯Ό κ· μΌν•œ κ°„κ²©μœΌλ‘œ 데이터λ₯Ό μƒμ„±ν•˜κ³ , 배열을 λ§Œλ“œλŠ” ν•¨μˆ˜, λ°°μ—΄ μš”μ†Œ 개수λ₯Ό κΈ°μ€€μœΌλ‘œ κ· λ“± 간격 λ°°μ—΄ 생성

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

 

b=np.linspace(1,101,5)
b #array([  1.,  26.,  51.,  76., 101.])

 

πŸŽ… logspace() - log scale의 linspace ν•¨μˆ˜. log scale둜 μ§€μ •λœ λ²”μœ„μ—μ„œ num 개수만큼 κ· λ“± κ°„κ²©μœΌλ‘œ 데이터λ₯Ό μƒμ„±ν•˜κ³  λ°°μ—΄ λ§Œλ“¦

numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)

 

c=np.logspace(np.log10(10),np.log10(100),5)
c
#array([ 10.        ,  17.7827941 ,  31.6227766 ,  56.23413252, 100.        ])

d=np.logspace(1,2,5)
d
#array([ 10.        ,  17.7827941 ,  31.6227766 ,  56.23413252, 100.        ])

* λ‚œμˆ˜ 기반 λ°°μ—΄ 생성

πŸ‘• np library의 random module의 μ—¬λŸ¬ ν•¨μˆ˜λ₯Ό μ‚¬μš©!

 

πŸ‘• random() - κ· λ“±λΆ„ν¬μ˜ 0 ~ 1μ‚¬μ΄μ—μ„œ λžœλ€ν•˜κ²Œ μΆ”μΆœ

e=np.random.random()
e
#0.29012953071795633


f=np.random.random((4,4))
f
'''
array([[0.75228104, 0.44754325, 0.59295188, 0.52459755],
       [0.29038476, 0.26463718, 0.90001467, 0.67435909],
       [0.35206165, 0.94948684, 0.05609491, 0.75023364],
       [0.84575654, 0.08500627, 0.93769984, 0.85128577]])
'''

 

πŸ‘• randint(a,b) - aλΆ€ν„° bμ‚¬μ΄μ˜(a이상 bμ΄ν•˜) λžœλ€ν•œ μ •μˆ˜ μΆ”μΆœ

g=np.random.randint(1,10)
g #7

g=np.random.randint(1,10, (4,4))
g
'''
array([[3, 6, 2, 6],
       [5, 2, 8, 8],
       [3, 1, 7, 8],
       [3, 7, 2, 9]])
'''

 

πŸ‘• rand() - 주어진 shape에 맞게 각 element에 λžœλ€ν•œ μ›μ†Œ μΆ”μΆœ

h=np.random.rand(3,3)
h
'''
array([[0.63293408, 0.47357913, 0.49762324],
       [0.57856465, 0.13234599, 0.45052694],
       [0.17399688, 0.31530116, 0.58504013]])
'''

 

πŸ‘• randn() - μœ„ rand()와 λ˜‘κ°™μœΌλ‚˜, ν‘œμ€€μ •κ·œλΆ„ν¬μ—μ„œ λžœλ€ν•œ μ›μ†Œλ₯Ό μΆ”μΆœ

i=np.random.randn(3,3)
i
'''
array([[-0.44726986, -0.48241921, -0.17961261],
       [ 1.63555379,  0.93241279, -0.071047  ],
       [ 0.36149075,  0.48039173, -1.01427038]])
'''

 

πŸ‘• normal() - κ°€μš°μ‹œμ•ˆ(μ •κ·œ) λΆ„ν¬μ—μ„œ λžœλ€ν•œ 수λ₯Ό μΆ”μΆœ (μΆ”ν›„ 톡계 ν¬μŠ€νŒ…μ—μ„œ λ§Œλ‚˜λ΄„!)

2. numpy λ°°μ—΄ 속성

πŸ‘“ ndim - λ°°μ—΄ 차원 수 λ˜λŠ” λ°°μ—΄μ˜ μΆ• 수

arr=np.arange(1,6)
print(arr.ndim) #1

 

πŸ‘“ shape - λ°°μ—΄ 각 μ°¨μ›μ˜ 크기λ₯Ό tuple ν˜•νƒœλ‘œ ν‘œν˜„

arr.shape #(5,)

 

πŸ‘“ size - λ°°μ—΄ μ›μ†Œμ˜ 개수

arr.size #5

 

πŸ‘“ dtype - λ°°μ—΄ λ‚΄μ˜ μ›μ†Œμ˜ data type

arr.dtype #dtype('int32')

 

πŸ‘“ itemsize - λ°°μ—΄ λ‚΄μ˜ μ›μ†Œμ˜ 크기λ₯Ό byte λ‹¨μœ„λ‘œ 기술

arr.itemsize
#4

 

πŸ‘“ nbytes - λ°°μ—΄ 전체 byte

arr.nbytes #20

3. numpy λ°°μ—΄ 쑰회

β€» numpy 배열도 python list처럼 indexing & slicing λͺ¨λ‘ κ°€λŠ₯ β€»

arr2d=np.arange(9).reshape((3,3))

arr2d
'''
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
'''
arr2d[0,1] #1
arr2d[0] #array([0, 1, 2])

arr2d[0,0]=10
arr2d
'''
array([[10,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8]])
'''

a2=np.arange(1,25).reshape((4,6))
a2

a2[1] #array([ 7,  8,  9, 10, 11, 12])

a2[1:3,1:5]
'''
array([[ 8,  9, 10, 11],
       [14, 15, 16, 17]])
'''

a2[:3,:]
'''
array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12],
       [13, 14, 15, 16, 17, 18]])
'''

a2[:3,::-1]
'''
array([[ 6,  5,  4,  3,  2,  1],
       [12, 11, 10,  9,  8,  7],
       [18, 17, 16, 15, 14, 13]])
'''

a2[::-1,::-1]
'''
array([[24, 23, 22, 21, 20, 19],
       [18, 17, 16, 15, 14, 13],
       [12, 11, 10,  9,  8,  7],
       [ 6,  5,  4,  3,  2,  1]])
'''

#boolean indexing
a=np.arange(1,6)
b=[True, False, False, True, False]
a[b] #array([1, 4])

a = np.arange(11)
a[a<5] #array([0, 1, 2, 3, 4])
a[(a>5) & (a%2==0)] #array([ 6,  8, 10])
a[~(a%3==0)] #array([ 1,  2,  4,  5,  7,  8, 10])

* 좜처)

- κ°•μ˜λͺ…: 2023 DAS(λ””μ§€ν„Έμ• λ„λ¦¬ν‹±μŠ€μœ΅ν•©κ³Όμ •) μ‚¬μ „κ΅μœ‘ - Python 기초

- ꡐ수자: 이정원

 

λŒ“κΈ€