π 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 κΈ°μ΄
- κ΅μμ: μ΄μ μ
'Python > Pandas&Numpy' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
map & applymap & apply(for dataframe & Series) (1) | 2024.06.02 |
---|---|
dataframe κΎΈλ―ΈκΈ° (1) | 2023.01.22 |
pandas Tricks (Kevin by DataSchool) μλ£! COMPILATION (0) | 2022.04.18 |
pandas Tricks_14ππ» 'styling a dataframe' (Kevin by DataSchool) (0) | 2022.04.18 |
pandas Tricks_13ππ» 'change display options' (Kevin by DataSchool) (0) | 2022.04.17 |
λκΈ