NumPy Basics: Essential Library for Data Science and Machine Learning

*Data Handling Basics Part 1: NumPy (Numerical Computing in Python)* 🔢 NumPy is one of the most important libraries for: - Data science - Machine learning - Scientific computing - Data analytics It provides fast mathematical operations on arrays. *1️⃣ Install NumPy* pip install numpy *2️⃣ Import NumPy* import numpy as np np is the standard alias. *3️⃣ Create NumPy Array* import numpy as np arr = np.array([1, 2, 3, 4]) print(arr) Output: [1 2 3 4] *4️⃣ NumPy vs Python List* Python list: a = [1,2,3] b = [4,5,6] print(a + b) Output: [1,2,3,4,5,6] NumPy array: import numpy as np a = np.array([1,2,3]) b = np.array([4,5,6]) print(a + b) Output: [5 7 9] NumPy performs element-wise operations. *5️⃣ Basic Array Operations* import numpy as np arr = np.array([1,2,3,4]) print(arr + 10) print(arr * 2) Output: [11 12 13 14] [2 4 6 8] *6️⃣ Useful NumPy Functions* import numpy as np arr = np.array([1,2,3,4]) print(np.mean(arr)) print(np.sum(arr)) print(np.max(arr)) print(np.min(arr)) Output example: 2.5 10 4 1 *7️⃣ Create Special Arrays* - Zeros array: `np.zeros(5)` - Ones array: `np.ones(4)` - Range array: `np.arange(1,10)` *8️⃣ 2D Arrays (Matrices)* import numpy as np arr = np.array([ [1,2,3], [4,5,6] ]) print(arr) Access element: `print(arr[0,1])` Output: 2 *Real Example: Student Marks Analysis* import numpy as np marks = np.array([78,85,90,66,72]) print("Average:", np.mean(marks)) print("Highest:", np.max(marks)) print("Lowest:", np.min(marks)) *Practice Tasks* 1. Create NumPy array of numbers 1–10 2. Add 5 to every element 3. Find mean and sum of array 4. Create 3×3 matrix 5. Find maximum value in array *✅ Practice Task Solutions — NumPy Basics* *Task 1. Create NumPy array of numbers 1–10* import numpy as np arr = np.arange(1, 11) print(arr) Output: [1 2 3 4 5 6 7 8 9 10] *Task 2. Add 5 to every element* import numpy as np arr = np.arange(1, 11) result = arr + 5 print(result) Output: [ 6 7 8 9 10 11 12 13 14 15] *Task 3. Find mean and sum of array* import numpy as np arr = np.array([1,2,3,4,5]) print("Sum:", np.sum(arr)) print("Mean:", np.mean(arr)) Output example: Sum: 15 Mean: 3.0 *Task 4. Create 3×3 matrix* import numpy as np matrix = np.array([ [1,2,3], [4,5,6], [7,8,9] ]) print(matrix) Output: [[1 2 3] [4 5 6] [7 8 9]] *Task 5. Find maximum value in array* import numpy as np arr = np.array([12,45,7,89,34]) print("Maximum:", np.max(arr)) Output: Maximum: 89 *✅ Key learning* - np.arange() → create range arrays - NumPy supports vectorized operations - np.mean() → average - np.sum() → total - np.max() → largest value *Double Tap ♥️ For More*

To view or add a comment, sign in

Explore content categories