Introduction to NumPy

NumPy is a powerful Python library used for numerical computing. It stands for Numerical Python and is widely used in Data Analytics, Data Science, Machine Learning, and scientific computing.

NumPy provides fast and efficient operations on large datasets using arrays.

Why Use NumPy?

  • Faster than Python lists
  • Supports large multi-dimensional arrays
  • Provides mathematical and statistical functions
  • Used as a foundation for libraries like pandas and matplotlib

Installing NumPy

If not installed, use:

pip install numpy

Import NumPy in Python:

import numpy as np

np is the common alias used for NumPy.

What is a NumPy Array?

A NumPy array is a grid of values stored in rows and columns.

Example of creating an array:

import numpy as nparr = np.array([1, 2, 3, 4, 5])
print(arr)

Output:

[1 2 3 4 5]

Creating Different Types of Arrays

Create a 2D array:

arr = np.array([[1, 2, 3], [4, 5, 6]])

Create an array of zeros:

np.zeros((3, 3))

Create an array of ones:

np.ones((2, 4))

Create a range of numbers:

np.arange(0, 10)

Create evenly spaced numbers:

np.linspace(0, 1, 5)

Array Attributes

Check shape (rows, columns):

arr.shape

Check number of dimensions:

arr.ndim

Check data type:

arr.dtype

Basic Mathematical Operations

Addition:

arr + 2

Multiplication:

arr * 3

Square:

arr ** 2

Statistical Functions

Mean:

np.mean(arr)

Sum:

np.sum(arr)

Minimum value:

np.min(arr)

Maximum value:

np.max(arr)

Standard deviation:

np.std(arr)

Why NumPy is Important in Analytics

  • Handles large datasets efficiently
  • Performs fast mathematical calculations
  • Supports matrix operations
  • Forms the base of pandas and many ML libraries

Key Takeaway

NumPy is the foundation of numerical computing in Python. If you want to work in Data Analytics or Data Science, mastering NumPy is essential for handling arrays and performing fast mathematical operations.

Home » PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Introduction to NumPy