donut

Python Numpy1

Module/Numpy

 

 

 

 

Numpy

Numpy란?

numerical Python.
파이썬의 고성능 과학 계산용 패키지
메트릭스와 백터, 배열연산 가능.

 

Numpy 사용

범용적으로 별칭 np 사용함.

In [101]:
import numpy as np
In [102]:
test1 = np.array([0,1,2,3,4,5])#배열안의 원소값들의 데이터 형식을 지정 가능 디폴트 int
test2 = np.array([0,1,2,3,4,5,6], float)#원소값들을 플롯 형식으로지정
test3 = np.array(["0","1",2,3,4], int)#배열안에 문자값을 숫자값으로 읽어드릴 수 있다.
test4 = np.array(range(10))
In [103]:
print(test1)
print(test2)# 플롯 형식은 '0.', '1.' 식으로 저장됨
print(test3)# "0" ,"1"이 숫자로 저장됨
print(test4)
 
[0 1 2 3 4 5]
[0. 1. 2. 3. 4. 5. 6.]
[0 1 2 3 4]
[0 1 2 3 4 5 6 7 8 9]
 

Numpy type

In [104]:
#ndarray 란 넘피 디맨션 어레이 = numpy dimansion array
print(type(test1))
print(type(test2))
print(type(test3))
print(type(test4))
 
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
In [105]:
#원소값을 직접 호출 가능하다
print(type(test1[0]))
print(type(test2[1]))
print(type(test3[2]))
print(type(test4[3]))
 
<class 'numpy.int64'>
<class 'numpy.float64'>
<class 'numpy.int64'>
<class 'numpy.int64'>
 

Numpy 응용

In [106]:
#배열은 다차원으로 만들 수 있다.
test1 = np.array([[0,1,2,3,4,],[5,6,7,8,9]], float)#로우와 컬럼이 존재하는 배열로 생성
print(test1)
print(type(test1))
print(type(test1[0]))
print(type(test1[0][0]))
 
[[0. 1. 2. 3. 4.]
 [5. 6. 7. 8. 9.]]
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.float64'>
 

Numpy & shape

In [107]:
test1 = np.array([0,1,2])#컬럼만 존재 [1차원]
test2 = np.array([[0,1,2],[3,4,5]])#컬럼과 로우가 존재[2차원] 
test3 = np.array([[0,1,2],[3,4,5],["6",7,8]])#로우한줄 추가[2차원]


#3차원 배열 선언
test4 = np.array([[[0,1,2],[3,4,5],[6,7,8]],
                  [[9,10,11],[12,13,14],[15,16,17]],
                  [[18,19,20],[21,22,23],[24,25,26]]])

test5 = [[[0,1,2],[3,4,5],[6,7,8]],
        [[9,10,11],[12,13,14],[15,16,17]],
        [[18,19,20],[21,22,23],[24,25,26]]]

print(test1.shape)#1차원의 경우 '(3,)' 과 같이 생성된다.
print(test2.shape)#차원이 늘어날 경우 기존 값은 오른쪽으로 밀리고, 새로운 값이 왼쪽에 생성된다.
print(test3.shape)
print(test4.shape)#(3 = 공간(테이블이 3개) , 3 =3줄(row), 3 = 3칸(column))
print(np.array(test5).shape)
 
(3,)
(2, 3)
(3, 3)
(3, 3, 3)
(3, 3, 3)
 

Numpy & shape 2

ndim - number of dimension
size - data 수

In [108]:
#위에서 사용한 배열 참고
print(test1.ndim ," , ", test1.size)
print(test2.ndim ," , ", test2.size)
print(test3.ndim ," , ", test3.size)
print(test4.ndim ," , ", test4.size)#데이터의 수가 9개인 테이블이 3개 존재
print(np.array(test5).ndim ," , ", np.array(test5).size)# 3 *9 = 27
 
1  ,  3
2  ,  6
2  ,  9
3  ,  27
3  ,  27
 

Numpy & shape3

reshape

배열을 재정립함 (원하는 차원으로)

In [109]:
test1 = np.array([[0,1,2],[3,4,5],["6",7,8]])
print(test1.shape,'\n'*2)#2차원의 배열
print(test1.reshape(9,),'\n'*2)#1차원으로 변환
print(test1.reshape(3,3),'\n'*2)
print(test1.reshape(-1,3),'\n'*2)#size를 기반으로 row개수 선정
print(test1.reshape(3,1,3),'\n'*2)
 
(3, 3) 


['0' '1' '2' '3' '4' '5' '6' '7' '8'] 


[['0' '1' '2']
 ['3' '4' '5']
 ['6' '7' '8']] 


[['0' '1' '2']
 ['3' '4' '5']
 ['6' '7' '8']] 


[[['0' '1' '2']]

 [['3' '4' '5']]

 [['6' '7' '8']]] 


 

flatten

다차원 배열을 1차원으로 변환한다.

In [111]:
test = [[[0,1]],[[2,3]],[[4,5]]]#3차원 선언
print(np.array(test),'\n'*2)

print(np.array(test).shape,'\n'*2)#3차원 shape확인

print(np.array(test).flatten(),'\n'*2)#3차원을 1차원으로 변환

print(np.array(test).flatten().shape,'\n'*2)# 확인
 
[[[0 1]]

 [[2 3]]

 [[4 5]]] 


(3, 1, 2) 


[0 1 2 3 4 5] 


(6,) 


In [ ]:
 

'Module > Numpy' 카테고리의 다른 글

Python Numpy3  (0) 2020.07.13
Python Numpy2  (0) 2020.07.12