Analyze Data/Python Libraries

numpy-ndim, ravel, permutation, clip, subtract

Naranjito 2022. 5. 10. 11:04
  • ndarray.ndim

Number of array dimensions.

c=np.array([
    [[1,2,3],[3,4,4]],
    [[5,6,6],[7,8,8]]
])
print(c.ndim)
print(c.shape)

>>>
3
(2, 2, 3)

 

  • ravel

It returned 1-d array.

np.array([[1,2,3],[4,5,6]]).ravel()

>>>

array([1, 2, 3, 4, 5, 6])

 

Same as below.

np.array([[1,2,3],[4,5,6]]).reshape(-1)

>>>

array([1, 2, 3, 4, 5, 6])

 

  • permutation

It returns the permutated range.

np.random.permutation(10)

>>>

array([7, 1, 8, 2, 4, 6, 9, 0, 3, 5])

 

  • clip

If an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

np.clip(np.arange(10),2,5)

>>>

array([2, 2, 2, 3, 4, 5, 5, 5, 5, 5])

 

  • subtract

subtract(x,y) : x-y

x1=np.arange(9.0).reshape((3,3))
x1

>>>

array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]])
       
x2=np.arange(3.0)
x2

>>>

array([0., 1., 2.])

np.subtract(x1,x2)

>>>

array([[0., 0., 0.],
       [3., 3., 3.],
       [6., 6., 6.]])