Excercise Lab 4
Excercise Lab 4
Exercises
1. Array Creation
Create a NumPy array with values ranging from 10 to 30, incrementing by 5, and print the
array.
2. Array Attributes
Create a 3x3 array of ones. Check and print the array's shape, size, dimensions, and data
type.
4. Challenge Exercise
Create a 2D array from a list of lists. Change the shape of the array to 3x2 using the
.reshape() method and check its attributes (shape, size, dimensions, and data type) after
reshaping.
1. Basic Indexing
Create a 1D NumPy array containing values from 1 to 10. Use indexing to:
o Retrieve the first element.
o Retrieve the last element.
o Retrieve the element at index 5.
2. Slicing
Create a 2D NumPy array with values from 1 to 9 arranged in a 3x3 shape. Use slicing to:
1
2. Reshaping Arrays
1. Reshape a 1D Array
Create a 1D array with 12 elements (values from 0 to 11). Reshape this array into:
o A 3x4 array.
o A 4x3 array.
o A 2x2x3 array.
2. Check Shape Compatibility
Try reshaping a 1D array with 10 elements into a 3x3 shape. What happens? Explain why
this reshaping does or does not work.
3. Flattening an Array
Create a 2D array with shape (4, 5) filled with random integers. Use a method to flatten
this 2D array back into a 1D array.
1. Horizontal Splitting
Create a 2D array with values ranging from 1 to 12 arranged in a (3, 4) shape. Split this array
horizontally into two equal-sized sub-arrays.
2. Vertical Splitting
Using the same 2D array as above, split it vertically into two equal-sized sub-arrays.
2. Custom Splitting
Create a 1D array with values from 1 to 15. Split this array into three sub-arrays with
equal sizes.
2
3