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

NumPy Level 4 Practice | Shape & Reshaping | reshape(), ndim, shape, size & flatten()

A.Kr.M.

0:00 / 0:00

NumPy Level 4 Practice | Shape & Reshaping | reshape(), ndim, shape, size & flatten()

0 просмотров · 6 дней назад
A.Kr.M.
0 просмотров · 6 дней назад
Continuing my NumPy learning journey with **Level 4: Shape & Reshaping**. In this practice session, I solved questions covering: `reshape()` for changing the shape of an array Understanding `ndim` Checking array `shape` Checking array `size` Reshaping the same data into different valid dimensions Predicting `ndim`, `shape`, and `size` Flattening a 2-D array into a 1-D array Using `flatten()` with different order options Questions Practiced *Q16. Reshape* Created `np.arange(1,13)` and reshaped it into a `3 × 4` array. *Q17. Different Shapes* Reshaped the same 12 elements into: `2 × 6` `3 × 4` `4 × 3` `6 × 2` *Q18. Shape Detective* Predicted and verified: `ndim = 2` `shape = (2, 3)` `size = 6` *Q19. Flattening* Converted a `3 × 3` array into a 1-D array using NumPy's `flatten()` method. *Q20. Reshape Challenge* Created numbers from 1 to 24 and reshaped them into `4 × 6` and `3 × 8`. Mistakes / Things I Corrected I initially couldn't remember the exact NumPy method used for flattening an array. I tried to recall it from memory and eventually remembered `flatten()`. I also used: `arr.flatten("A")` This works, but my initial explanation of `"A"` was incomplete. `"A"` means NumPy chooses the flattening order based on the array's memory layout. For a normal C-contiguous array, it generally behaves like `"C"`. The simpler solution for this question was: `arr.flatten()` I also reminded myself that: `ndim` gives the number of dimensions/axes. `shape` gives the dimensions as a tuple, such as `(2, 3)`. `size` gives the total number of elements. There were no major errors in the other questions. This was mainly a practice session to strengthen my understanding of NumPy array shapes and reshaping. My Learning Approach I am following the same approach throughout this series: *Learn → Practice → Make mistakes → Correct them → Record → Upload* These are raw practice recordings from my NumPy learning journey, so I am keeping the mistakes and corrections instead of hiding them. This is part of my ongoing Python → NumPy learning journey. #NumPy #Python #PythonProgramming #NumPyPractice #DataScience #PythonLearning #ArrayReshaping