๋ฐ์ดํ„ฐ ๋ถ„์„/Today I learned :

Numpy ๊ธฐ์ดˆ

์ฃผ์˜ ๐Ÿฑ 2021. 9. 18. 17:58
728x90
๋ฐ˜์‘ํ˜•

๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์—ด์˜ ๊ตฌ์กฐ,  ๋‹ค์ฐจ์› ๋ฐฐ์—ด, ๋ฐฐ์—ด ๊ฐ„ ์—ฐ์‚ฐ, ์ •๋ ฌ ๋“ฑ ๊ฐ€๋Šฅ

 

๋ฐฐ์—ด ๊ฐ์ฒด ndarray

 

import numpy as np

data = np.array([1,2,3,4,5])
print(data)
print(type(data))
print(data.dtype)

์‹คํ–‰ ๊ฒฐ๊ณผ

 

[1 2 3 4 5]    โ˜†์‰ผํ‘œ ์—†์Œ!!!!

<class 'numpy.ndarray'>

int32

 

๋ฐฐ์—ด ๊ฐ์ฒด ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• : array() ๋ฉ”์†Œ๋“œ 

 

2์ฐจ์›(2ํ–‰ 3์—ด) ๋žœ๋ค ์ˆ˜ ์ƒ์„ฑ : random.randn() ๋ฉ”์†Œ๋“œ 

 

import numpy as np

data = np.random.randn(2,3)
print(data)
print(data.shape)
print(data.dtype)

 

[[1.275644478 -0.0237722 1.0475675]

 [-0.86039567 1.2096523 1.33460799]]

(2,3)

float64

 

shape(ํ–‰ ์ˆ˜, ์—ด ์ˆ˜)

 

ndarray ๊ฐ์ฒด ์š”์†Œ์˜ ๊ฐ’์„ ๋ชจ๋‘ 0์œผ๋กœ ์ดˆ๊ธฐํ™” : zeros() ๋ฉ”์†Œ๋“œ

import numpy as np

data1 = np.zeros(10)
print(data1)
print(data1.dtype)

data2 = np.zeros((2,3))
print(data2)

data3 = np.zeros((2,3),dtype=np.int32)
print(data3)

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

float64

[[0. 0. 0.]

 [0. 0. 0.]]

[[0 0 0]

 [0 0 0]]

 

1๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ : ones() ๋ฉ”์†Œ๋“œ

 

์ผ์ • ๊ทœ์น™ ๊ฐ€์ง„ ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ : arrange() ๋ฉ”์†Œ๋“œ

import numpy as np

data = np.arrange(10, 121, 10)
print(data)
print(data[2])
print(data[5:8])

data[7:10] = 800
print(data)

data2 = data.reshape(2,6)
print(data2)

์‹คํ–‰ ๊ฒฐ๊ณผ

 

[ 10 20 30 40 50 60 70 80 90 100 110 120]

30

[60 70 80]

[ 10 20 30 40 50 60 70 800 800 800 110 120]

[[ 10 20 30 40 50 60]

 [70 800 800 800 110 120]]

 

reshape() : ndarray์ฐจ์›์˜ ๊ฐ์ฒด ์žฌ๊ตฌ์„ฑ. ์˜ˆ๋ฅผ ๋“ค์–ด reshape(3,5)์€ ๋ฐฐ์—ด์˜ ๊ตฌ์กฐ๋ฅผ 3ํ–‰ 5์—ด์˜ 2์ฐจ์›์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

 

 

 

์Šฌ๋ผ์ด์‹ฑ

 

import numpy as np

data = np.array([[1,2,3,4,5],
              [6,7,8,9,10],)
              [11,12,13,14,15]])
print(data)
print(data[2][3])
print(data[0][1:])
print(data[0])
print(data[[1,2]])
print()

data[1]=100
print(data)

data[:] = 200
print(data)

 

[[ 1 2 3 4 5]

 [ 6 7 8 9 10]

 [11 12 13 14 15]]

14

[2 3 4 5]

[1 2 3 4 5]

[[6 7 8 9 10]

 [11 12 13 14 15]]

 

[[ 1 2 3 4 5 ]

 [100 100 100 100 100]

 [ 11 12 13 14 15]]

[[200 200 200 200 200]

 [200 200 200 200 200]

 [200 200 200 200 200]]

 

 

 

๋ฐฐ์—ด์˜ ์‚ฐ์ˆ ์—ฐ์‚ฐ (+,-,*,/,%) ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ

import numpy as np



a = np.array([[10,7,-8,2],
          [-2,2,8,3],
          [6,-8,-5,3]])
          
b= a * 2
print(b)

c = a * a
print(c)

print(a>b)

์‹คํ–‰ ๊ฒฐ๊ณผ

 

[[ 20 14 -16 4]

 [ -4 4 16 6]

 [12 -16 -10 6]]

[[100 49 64 4]

 [ 4 4 64 9]

 [ 36 64 25 9]]

[[False False True False]

 [ True False False False]

 [False True True False]]

 

 

np.where(์กฐ๊ฑด์‹, ๊ฐ’1, ๊ฐ’2)

์กฐ๊ฑด์ด ์ฐธ์ด๋ฉด ๊ฐ’1, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฐ’2์˜ ์š”์†Œ ๊ฐ’์„ ๊ฐ€์ง„ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.

 

np.where(data > 0, 5, data)

data์—์„œ ์š”์†Œ๊ฐ’์ด ์–‘์ˆ˜๋ฉด 5, ๊ทธ๋ ‡์ง€์•Š์œผ๋ฉด ์›๋ž˜ ์š”์†Œ ๊ฐ’์ธ data๋ฅผ ๋ฐ˜ํ™˜

 

 

data.sort(0) ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

data.sort(1) ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

 

sum() ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์˜ ํ•ฉ

mean() ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์˜ ํ‰๊ท 

max() ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ์ตœ๋Œ“๊ฐ’

min()

argmax() ์ตœ๋Œ“๊ฐ’์„ ๊ฐ€์ง€๋Š” ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

argmin()

insert() ๋ฐฐ์—ด์— ํ–‰ ๋˜๋Š” ์—ด ์‚ฝ์ž…

 

np.insert(a, 3, 10)

๋ฐฐ์—ด a์˜ ์ธ๋ฑ์Šค 3์˜ ์š”์†Œ์— 10์„ ์‚ฝ์ž…

 

np.insert(x, 1, 10, axis =0)

๋ฐฐ์—ด x์˜ ์—ด ๋ฐฉํ–ฅ์œผ๋กœ ์ธ๋ฑ์Šค 1์˜ ์š”์†Œ์— ๋ชจ๋“  ์š”์†Œ๊ฐ’์ด 10์ธ ํ–‰์„ ํ•˜๋‚˜ ์‚ฝ์ž… - [10 10 10]

 

np.insert(x, 1, 10, axis =)1

๋ฐฐ์—ด x์˜ ํ–‰ ๋ฐฉํ–ฅ์œผ๋กœ ์ธ๋ฑ์Šค 1์˜ ์š”์†Œ์— ๋ชจ๋“  ์š”์†Œ๊ฐ’์ด 10์ธ ์—ด์„ ํ•˜๋‚˜ ์‚ฝ์ž…

 

[[1 10 1 1 ]

 [ 2 10  2 2 ]

 [ 3 10  3 3]]

๋ฐ˜์‘ํ˜•