NumPy for Data Science
NumPy
What is NumPy?
The NumPy library provides the foundation of Python's scientific computational power. It offers mathematical functions like random number generators, linear algebra and Fourier transformation. Furthermore, Numpy is open source and easy to use.
Installation of Numpy
There are some basic steps you have to follow first before installing NumPy libaray.
Step 1: Verify Python's Version
Before installing NumPy, you have to check the version of the python.
You almost certainly have Python 2 or Python 3 installed, or maybe both.
Run the following command to see if you have Python 2.
>>> python -V
Run the following command to see if you have Python 3.
>>> python3 -V
Step 2: Install Pip
Pip is the most convenient way to install NumPy. Pip is a Python package manager that allows you to install and manage Python software packages.
Install Pip (for Python 2) by running:
>>> sudo apt install python-pip
Install Pip (for Python 3) by running:
>>> sudo apt install python3-pip
Step 3: Install NumPy
You can use Pip's command line for install NumPy once it's downloaded.
Install NumPy with Python 2 by typing:
>>> pip install numpy
Install NumPy with Python 3 by typing
>>> pip3 install numpy
Why is NumPy more effective than Lists?
NumPy arrays, unlike lists, are stored in a single continuous memory location, allowing programmes to access and manipulate them quickly.
Import Numpy
Once NumPy is installed, use the “import” keyword to include that in your applications:
For Example-
Creating Arrays:
Array creation is easy with Numpy's built-in functions. We'll see the a few of them in this article.
For Example-
Reshaping Array Numpy:
Changing the shape of an array is referred to as reshaping. The number of elements in each dimension determines the array's form. We can add or subtract dimensions, as well as vary the amount of elements in each dimension, by reshaping.
For Example-
Checking an Array's Data Type:
Every numpy array is a grid of the same sort of items. Numpy comes with a huge number of numeric datatypes that can be used to build arrays. When you create an array in Numpy, it tries to guess the datatype, however most functions that construct arrays have an extra argument to specify the datatype properly.
The following is a list of all NumPy data types and the characters that represent them.
For Example-
Accessing Numpy:
Accessing an array element is the same as indexing an array. The index number of an array element can be used to access it. NumPy array indexes begin with 0, therefore the first element has index 0, the second has index 1, and so on.
For Example-
Array Slicing
Numpy arrays can be sliced in the same way that Python lists are. Because arrays might be multidimensional, you'll now have to specify a slice for each dimension:
In this case, we use slice so instead of index and format for that : [start:end].
Even we can write the dimension like this: [start:end:step].
If we don't pass start it will automatically considered it’s 0.
If we don't pass end it will automatically considered length of array in that dimension.
If we don't pass step it will automatically considered 1.
For Example-
Sorting Numpy
Sorting is the process of arranging elements in a logical order. Any series with an order corresponding to elements, such as numeric or alphabetical, ascending or descending, is called an ordered sequence. Sort() is a function in the NumPy that object sorts a provided array.
For Example-
I hope you liked the blog we will be back with more such blog in future. Stay tuned.
- Avani Popat
- Mar, 10 2022