Перейти к содержимому

NumPy Beginner Practice – Level 5 | Insert, Delete & Manipulate Arrays

A.Kr.M.

0:00 / 0:00

NumPy Beginner Practice – Level 5 | Insert, Delete & Manipulate Arrays

0 просмотров · 5 дней назад
A.Kr.M.
0 просмотров · 5 дней назад
This is *Level 5* of my NumPy Beginner Practice series. In this practice session, I worked with NumPy arrays and practiced **inserting, deleting, and manipulating elements in 1-D and 2-D arrays**. 📚 Topics Practiced `np.insert()` – inserting an element at a specific index `np.delete()` – deleting an element using its index `np.append()` – adding rows or columns to a 2-D array Using `axis=0` and `axis=1` Deleting rows and columns from 2-D arrays Understanding how array shape changes during deletion Working with 1-D and 2-D NumPy arrays 🧪 Questions Practiced *Q21. Insert an Element* Insert `25` between `20` and `30`. *Q22. Delete an Element* Delete `30` and then delete the last element. *Q23. Insert into a 2-D Array* Add `[5, 6]` as a new row to `[[1,2],[3,4]]`. *Q24. Delete a Row and Column* Delete the middle row and then the middle column from a 3×3 array. ⚠️ Mistakes & Corrections *1. Q22: I initially missed part of the question.* I deleted `30`, but forgot that the question also asked me to delete the last element afterward. Correction: ```python arr = np.delete(arr, 2) arr = np.delete(arr, -1) ``` This was a reading mistake rather than a NumPy mistake. *2. Q23: I initially struggled with inserting a row into a 2-D array.* I tried to figure out the correct way to insert the new row and then used `np.append()` with `axis=0`. Correct approach: ```python arr = np.append(arr, [[5,6]], axis=0) ``` *3. Axis explanation correction* I initially described the axes using x/y directions, which was not the clearest way to think about them. The better rule to remember is: ```text axis=0 → rows axis=1 → columns ``` So: ```python np.append(arr, new_row, axis=0) # add a row np.append(arr, new_column, axis=1) # add a column ``` And for deletion: ```python np.delete(arr, index, axis=0) # delete a row np.delete(arr, index, axis=1) # delete a column ``` *4. `np.delete()` without an axis* I initially thought `np.delete(arr, 1)` meant deleting index `1` from a particular row. Correction: when `axis` is not specified, NumPy treats the array as flattened for the deletion. ```python np.delete(arr, 1) ``` removes the element at index `1` from the flattened array. *5. Irregular 2-D array* I also experimented with rows having different lengths. A normal NumPy 2-D numerical array should have a consistent rectangular shape. Removing elements can change an array's shape, but NumPy does not automatically preserve the original dimensions. 🎯 Main Takeaway The most important concept from this level: ```text axis=0 → ROWS axis=1 → COLUMNS ``` This is part of my ongoing Python → NumPy learning journey. *I’ll learn, practice, record, and upload.* No heavy editing, just a raw record of my learning and practice. #NumPy #Python #NumPyPractice #PythonLearning #DataScience #MachineLearning