Definition of NumPy
M3-R5.1 · Chapter 9: NumPy Basics · 6 min read
Definition of NumPy: NumPy (Numerical Python) is a Python library used for performing numerical computations efficiently. It provides support for multi-dimensional arrays and matrices, along with a wide variety of mathematical functions to operate on these arrays.
OR
NumPy is a Python library for working with large arrays and matrices of numeric data, and it provides tools to perform high-level mathematical operations on them.
Key Points:
- NumPy is a python package which stands for “Numerical Python”
- This was created in 2005 by Travis Oliphant
- It introduces the powerful object ndarray for array operations.
- It is highly efficient and is used as the base for many other scientific libraries in Python, such as Pandas, SciPy, and TensorFlow
- Using NumPy we can perform the following Functionalities
- It is faster than Python List. It is fast because it is associated with C Programming
Why Use NumPy?
- In Python we have lists that serve the purpose of arrays, but they are slow to process.
- NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
- The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.
- Arrays are very frequently used in data science, where speed and resources are very important.
Data Science: is a branch of computer science where we study how to store, use and analyze data for deriving information from it.
Which Language is NumPy written in?
- NumPy is a Python library and is written partially in Python, but most of the parts that require fast computation are written in C or C++.
How to Check pip is Installing or not
Win+R->cmd->Press Enter Key
C:\Users\Server>pip --version -> Press Enter Key Then this command Show the Pip Version Details
If PIP is not installed in your PC Then type the following on command prompt
C:\Users\Server>pip Install pip -> Press Enter Key then copy the given address and paste in new line on command prompt like.
C:\Users\Server>…………………………………python.exe –m pip Install –upgrade pip -> Press Enter Key
And if you have Python and PIP already installed on a system then installation of numPy is very easy. So install it by using this command:
C:\Users\Server>pip install numpy -> Press Enter Key
Note: Make sure the internet service is working on your pc
If this command fails, then use a python distribution that already has NumPy installed like, Anaconda, Spyder etc.
Installing NumPy
- Anaconda (https://www.anaconda.com) is a free Python distribution for SciPy Stack. It is also available for Linux and Mac Download and install Anaconda and finish it.
- After installation go to start menu and click on “Anaconda Navigator”
- This comes with many options like Jupyter Notebook, Spyder, VS Code etc.
- Here we will use Jupyiter Notebook
Working With NumPy: We need to create a folder (suppose we will create a folder on desktop named “jainp”) and create our all files inside it. Now , go to anaconda select your directory and type the following command as your first statement
Import numpy or Import numpy as np
And run it if it runs successfully without any error numpy is successfully installed in your computer. It not then due to some reason numpy has not been installed successfully.
In this situation open anaconda prompt and write a command: conda install numpy
Import NumPy : Once NumPy is installed, import it in your applications by adding the import keyword:
import numpy # Now NumPy is imported and ready to use.
NumPy as np : NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing: import numpy as np
Now the NumPy package can be referred to as np instead of numpy.
Example:
|
import numpy arr = numpy.array([1, 2, 3, 4, 5]) print(arr) |
import numpy as np a=[1,2,3,4,5] myarr=np.array(a) print(myarr) |
import numpy as np a=[] size=int(input("Enter number of Elements")) for i in range(size): val=int(input("Enter Number:")) a.append(val) jai=np.array(a) print(jai) |
:NumPy Creating Arrays :
Create a NumPy ndarray Object
- NumPy is used to work with arrays. The array object in NumPy is called ndarray.
- We can create a NumPy ndarray object by using the array() function.
Example :
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that arr is numpy.ndarray type.
- To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:
Example Use a tuple to create a NumPy array:
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
OUTPUT: [1 2 3 4 5]