What is NumPy?
(Reading time 10-12minutes)
NumPy (Numerical Python), an open-source Python toolkit for numerical and scientific computation, is powerful. It supports massive multi-dimensional arrays and matrices and gives mathematical algorithms to effectively control them.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Output:
[1 2 3 4 5]
Need of NumPy
Python lists are flexible but slow and inefficient for numerical operations (like matrix multiplication or element-wise addition).
Why Use NumPy?
| Feature |
Description |
| Speed |
NumPy is much faster than Python lists for numerical tasks. |
| Convenience |
Easy syntax for array and matrix operations. |
| Efficiency |
Uses less memory and executes faster due to C backend. |
| Mathematical tools |
Includes a large collection of built-in mathematical functions. |
| Integration |
Works well with other libraries (Pandas, Matplotlib, Scikit-learn, etc.) |
Common Functions Used with NumPy
Here’s a complete list of commonly used NumPy functions with their syntax and examples
1. Creating Arrays
| Function |
Description |
Syntax |
Example |
np.array() |
Create an array from a list or tuple |
np.array(object) |
np.array([1,2,3]) → [1 2 3] |
np.arange() |
Create array with range of values |
np.arange(start, stop, step) |
np.arange(0,10,2) → [0 2 4 6 8] |
np.linspace() |
Evenly spaced numbers between range |
np.linspace(start, stop, num) |
np.linspace(0,1,5) → [0. 0.25 0.5 0.75 1.] |
np.zeros() |
Create array of zeros |
np.zeros(shape) |
np.zeros((2,3)) → [[0. 0. 0.] [0. 0. 0.]] |
np.ones() |
Create array of ones |
np.ones(shape) |
np.ones((3,3)) |
np.eye() |
Identity matrix |
np.eye(n) |
np.eye(3) → 3×3 identity |
np.random.rand() |
Random numbers (0–1) |
np.random.rand(rows, cols) |
np.random.rand(2,2) |
2. Array Attributes
| Function |
Description |
Example |
arr.shape |
Returns dimensions |
arr.shape → (2,3) |
arr.size |
Total number of elements |
arr.size |
arr.ndim |
Number of dimensions |
arr.ndim |
arr.dtype |
Data type |
arr.dtype |
3. Reshaping and Modifying Arrays
| Function |
Description |
Syntax |
Example |
np.reshape() |
Change shape of array |
np.reshape(arr, new_shape) |
np.reshape(arr, (3,2)) |
arr.flatten() |
Convert multi-d array to 1D |
arr.flatten() |
|
np.concatenate() |
Join arrays |
np.concatenate((a,b)) |
|
np.hstack() |
Stack arrays horizontally |
np.hstack((a,b)) |
|
np.vstack() |
Stack arrays vertically |
np.vstack((a,b)) |
|
4. Mathematical Operations
| Function |
Description |
Example |
np.add(a,b) |
Element-wise addition |
[1,2,3] + [4,5,6] → [5 7 9] |
np.subtract(a,b) |
Subtraction |
[5,6,7] - [1,2,3] → [4 4 4] |
np.multiply(a,b) |
Multiplication |
|
np.divide(a,b) |
Division |
|
np.power(a,b) |
Power function |
np.power([2,3,4],2) → [4 9 16] |
np.sqrt(a) |
Square root |
np.sqrt([4,9,16]) → [2. 3. 4.] |
5. Statistical Functions
| Function |
Description |
Example |
np.mean(arr) |
Mean |
np.mean([1,2,3,4]) → 2.5 |
np.median(arr) |
Median |
np.median([1,2,3,4]) → 2.5 |
np.std(arr) |
Standard deviation |
|
np.var(arr) |
Variance |
|
np.sum(arr) |
Sum of elements |
|
np.min(arr) |
Minimum value |
|
np.max(arr) |
Maximum value |
|
6. Indexing and Slicing
| Operation |
Example |
Output |
arr[0] |
First element |
1 |
arr[-1] |
Last element |
|
arr[1:4] |
Elements from 1 to 3 |
|
arr[::2] |
Every second element |
|
7. Linear Algebra Operations
| Function |
Description |
|
np.dot(a,b) |
Matrix multiplication |
|
np.transpose(a) |
Transpose of matrix |
|
np.linalg.inv(a) |
nverse of matrix |
|
np.linalg.det(a) |
Determinant |
|
np.linalg.eig(a) |
Eigenvalues & eigenvectors |
|
8. Random Module Functions
| Function |
Description |
Example |
np.random.randint(low, high, size) |
Random integers |
np.random.randint(0,10,5) |
np.random.randn(n) |
Random numbers (normal distribution) |
|
np.random.choice(a) |
Random element from array |
|
Example Program
import numpy as np
# Create array
a = np.array([1, 2, 3, 4, 5])
# Operations
print("Mean:", np.mean(a))
print("Sum:", np.sum(a))
print("Square Root:", np.sqrt(a))
print("Reshape:", np.arange(6).reshape(2,3))
Output:
Mean: 3.0
Sum: 15
Square Root: [1. 1.414 1.732 2. 2.236]
Reshape:
[[0 1 2]
[3 4 5]]
| |
|
Summary
NumPy is the foundation of data science and machine learning in Python providing fast, efficient, and easy-to-use numerical computation tools.
Previous Next
Comments
Post a Comment