Introduction To Numpy
In this Blog, I will be writing about all the basic stuff you need to know about numpy such as what is numpy, why we use numpy, why numpy is more useful than lists in python, getting started with numpy etc.
What is NumPy?
NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays. Using NumPy, mathematical and logical operations on arrays can be performed. This tutorial explains the basics of NumPy such as its architecture and environment. It also discusses the various array functions, types of indexing, etc.
Why Use NumPy?
n 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.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
This behavior is called locality of reference in computer science.
This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.
Before getting started first we need to install NumPy and in our systems. Make sure you already have python installed.
Installing NumPy
To download and install NumPy in your local environment. You first need to open cmd (command Prompt) and type the following command.
pip install numpy
After installing this library you need to import this library in your work space.
Importing NumPy
To import NumPy in your workspace refer to the below code.
import numpy as np
Now you are ready to get started with NumPy
Getting Started with NumPy
1. Creating array using NumPy
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.
2. NumPy Array Indexing
Array indexing is the same as accessing an array element.
You can access an array element by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Example
Get third and fourth elements from the following array and add them:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
Access 2-D Arrays
To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.
For Example:
Access the 5th element on 2nd dim:
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print(‘5th element on 2nd dim: ‘, arr[1, 4])
3. NumPy Array Slicing
Slicing in python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start:end]
.
We can also define the step, like this: [start:end:step]
.
If we don’t pass start its considered 0
If we don’t pass end its considered length of array in that dimension
If we don’t pass step its considered 1.
Example
Slice elements from index 1 to index 5 from the following array:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
Negative Slicing
Use the minus operator to refer to an index from the end:
Example:
Slice from the index 3 from the end to index 1 from the end:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
4. NumPy Array Shape
NumPy arrays have an attribute called shape
that returns a tuple with each index having the number of corresponding elements.
Example
Print the shape of a 2-D array:
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
5. NumPy Array Reshaping
Reshaping means changing the shape of an array.
The shape of an array is the number of elements in each dimension.
By reshaping we can add or remove dimensions or change number of elements in each dimension.
Reshape From 1-D to 2-D
Example
Convert the following 1-D array with 12 elements into a 2-D array.
The outermost dimension will have 4 arrays, each with 3 elements:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)
Reshape From 1-D to 3-D
Example
Convert the following 1-D array with 12 elements into a 3-D array.
The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2)
print(newarr)
Note: You don’t need to import NumPy library each time on same workspace. This is just for reference purpose.
There are plenty more operations that can be performed using NumPy library. For more details you can always visit following links:
Here’s my Jupyter Notebook, you can check for references.
So I am ending my Blog here. Thank you for reading till the end. I hope you will find something useful here. Any critics, suggestions, how can I improve my Blog writing is highly appreciated. Please do comment if any.