NumPy 라이브 불러오기 1 2 import numpyprint (numpy.__version__)
1.21.5
1 2 import numpy as npprint (np.__version__)
1.21.5
배열로 변환
1부터 10까지의 리스트를 만든다.
NumPy 배열로 변환해서 저장한다.
1 2 3 4 temp = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] arr = np.array(temp) print (arr)print (temp)
[ 1 2 3 4 5 6 7 8 9 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1 2 print (type (temp))print (type (arr))
<class 'list'>
<class 'numpy.ndarray'>
array([5, 6, 7, 8])
NumPy를 사용하여 기초 통계 함수를 사용한다.
1 2 3 4 np.mean(arr) np.sum (arr) np.median(arr) np.std(arr)
2.8722813232690143
사칙연산 1 2 3 4 5 math_scores = [90 , 80 , 88 ] english_scores = [80 , 70 , 90 ] total_scores = math_scores + english_scores total_scores
[90, 80, 88, 80, 70, 90]
1 2 3 4 5 6 7 8 math_scores = [90 , 80 , 88 ] english_scores = [80 , 70 , 90 ] math_arr = np.array(math_scores) english_arr = np.array(english_scores) total_scores = math_arr + english_arr total_scores
array([170, 150, 178])
150
178
1 2 3 4 5 6 7 8 9 10 11 12 math_scores = [2 , 3 , 4 ] english_scores = [1 , 2 , 3 ] math_arr = np.array(math_scores) english_arr = np.array(english_scores) print ("덧셈:" , np.add(math_arr, english_arr))print ("뺄셈:" , np.subtract(math_arr, english_arr))print ("곱셈:" , np.multiply(math_arr, english_arr))print ("나눗셈:" , np.divide(math_arr, english_arr))print ("거듭제곱:" , np.power(math_arr, english_arr))
덧셈: [3 5 7]
뺄셈: [1 1 1]
곱셈: [ 2 6 12]
나눗셈: [2. 1.5 1.33333333]
거듭제곱: [ 2 9 64]
배열의 생성
1 2 3 4 temp_arr = np.array(20 ) print (temp_arr)print (type (temp_arr))print (temp_arr.shape)
20
<class 'numpy.ndarray'>
()
1 2 3 4 5 6 temp_arr = np.array([1 , 2 , 3 ]) print (temp_arr)print (type (temp_arr))print (temp_arr.shape)print (temp_arr.ndim)
[1 2 3]
<class 'numpy.ndarray'>
(3,)
1
1 2 3 4 5 6 temp_arr = np.array([[1 , 2 , 3 ], [4 , 5 , 6 ]]) print (temp_arr)print (type (temp_arr))print (temp_arr.shape) print (temp_arr.ndim)
[[1 2 3]
[4 5 6]]
<class 'numpy.ndarray'>
(2, 3)
2
1 2 3 4 5 6 temp_arr = np.array([[[1 , 2 , 3 ], [4 , 5 , 6 ]], [[1 , 2 , 3 ], [4 , 5 , 6 ]]]) print (temp_arr)print (type (temp_arr))print (temp_arr.shape)print (temp_arr.ndim)
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
<class 'numpy.ndarray'>
(2, 2, 3)
3
1 2 3 4 5 temp_arr = np.array([1 , 2 , 3 , 4 ], ndmin = 2 ) print (temp_arr)print (type (temp_arr))print (temp_arr.shape)print (temp_arr.ndim)
[[1 2 3 4]]
<class 'numpy.ndarray'>
(1, 4)
2
소수점 정렬 1 2 temp_arr = np.trunc([-1.23 , 1.23 ]) temp_arr
array([-1., 1.])
1 2 temp_arr = np.fix([-1.23 , 1.23 ]) temp_arr
array([-1., 1.])
1 2 temp_arr = np.around([-1.23789 , 1.23789 ], 4 ) temp_arr
array([-1.2379, 1.2379])
1 2 temp_arr = np.round ([-1.23789 , 1.23789 ], 4 ) temp_arr
array([-1.2379, 1.2379])
1 2 temp_arr = np.floor([-1.23789 , 1.23789 ]) temp_arr
array([-2., 1.])
1 2 temp_arr = np.ceil([-1.23789 , 1.23789 ]) temp_arr
array([-1., 2.])
배열을 생성하는 다양한 방법들 1 2 temp_arr = np.arange(5 ) temp_arr
array([0, 1, 2, 3, 4])
1 2 temp_arr = np.arange(1 , 11 , 3 ) temp_arr
array([ 1, 4, 7, 10])
1 2 3 4 5 6 zero_arr = np.zeros((2 , 3 )) print (zero_arr)print (type (zero_arr))print (zero_arr.shape)print (zero_arr.ndim)print (zero_arr.dtype)
[[0. 0. 0.]
[0. 0. 0.]]
<class 'numpy.ndarray'>
(2, 3)
2
float64
1 2 3 4 5 6 temp_arr = np.ones((4 , 5 ), dtype = "int32" ) print (temp_arr)print (type (temp_arr))print (temp_arr.shape)print (temp_arr.ndim)print (temp_arr.dtype)
[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]
<class 'numpy.ndarray'>
(4, 5)
2
int32
1 2 3 4 5 6 7 temp_arr = np.ones((2 , 6 ), dtype = "int32" ) temp_res_arr = temp_arr.reshape(2 , 2 , 3 ) print (temp_res_arr) print (type (temp_res_arr))print (temp_res_arr.shape)print (temp_res_arr.ndim)print (temp_res_arr.dtype)
[[[1 1 1]
[1 1 1]]
[[1 1 1]
[1 1 1]]]
<class 'numpy.ndarray'>
(2, 2, 3)
3
int32
1 2 3 4 5 6 7 temp_arr = np.ones((12 , 12 ), dtype = "int32" ) temp_res_arr = temp_arr.reshape(5 , -1 ) print (temp_res_arr) print (type (temp_res_arr))print (temp_res_arr.shape)print (temp_res_arr.ndim)print (temp_res_arr.dtype)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-60-dfc75cfbf69a> in <module>()
1 temp_arr = np.ones((12, 12), dtype = "int32")
----> 2 temp_res_arr = temp_arr.reshape(5, -1)
3 print(temp_res_arr)
4 print(type(temp_res_arr))
5 print(temp_res_arr.shape)
ValueError: cannot reshape array of size 144 into shape (5,newaxis)
numpy 조건식
1 2 temp_arr = np.arange(10 ) temp_arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1 2 3 np.where(temp_arr < 5 , temp_arr, temp_arr * 10 )
array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
1 2 3 4 5 temp_arr = np.arange(101 ) np.where(temp_arr < 50 , temp_arr * 10 , temp_arr)
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120,
130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250,
260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380,
390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
1 2 3 4 temp_arr = np.arange(10 ) temp_arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1 2 3 condlist = [temp_arr > 5 , temp_arr < 2 ] choicelist = [temp_arr * 2 , temp_arr + 100 ] np.select(condlist, choicelist, default = temp_arr)
array([100, 101, 2, 3, 4, 5, 12, 14, 16, 18])