http://computertriksno1.blogspot.in/

Learn Numpy in 5 Minutes | Python | Trainosite

11 May 2022 0 comments

Python is a Famous Programming language and the main reason that it is so much popular is the Simplicity of the Python Programming Language. Numpy is The Package in Python Programming language which is used for scientific Computing. Numpy makes complicated Scientific calculations easy for everyone. Numpy is one of the Python libraries which is mostly used in  Data related projects as it provides multidimensional array objects and other various Objects.

Numpy provides some important functionalities for scientific calculations which include shape manipulation, sorting, basic linear algebra, and a lot more which help scientists and data programmers in their projects. 

Some Important Properties of Numpy Python Package

  1.  Numpy arrays are different than Python List. You have defined the size of the NumPy array before using it. unlike the python list where it is dynamically changed. but for efficiency, it is important to specify the size of the NumPy array.
  2.  If you changed the size of the NumPy array it will automatically delete the NumPy array and will recreate another array of that size.
  3.  To make the calculation easy the Numpy array has another restriction that wants us to put data of the same type in a NumPy array. If we want to put integer values in the array then we are not allowed to put float and double or character values in that NumPy array.
  4.  Numpy Array is able to carry out a large number of mathematical and other scientific operations on a large number of data.

How to install NumPy in Python

There are many ways you can install Numpy but the easiest way is to install it by using the pip which will install the Numpy from the PyPI. Run the below command on your terminal to install Numpy.


pip install numpy

Let the Begin and Start Learn by coding

You need a little bit of python to start NumPy which I know you will. 
To use Numpy we need to import it by the import statement. Numpy is normally imported with np alias. To create a NumPy array we can do it as follows.


import numpy as np

myArr = np.array([10, 20, 30, 40, 50])

print(myArr)

To find the version of the NumPy that is currently installed we can use the following code.


import numpy as np

myArr = np.array([10, 20, 30, 40, 50])

print(f"NumPy Version: {np.__version__}")







  •  To Create a One Dimensional Array you can write a normal list and pass it the numpy.array() method. You can do something like below.
    
    import numpy as np
    normallist=[10, 20, 30, 40, 50]
    myArr = np.array(normallist)
    print(myArr)
    
    
  • To create a Two Dimensional Numpy Array Array you can pass a two dimensioan list to the np.array() method and you can create a multidimensional array in the same method.

    
    import numpy as np
    
    _2DArray=[[10, 20, 30, 40, 50],[10, 20, 30, 40, 50]]
    myArr = np.array(_2DArray)
    print(myArr)
    
    
  • When we are trying to find the dimension of a NumPy array we can do it as follows. The below code shows us how to find the dimensions of a NumPy array.


import numpy as np

_1DList=[[10, 20, 30, 40, 50]]
_2DList=[[10, 20, 30, 40, 50],[10, 20, 30, 40, 50]]

_1DArray=np.array(_1DList)
_2DArray=np.array(_2DList)


print(_1DArray.ndim)
print(_2DArray.ndim)

  • We can slice a NumPy array like we are slicing a python list. it works much similar to the python list. Remember we need not pass the indices of the start and the end index. we only first the start index and then we pass the index up to what we want to slice. if we do not pass the start index it will start from the zeroth index and if we do not pass the end index it will go up to the last index.

    
    import numpy as np
    
    _1DList=[[10, 20, 30, 40, 50]]
    _2DList=[[10, 20, 30, 40, 50],[10, 20, 30, 40, 50]]
    
    _1DArray=np.array(_1DList)
    _2DArray=np.array(_2DList)
    
    newArray = _1DArray[1:4]
    newArray1 = _1DArray[:4]
    newArray2 = _1DArray[0:]
    
    
    
    
  • Although Python has five types of data types with the help of NumPy, we are having more than these python data types some of them are given below.integer, boolean, unsigned integer, float, complex float, time delta, DateTime, object, string, Unicode string, a fixed chunk of memory for other types.
  • Below are all of the examples that we can create an integer array. the second parameter specifies that these arrays are of type integer.
    
    import numpy as np
    
    integertype = np.array([1,3,4],"i")
    integertype1 = np.array(["4","5",4],"i")
    integertype2 = np.array(["2","4"],"i")
    integertype3 = np.array([4.2,5.2],"i")
    
    print(integertype.dtype)
    print(integertype1.dtype)
    print(integertype2.dtype)
    print(integertype3.dtype)
    
    
  • A string array can be created by specifying the second parameter as "S". Below are all of the examples that we can create a String array. the second parameter specifies that these arrays are of type String. 
    
    import numpy as np
    
    string1 = np.array([1,3,4],"S")
    string2 = np.array(["4","5",4],"S")
    string3 = np.array(["2","4"],"S")
    string4 = np.array([4.2,5.2],"S")
    
    print(string1.dtype)
    print(string2.dtype)
    print(string3.dtype)
    print(string4.dtype)
    
    
    
  • To create an integer Numpy array just pass the "i" as a second parameter and for the boolean use "b", for unsigned use "u", for float use "f", for complex use "c", for time delta use "m", for DateTime use "M", for object use "O", for string use "S", for Unicode use "U".
  • The Copy() method creates a new array and any changes to that array will not affect the original array. Below is the code which shows that the copy() function creates a separate array and then we assigned it to another variable and any change does not affect the original array overall. 
    
    import numpy as np
    
    orignal = np.array([1,3,4],"S")
    
    newArr = orignal.copy()
    newArr[0] = "56"
    
    print(orignal)
    print(newArr)
    
    #output
    # [b'1' b'3' b'4']
    # [b'5' b'3' b'4']
    
    
    
  • Another Important method which is a view() will just be the view of the original like it is a shadow of the original and any changes to the view will affect the original array as well. you can see it in the following code.
    
    
    import numpy as np
    
    orignal = np.array([1,3,4],"S")
    
    newArr = orignal.view()
    newArr[0] = "56"
    
    print(orignal)
    print(newArr)
    
    #output
    # [b'1' b'3' b'4']
    # [b'1' b'3' b'4']
    
    
    
  • Sometimes we need to see how many elements we have in the array. below is the code which explores the shape method which tells us about the shape of the NumpyArray.


import numpy as np

myArr = np.array([[1, 4], [5, 6],[1, 4], [5, 6]])

print(myArr.shape)

# output
# (4,2)


  • To iterate and access each element of a NumPy array we can use the normal method of the loop. we can iterate on one-dimensional and as well as multidimensional arrays by using the nested loop. below is the code for iterating and accessing the array elements.
    
    
    import numpy as np
    
    myArr = np.array([[1, 4], [5, 6],[1, 4], [5, 6]])
    
    for i in myArr:
        for j in i:
            print(j)
    
    # output        
    # 1
    # 4
    # 5
    # 6
    # 1
    # 4
    # 5
    # 6
    
    
  • Joining the Numpy arrays can be easily done with the help of the concatenate method available in the NumPy Package.
    
    import numpy as np
    
    myArr = np.array([4,53,4])
    mySec = np.array([4,3,4])
    
    thirdarr = np.concatenate((myArr,mySec))
    print(thirdarr)
    # output
    # [ 4 53  4  4  3  4]
    
    
  • The splitting array is another art that we can do. NumPy provides us a very easy interface to split the array. We can split a single array into multiple arrays of our choice. 
    
    import numpy as np
    
    myArr = np.array([4,53,4,5,64,1,90,23])
    mySec = np.split(myArr,2)
    
    print(mySec)
    
    # output
    # [array([ 4, 53,  4,  5]), array([64,  1, 90, 23])]
    
    
  • We have the where() method in NumPy which helps us find an element of the numpy array. it will return us the index of the array. It returns all the indices where the element of the numpy array is found. 
    
    import numpy as np
    
    myArr = np.array([4,5,4,5,64,1,90,23])
    indices = np.where(myArr == 5)
    
    print(indices)
    
    # output
    # (array([1, 3], dtype=int64),)
    
    
    
  • To sort the NumPy array we use the sort method. We can call the sort method on any type of NumPy array though the effect will be different on different data types below is the code which explains the sort method.
    
    import numpy as np
    
    #sorting int array
    intArr = np.array([4,5,4,5,64,1,90,23])
    print(np.sort(intArr))
    
    # sorting string array
    friends = np.array(['ali', 'jabir', 'sabit'])
    print(np.sort(friends))
    
    # sorting boolean array
    booleanarr = np.array([True, False, True])
    print(np.sort(booleanarr))
    
    # sorting two 2 array
    _2D= np.array([[3, 2, 4], [5, 0, 1]])
    print(np.sort(_2D))
    
    # output
    # [ 1  4  4  5  5 23 64 90]
    # ['ali' 'jabir' 'sabit']
    # [False  True  True]
    # [[2 3 4]
    #  [0 1 5]]
    
    




Share this article :

Post a Comment

 
Support : GDDon | Creating Website | Gddon |
Copyright © 2013. Computer Tricks and Tips for System - All Rights Reserved
Template Created by Creating Website Modify by GDDon.Com
Proudly powered by Blogger