Getting Started With NumPy

Introduction

  • NumPy stands for Numerical Python. It is the core library for scientific computing.
  • It is a multi-dimensional array library in Python. You can store any kind of data in it, be it 1D, 2D or 3D arrays etc.
  • It stores N-dimensional array objects in the form of rows and columns.
We cannot just directly use NumPy in python, it requires separate installation.
 
Below are the steps to install NumPy:
  1. You must have Python installed in your system beforehand. (For Example, I have Python3.7)
    You can visit here and install the desired version.
     
  2. After installing Python, search where it is installed. (For Example, C:\Users\Aashina\AppData\Local\Programs\Python\Python37\Scripts),
    Open the Scripts folder.
     
    Use a notepad, and write cmd in it, save it as “local.bat”
     
    Something like this:
     
     
  3. Now open this “local.bat” file, the cmd will open and it will give you the path where Python is installed.
    (If it doesn’t give you the right path, there is an installation issue. You can directly open Command prompt, Go to Start-> cmd)
     
  4. To install the NumPy library, use command: “pip install numpy”
     
     
    After it is installed, it will give you a prompt: “Successfully Installed”
     
  5. Now to make sure it is working fine, type: “python”
     
    If it compiles, there is no issue
     
  6. Test it, by typing: “import numpy as np”
     
    If it doesn’t give you an error, this means NumPy is working fine now. You have the Library.
       
Now if we go to IDLE, and try to test it by writing a NumPy program, it will work.
 
 

NumPy Operations

 
There are some in-built commands in NumPy, used to play with and perform some specific operations on given data,
  • Slicing
  • Size
  • Shape
  • Min
  • Max
  • Sum
  • Square root
  • Standard Deviation
  1. import numpy as np  
  2. import sys  
  3. a=np.array ([1,2,3])  
  4. print("Single Dimensional Array")  
  5. print(a)  
  6. b=np.array ([(1,2,3),(4,5,6)])  
  7. print("Multi Dimensional Array")  
  8. print(b)  
  9. print("Slicing")  
  10. print(b[0,2])  
  11. print(b[1,2])  
  12. print("size is used to size of an array")  
  13. print(a.size)  
  14. print(b.size)  
  15. print("shape is used to find dimension of an array")  
  16. print(a.shape)  
  17. print(b.shape)  
  18. print("min() dispalys the smallest element in array")  
  19. print(b.min())  
  20. print("max() dispalys the largest element in array")  
  21. print(b.max())  
  22. print("sum() calculates the sum of an array")  
  23. print(b.sum())  
  24. print("sqrt() calculates the square root of each element in an array")  
  25. print(np.sqrt(b))  
  26. print("std() calculates the standard deviation of elements in an array")  
  27. print(np.std(b))  
Sample Output
 
  • Linspace
  • Sum (with axis)
  1. import numpy as np  
  2. import sys  
  3. a=np.linspace(1,3,10)  
  4. print("This prints 10 values between 1 and 3")  
  5. print(a)  
  6. b=np.array([(1,2,3),(4,5,6)])  
  7. print("Sums up the elements in axis-0")  
  8. print(b.sum(axis=0))  

Within Matrices

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Horizontal Stack
  • Vertical Stack
  1. import numpy as np  
  2. import sys  
  3. x=np.array([(1,2,3),(4,5,6)])  
  4. y=np.array([(11,12,13),(14,15,16)])  
  5. print("Addition of matrices")  
  6. print(x+y)  
  7. print("Subtraction of matrices")  
  8. print(x-y)  
  9. print(y-x)  
  10. print("Multiplication of matrices")  
  11. print(x*y)  
  12. print("Division of matrices")  
  13. print(x/y)  
  14. print("Vertical Stack")  
  15. print(np.vstack(x))  
  16. print("Horizontal Stack")  
  17. print(np.hstack(y))   
Sample Output
 
 

Summary

 
In this article, we discussed NumPy and it's operations. I hope that it helps readers understand how to install, use and implement NumPy in Python.
 
My next article will discuss other concepts in NumPy, such as Array Creation, indexing, and some programs.
 
Feedback or queries related to this article are most welcome.
 
Thanks for reading.


Recommended Free Ebook
Similar Articles