0% found this document useful (0 votes)
4 views3 pages

Excercise Lab 4

Uploaded by

mallahzaib80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Excercise Lab 4

Uploaded by

mallahzaib80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

QUAID-E-AWAM UNIVERSITY

OF ENGINEERING, SCIENCE & TECHNOLOGY


NAWABSHAH, SINDH, PAKISTAN
Department of Cyber Security

Subject: Machine Learning Batch: 22 CYS


Lab # 03
Machine Learning

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.

3. Practice with Functions


a. Create a linearly spaced array with values from -1 to 1 and containing 7 elements.
b. Generate a 4x4 array filled with random values (use np.empty for practice).

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.

Exercises: Array Operations

1. Indexing and Slicing

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:

o Extract the first row.


o Extract the last column.
o Extract the 2x2 subarray in the top-left corner.
3. Advanced Indexing
Create an array with values from 20 to 40 in a 5x4 shape. Use indexing and slicing to:

o Retrieve every element in the second column.


o Select the last two rows and the last two columns.

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.

3. Stacking and Splitting Arrays

1. Horizontal and Vertical Stacking


Create two 1D arrays: arr1 with values [1, 2, 3] and arr2 with values [4, 5, 6].
o Horizontally stack arr1 and arr2.
o Vertically stack arr1 and arr2.
2. 2D Array Stacking
Create two 2D arrays with shapes (2, 3): arr1 with values [[1, 2, 3], [4, 5, 6]] and
arr2 with values [[7, 8, 9], [10, 11, 12]].

o Stack them horizontally to form a (2, 6) array.


o Stack them vertically to form a (4, 3) array.
3. Splitting Arrays into Sub-arrays

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.

3. Challenge Exercise: Mixed Operations


Create a 3D array with shape (3, 4, 2) filled with random values. Use indexing, slicing,
and reshaping to:

o Extract the first 2x2x2 sub-array.


o Reshape it into a 4x3 array.
o Split this reshaped array into three sub-arrays along the second axis.

2
3

You might also like