NUMPY Tutorial



INTRODUCTION TO NUMPY


๐Ÿ“˜ Introduction to NumPy

NumPy (short for Numerical Python) is a powerful Python library used for numerical computations. It provides support for large multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on them efficiently.

Why Use NumPy? ๐Ÿค”

  • Highly efficient operations on arrays
  • Mathematical, logical, and statistical functions
  • Supports broadcasting and vectorization (faster execution)
  • Foundation for libraries like Pandas, TensorFlow, SciPy

๐Ÿงช Installing NumPy

pip install numpy

๐Ÿ”ง Importing NumPy

import numpy as np

๐Ÿ“Œ Creating NumPy Arrays

Arrays are the core of NumPy. Hereโ€™s how to create them:

import numpy as np

# 1D array
a = np.array([1, 2, 3])
print(a)

# 2D array
b = np.array([[1, 2], [3, 4]])
print(b)

๐Ÿ“ Array Properties

  • .ndim: Number of dimensions
  • .shape: Shape (rows, columns)
  • .size: Total number of elements
  • .dtype: Data type of array elements
Example:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Dimensions:", arr.ndim)
print("Shape:", arr.shape)
print("Size:", arr.size)
print("Data type:", arr.dtype)

๐Ÿงฎ Useful NumPy Functions

  • np.zeros((2, 3)) โ†’ Creates array of all zeros
  • np.ones((2, 3)) โ†’ Creates array of all ones
  • np.arange(0, 10, 2) โ†’ Array with step value
  • np.linspace(0, 1, 5) โ†’ 5 values between 0 and 1
  • np.random.rand(2, 2) โ†’ Random 2x2 matrix

๐Ÿ”„ Array Operations

NumPy makes mathematical operations easy:

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

print(a + b)      # [5 7 9]
print(a * b)      # [ 4 10 18]
print(np.dot(a, b)) # 32 (dot product)

๐Ÿ“Š Basic Statistics

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

print(np.mean(arr))  # 3.0
print(np.median(arr))  # 3.0
print(np.std(arr))   # Standard deviation
print(np.max(arr))   # Max value
Quick Tip ๐Ÿ’ก: NumPy arrays are faster and more memory-efficient than Python lists. Use them for any data-heavy task!

๐Ÿ“ฑ Mobile Friendly Suggestion:

Use Jupyter Notebook or Google Colab on mobile for writing and testing NumPy code while learning on the go.

โœ… Conclusion

NumPy is a must-know for anyone diving into Python-based data science, machine learning, or analytics. Mastering its basics lays a strong foundation for advanced tools like Pandas, SciPy, and TensorFlow.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review