๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[Python][AI] pandas, numpy ๊ธฐ์ดˆ ํ•จ์ˆ˜ ์ •๋ฆฌ

by ์œ ๋ฏธ๋ฏธYoomimi 2023. 10. 9.
array([[-1.5, 0.5],
        [ 1. , 0. ]])

๊ธฐ์ดˆ์ธ๊ณต์ง€๋Šฅ ์ˆ˜์—… Project๋ฅผ ํ•˜๋ฉด์„œ pandas, numpy ํ•จ์ˆ˜๋ฅผ ์—„์ฒญ ์“ฐ๋Š”๋ฐ,

์ •๋ฆฌํ•ด๋‘๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ๋„์ ๋„์  ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

 


 

[Pandas] .dropna()

Missing value๊ฐ€ ์žˆ๋Š” ํ–‰์„ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜.

     A    B  C
0  1.0  NaN  1
1  2.0  2.0  2
2  3.0  3.0  3
3  NaN  4.0  4
4  5.0  5.0  5

๋งŒ์ผ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ„์™€ ๊ฐ™์„ ๋•Œ data = data.dropna()๋ฅผ ํ•ด์ค€๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ •๋ฆฌ๋œ๋‹ค.

     A    B  C
1  2.0  2.0  2
2  3.0  3.0  3
4  5.0  5.0  5

 

 

[NumPy] concatenate()

๋ฐฐ์—ด์„ ๊ฒฐํ•ฉ, ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ํ•จ์ˆ˜.

ex) np.concatenate((arr1, arr2, ...), axis=0)

์—ฌ๊ธฐ์„œ axis๋Š” ๊ฒฐํ•ฉํ•  ์ถ•์„ ์ง€์ •ํ•œ๋‹ค. axis๊ฐ€ 0์ด๋ฉด ์ˆ˜์ง ๊ฒฐํ•ฉ, 1์ด๋ฉด ์ˆ˜ํ‰ ๊ฒฐํ•ฉ์„ ํ•œ๋‹ค.

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
result = np.concatenate((arr1, arr2), axis=1)

์ด๋ ‡๊ฒŒ ํ•œ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ–‰ ์ˆ˜๊ฐ€ ๋งž์ง€ ์•Š์•„๋„ 0, 0์œผ๋กœ ์ฑ„์šด๋‹ค.

array([[1, 2, 5, 6],
       [3, 4, 0, 0]])

๋งŒ์ผ ์ด๋•Œ axis=0์„ ํ•˜๋ฉด ๋‘ arr์˜ ํ–‰ ์ˆ˜๊ฐ€ ๋งž์ง€ ์•Š์•„์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.

 

์—ด์„ ๋งž์ถ”๊ณ  axis=0์œผ๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค๋ฉด?

arr2 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.concatenate((arr1, arr2), axis=0)

์ด๋ ‡๊ฒŒ ์—ด์ด ๊ฐ™๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])

 

 

[NumPy] linalg.inv()

์—ญํ–‰๋ ฌ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค.

a = np.array(range(4)).reshape(2, 2)

๊ทธ๋Ÿฌ๋ฉด a๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

[[0 1]

 [2 3]]

a์˜ ์—ญํ–‰๋ ฌ์„ ๊ตฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

a_inv = np.linalg.inv(a)
([[-1.5, 0.5],
  [ 1. , 0. ]])

 

[NumPy] dot()

๋‚ด์ ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜.