numpy ndarray

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2024 Sep 21 15:46
Editor
Edited
Edited
2026 Aug 9 23:5

N-dimensional array

Create

  • np.array()
    • a = np.array([[1,2], [3,4]])
  • numpy.ones
    • create array which has all components 1
      np.ones((2, 3)) # [[1., 1., 1.], # [1., 1., 1.]]
  • numpy.full()
    • np.full((2, 3), 7) # [[7, 7, 7], [7, 7, 7]]
  • numpy.zeros_like()
    • a = np.array([[1,2], [3,4]]) np.zeros_like(a) # [[0, 0], [0, 0]]
  • np.arange()
    • like range function, define size of the step
      np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
  • numpy.empty()
    • np.empty((2, 3)) # [[?, ?, ?], [?, ?, ?]] # arbitrary values
  • np.linspace
    • Number of samples
      np.linspace(0, 10, 5) # [0., 2.5, 5., 7.5, 10.]
  • np.eye()
    • return 2D identity matrix with size N
      np.eye(3) # [[1,0,0], [0,1,0], [0,0,1]]

Basic

  • numpy ndarray.shape
  • np.min(arr, axis)
  • np.max(arr, axis)
  • np.sum(arr, axis)
  • np.isnan
  • np.nan == x, np.nan == np.nan is False

Dimension

  • numpy ndarray.flatten()
    • Return a copy of the array collapsed into one dimension.

      np.array([[1,2,3], [4,5,6]]).flatten() # all dimension to 1 dimension # [[1,2,3], → [1,2,3,4,5,6] # [4,5,6]]
  • numpy ndarray.transpose()
    • (new axis order)
      (2,3,4) → arr.transpose(1,0,2) → (3,2,4)

Condition

  • np.nonzero() - array of non-zero indices
  • np.where(condition, if yes value, if no value)
    • np.where(condition) - array of true indices
  • np.where
  • rng = np.random.RandomState(seed) - rng.randint(low, high, size)
  • np.bincount() - count occurrences of non-negative integers
    • if negatives are there, ValueError
 
 
 
numpy.eye — NumPy v2.1 Manual
Return a 2-D array with ones on the diagonal and zeros elsewhere.
The N-dimensional array (ndarray) — NumPy v2.1 Manual
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension. The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.
[numpy 기초] np.linspace와 np.arange의 차이(번역)
Numpy 기초를 공부하면서 linspace와 arange 함수가 헷갈려서 이를 잘 정리해준 Statology의 영문글을 기계 번역하여 포스팅합니다. (원본: https://www.statology.org/numpy-linspace-vs-arange/)NumP
[numpy 기초] np.linspace와 np.arange의 차이(번역)
(파이썬) numpy.arange
numpy.arange([start, ] stop, [step, ] dtype=None) numpy 모듈의 arange 함수는 반열린구간 [start, stop) 에서 step 의 크기만큼 일정하게 떨어져 있는 숫자들을 array 형태로 반환해 주는 함수다. stop 매개변수의 값은 반드시 전달되어야 하지만 start 는 step 은 꼭 전달되지 않아도 된다. start 값이 전달되지 않았다면 0 을 기본값으로 가지며, step 값이 전달되지 않았다면 1 값을 기본값으로 갖게 된다. dtype 의 경우 결과로 반환되는 array 이의 type 을 지정할 때 사용한다. dtype 값이 주어지지 않는 경우 전달된 다른 매개 변수로부터 type 을 추론하게 된다. 다음의 예를 보면 쉽게 이해할 수 있을 것이다..
(파이썬) numpy.arange
 
 

Recommendations