라일락 꽃이 피는 날

[Numpy] ndarray 생성 본문

데이터 분석/Python

[Numpy] ndarray 생성

eunki 2021. 6. 16. 18:45
728x90

1. zeros

0으로 채워진 지정된 모양과 유형의 새로운 배열을 반환한다.

 

numpy.zeros(shape, dtype)

shape : 반환할 배열의 모양

dtype : 반환할 데이터 유형

np.zeros(5)  # array([ 0.,  0.,  0.,  0.,  0.])

np.zeros((5,), dtype=int)  # array([0, 0, 0, 0, 0])

np.zeros((2, 1))  # array([[ 0.],
                  #        [ 0.]])

 

 

 

2. ones

1로 채워진 지정된 모양과 유형의 새로운 배열을 반환한다.

 

numpy.ones(shape, dtype)

shape : 반환할 배열의 모양

dtype : 반환할 데이터 유형

np.ones(5)  # array([1., 1., 1., 1., 1.])

np.ones((5,), dtype=int)  # array([1, 1, 1, 1, 1])

np.ones((2, 1))  # array([[1.],
                 #        [1.]])

 

 

 

3. empty

쓰레기 값으로 채워진 지정된 모양과 유형의 새로운 배열을 반환한다.

np.empty((3, 4))

 

 

 

4. full

특정한 값으로 채워진 지정된 모양과 유형의 새로운 배열을 반환한다.

np.full((3, 4), 7)

 

 

 

5. eye

단위 행렬로 이루어진 배열을 반환한다.

np.eye(5)

 

 

 

6. linspace

지정된 구간 내에서 균등한 간격을 두고 값을 반환한다.

 

numpy.linspace(start, stop, num, endpoint)

start : 시퀀스의 시작 값

stop : 시퀀스의 끝 값

num : 생성할 샘플의 개수

endpoint : 시퀀스의 끝 값을 포함(True), 포함하지 않음(False)

np.linspace(start=0, stop=5, num=3)  # array([0. , 2.5, 5. ])

np.linspace(2.0, 3.0, 5)  # array([2.  , 2.25, 2.5 , 2.75, 3.  ])

np.linspace(2.0, 3.0, num=5, endpoint=False)  # array([2. ,  2.2,  2.4,  2.6,  2.8])
728x90

'데이터 분석 > Python' 카테고리의 다른 글

[Numpy] ravel, flatten  (0) 2021.09.07
[Numpy] 삼각함수 (sin, cos, tan)  (0) 2021.06.16
[Numpy] 기본 함수  (0) 2021.06.16
[Pandas] melt  (0) 2021.06.14
[Numpy] concatenate, split  (0) 2021.06.14