A blog for computer science passionates.

Monday, 11 November 2019

How to start ML & DL? NumPy Module Part - 2.5

Hello Friends!!! Welcome again,
Today we are going to discuss more methods of NumPy module. I hope you all do well with NumPy. So, let's start with NumPy and learn some more methods.

  • numpy.delete - this method is used to remove elements from an array. it returns a new array with sub-array along with axis that is deleted.
    • Syntax: numpy.delete(array,index,axis=None)
    • Here, array - represents an array from which you want to remove elements.
    • index - represents indexes of sub-arrays to remove along the specified axis. it may be any object.
    • axis - is an optional parameter. represents an axis along which to delete the sub-array defined in an index argument. 
  • It returns a ndarray. A copy of an array with the elements specified by the index removed.
  • Example,
  • #numpy delete method
    import numpy as np
    ar=np.array([1,2,3,4,5,6])
    arr=np.delete(ar,[1,3]) # removes element at first index and element ar third index
    print(ar)
    #Output [1 2 3 4 5 6]
    print(arr)
    #Output [1 3 5 6]
    ar=np.array([[1,2,3],[4,5,6]])
    arr=np.delete(ar,[1,3])
    print(ar)
    #Output
    #[[1 2 3]
    # [4 5 6]]
    print(arr)
    #Output [1 3 5 6]
    ar=np.array([[1,2,3],[4,5,6],[7,8,9]])
    arr=np.delete(ar,[2,3,5],0)
    print(ar)
    #Output
    #[[1 2 3]
    # [4 5 6]
    # [7 8 9]]
    print(arr)
    #Output
    #[[1 2 3]
    # [4 5 6]]
    ar=np.array([[1,2,3],[4,5,6],[7,8,9]])
    arr=np.delete(ar,[2,3,5],1)
    print(ar)
    #Output
    #[[1 2 3]
    # [4 5 6]
    # [7 8 9]]
    print(arr)
    #Output
    #[[1 2]
    # [4 5]
    # [7 8]]
    ar=np.array([[1,2,3],[4,5,6],[7,8,9]])
    arr=np.delete(ar,[2,3,5],None) # if axis is None, returned array becomes flattern
    print(ar)
    #Output
    #[[1 2 3]
    # [4 5 6]
    # [7 8 9]]
    print(arr)
    #Output [1 2 5 7 8 9]
    view raw numpy_delete.py hosted with ❤ by GitHub
  • numpy.concatenate - this method is used to join different arrays along with an existing axis.
    • Syntax: numpy.concatenate((ar1, ar2, ...),axis=0,out=None)
    • Here, ar1, ar2 - represents a sequence of arrays, all the array must have the same shape.
    • axis - is an optional argument, represents axis along which arrays will be joined. here default value of this parameter is 0.
    • out - is an optional parameter, it is ndarray object. represents the destination to place the result.
  • It returns ndarray object after concatenation of arrays. 
  • Example,  
    #NumPy concatenate method
    import numpy as np
    ar1=np.array([1,2,3])
    ar2=np.array([4,5,6])
    ar=np.concatenate([ar1,ar2])
    print(ar)
    #Output [1 2 3 4 5 6]
    ar1=np.array([[1,2,3],[7,8,9]])
    ar2=np.array([[4,5,6]])
    ar=np.concatenate([ar1,ar2])
    print(ar)
    #Output
    #[[1 2 3]
    # [7 8 9]
    # [4 5 6]]
    ar1=np.array([[1,2,3],[7,8,9]])
    ar2=np.array([[4,5,6],[11,12,13]])
    ar=np.concatenate([ar1,ar2],axis=0)#default axis
    print(ar)
    #Output
    #[[ 1 2 3]
    # [ 7 8 9]
    # [ 4 5 6]
    # [11 12 13]]
    ar1=np.array([[1,2,3],[7,8,9]])
    ar2=np.array([[4,5,6],[11,12,13]])
    ar=np.concatenate([ar1,ar2],axis=1)
    print(ar)
    #Output
    #[[ 1 2 3 4 5 6]
    # [ 7 8 9 11 12 13]]

These are methods of NumPy for this article that's it, in next article we will learn more methods.
Happy Learning & Coding...☺😊

No comments:

Post a Comment