Beginner’s Guide to NumPy: Understanding Arrays, Functions, and More
Introduction
NumPy stands for Numerical Python. It is one of the most famous Python libraries used in data science to work with arrays. It is widely used in machine learning, data science, and scientific computing where speed and efficiency are important. NumPy is much faster than lists because arrays are stored in one continuous place in memory, unlike lists, so processes can access and manipulate them very efficiently.
Here is the Kaggle Notebook where you can play around and practise
https://www.kaggle.com/code/muhammadfahadbashir/numpy-tutorial-basic-functions
Below are some of the common basic functions which we usually deal with.
Common Basic Functions
1. Creating Arrays
2. Array of Zeros and Ones
3. Range of Numbers
4. Reshaping Arrays
5. Basic Operations (Add, subtract, multiply, divide)
6. Array Statistics
7. Indexing and Slicing
8. Random Numbers
9. Identity Matrix
10. Flatten
11. Transpose
12. Concatenate
13. Unique
14. Sum along axis
Installing NumPy
To install NumPy, you need to have pip
installed in your system. You can install it using the command:
pip install numpy
Alternatively, if you are using an online platform like Google Colab, you can simply import it:
import numpy as np
1. Creating Arrays
NumPy is used to work with arrays. The array object in NumPy is called ndarray
. We can create arrays easily
Example of Array Creation
2. Arrays of Zeros and Ones
We can create arrays filled with zeros or ones using zeros
and ones
functions
Example of Zeros and Ones
3. Range of Numbers
It’s similar to Python’s built-in range function but returns an array. We can create an array from start to end event specifying gap in an array form.
Example of range of Numbers
4. Reshaping
The reshape
function changes the shape of an array without changing its data. It is particularly useful when you need to convert a one-dimensional array into a multi-dimensional one.
Example of reshaping Array
5. Basic Operations with NumPy Arrays
NumPy allows for easy and efficient array operations such as addition, subtraction, multiplication, and division. It performs element-wise operations on arrays which makes them highly efficient.
Basic Operations with Numpy
6. Statistics of Arrays
NumPy provides various statistical functions like mean, median, and standard deviation.
Basic Statistics of Arrays
7. Array Slicing & Indexing
Slicing in Python means taking elements from one given index to another given index. We pass a slice instead of an index like this: [start:end]
. We can also define the step like this: [start:end:step]
.
Array slicing & indexing
8. Random Numbers
9. Identity Matrix
use np.eye() function to create identity matrix with passing size
it creates an identity matrix, which is a square matrix with ones on the main diagonal and zeros elsewhere.
Identity Matrix Example
10. Flatten Matrix
The flatten function converts a multi-dimensional array into a one-dimensional array.
Flatten
11. Transpose Matrxi
This swaps the dimensions of an array, making rows become columns and vice versa.
Example of Transpose Matrix
12. Concatenate
The concatenate
function joins two or more arrays along a specified axis.
Example of concatenation
13. Unique
The unique function finds and returns the unique elements of an array, often used to remove duplicates.
unique function
14. Sum Along Axis
The sum function can be used to add the elements of an array along a specified axis. This is useful for computing the sum of rows or columns.
Example of Sum Along Axis
Conclusion
NumPy is a fundamental library for anyone working with data in Python. Its array operations and mathematical functions make it a powerful tool for scientific computing. Understanding the basics of NumPy will set a solid foundation for more advanced topics in data science and machine learning.