기록하는 습관

[Numpy] 002. 여러가지 ndarray 생성 함수 본문

Python/Numpy

[Numpy] 002. 여러가지 ndarray 생성 함수

Avalla 2023. 8. 4. 16:42

여러가지 ndarray 생성 함수들

import numpy as np

 

np.arange()

Docstring:

arange([start,] stop[, step,], dtype=None, *, like=None)

print( np.arange(0,10) )
print( np.arange(0,5, 0.5) ) # 리스트와 다른 부분
print( np.arange(0,10,3) )
print( np.arange(0,10,-2) )
print( np.arange(10,0,-2) )
[0 1 2 3 4 5 6 7 8 9]
[0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5]
[0 3 6 9]
[]
[10  8  6  4  2]

 

np.zeros(), np.ones()

Docstring:

zeros(shape, dtype=float, order='C', *, like=None)

print( np.zeros(5) )
print( np.zeros(5,) )
print( np.zeros((1,5)) )
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[[0. 0. 0. 0. 0.]]
print( np.zeros(1,5) )
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-38-e78071e71b02> in <module>
----> 1 print( np.zeros(1,5) )


TypeError: Cannot interpret '5' as a data type
print( np.ones(4) )
print( np.ones(4,) )
print( np.ones((4,1,1)) )
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[[[1.]]

 [[1.]]

 [[1.]]

 [[1.]]]
print( np.ones(4,1) )
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-40-05479a2f84cc> in <module>
----> 1 print( np.ones(4,1) )


~\anaconda3\lib\site-packages\numpy\core\numeric.py in ones(shape, dtype, order, like)
    201         return _ones_with_like(shape, dtype=dtype, order=order, like=like)
    202 
--> 203     a = empty(shape, dtype, order)
    204     multiarray.copyto(a, 1, casting='unsafe')
    205     return a


TypeError: Cannot interpret '1' as a data type

 

np.■_like()

기존 ndarray의 shape과 같은 ndarray에 값을 ■로 새로 생성

print( np.zeros_like([1,1,1]) )
print( np.ones_like([[1,2],[3,4]]) )
print( np.empty_like([[0,0,0,0]]) )
[0 0 0]
[[1 1]
 [1 1]]
[[1 2 3 4]]

 

np.empty()

Docstring:

empty(shape, dtype=float, order='C', *, like=None)

  • null 값을 의미하는 것이 아니라 메모리 초기화가 안 된 상태
print( np.empty((3,5)) )
print( np.empty(3, dtype="int") )
print( np.empty_like([[1,2],[3,4]]) )
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
[-1391561200         376           0]
[[1 2]
 [3 4]]

 

np.full()

Signature: np.full(shape, fill_value, dtype=None, order='C', *, like=None)

Docstring:

Return a new array of given shape and type, filled with fill_value.

print( np.full((3,4),7) )
[[7 7 7 7]
 [7 7 7 7]
 [7 7 7 7]]

 

대각행렬 np.identity(), np.eye(), np.diag()

print( np.identity(3) )
print( np.identity(3, dtype="int") )
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[[1 0 0]
 [0 1 0]
 [0 0 1]]
print( np.eye(N=3, M=4) )
print( np.eye(3, 4, k=2) ) # k : 스타팅 포인트
print( np.eye(3, 4, k=3) ) # k : 스타팅 포인트
print( np.eye(4, dtype="int") )
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
[[0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [0. 0. 0. 0.]]
[[0. 0. 0. 1.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[[1 0 0 0]
 [0 1 0 0]
 [0 0 1 0]
 [0 0 0 1]]
matrix = [[1,2],[3,4]]
print( np.diag(matrix) )
print( np.diag(matrix, k=-1) )
[1 4]
[3]