A blog for computer science passionates.

Wednesday, 6 November 2019

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

Hello Friends!!!
This article takes some time, as I do work on some other articles also. Let's again work on NumPy, Kudos you all have learned some methods already from other articles of this module successfully. Now let's start with new methods with examples.
image (source: pixabay.com): Peoples are in a queue and new peoples are appended to the queue at the end of line
  • numpy.append - This method is used to add (append)new values in an array at the end. 
    • Syntax: numpy.append(array,values,axis=None)
    • Here, array - is array-like values to be appended to the copy of the main array.
    • values - is array-like values to be appended to the copy of the first argument array. it should be the same shape as the first argument has.
    • axis - is an optional argument. it is used to set the axis on which array and values are to be appended.
  • It returns ndarray object. copy array with values to the axis and append to ndarray.
  • Example,
  • #numpy append method.
    import numpy as np
    ar=np.array([12,23,34,45]) # define an array
    ap=np.append(ar,[4]) # append one element at the end of array
    ap1=np.append(ap,[56,199,647]) # append multiple elements at the end of array return after appending one element
    print(ar) # Output : [12 23 34 45]
    print(ap) # Output : [12 23 34 45 4]
    print(ap1) # Output : [ 12 23 34 45 4 56 199 647]
    #with axis
    arr=np.array([[1,2,3],[4,5,6]])
    app=np.append(arr,[[7,8,9]],axis=0)
    print(arr)
    # Output of the above statement
    #[[1 2 3]
    # [4 5 6]]
    print(app)
    # Output of the above statement
    #[[1 2 3]
    # [4 5 6]
    # [7 8 9]]
    view raw numpy_app.py hosted with ❤ by GitHub
  • It also gives ValueError, if axis of the array and values are different.

  • numpy.insert - This method is used to insert element(s) on specific index.
    • Syntax: numpy.insert(array,index,values,axis=None)
    • Here, array - is an array.
    • index - is an integer value that defines an index on which values are inserted. Even we can pass multiple integer indexes in the form of an object.
    • values - is values to be inserted in an array.
    • axis - is an optional argument, it is used to set the axis on which array and values are to be inserted.
  • It returns ndarray object with all inserted values to the array.
  • Example,
  • # numpy insert method demo
    import numpy as np
    ar=np.array([1,2,3])
    ins=np.insert(ar,2,[45])
    print(ar) # Output : [1 2 3] original array
    print(ins) # Output : [ 1 2 45 3] after inserting value 45 on index 2.
    arr=np.array([[12,34,45],[56,87,91]])
    ins_ar=np.insert(arr,1,[1,2,3])
    print(arr)
    #Output
    #[[12 34 45]
    # [56 87 91]]
    print(ins_ar)
    #Output : [12 1 2 3 34 45 56 87 91]
    #with axis
    ins_ar1=np.insert(arr,1,[1,2,3],axis=0)
    print(ins_ar1)
    #Output
    #[[12 34 45]
    # [ 1 2 3]
    # [56 87 91]]
    view raw numpy_insert.py hosted with ❤ by GitHub
  • Now, you can find the difference between append and insert. There is a simple difference through append you can insert an element at the end of the array only and through insert method, you can insert element(s) on any indexes that you have mentioned in index argument.
So, that's it for this article, in the next article we'll learn more methods.
Enjoy and learn new things every day, every time...☺😊

No comments:

Post a Comment