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
 
 
 
 
 

Recommendations