{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 🏋️ NumPy Practice Exercise 1: Fundamentals & Intermediate Array Operations\n", "\n", "This notebook contains **20 progressive practice problems** designed to solidify your understanding of NumPy array creation, indexing, multi-dimensional slicing, broadcasting, ufuncs, and basic reductions.\n", "\n", "### 📝 Instructions:\n", "- Write your code in the provided code cells under each question.\n", "- Do not use Python standard loops (`for`, `while`) unless explicitly instructed; practice vectorized NumPy thinking!\n", "- When you are ready to verify your solutions, check the companion notebook: `03_numpy_exercise_1_solutions.ipynb`.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1: Import NumPy and Check Version\n", "\n", "Import NumPy using the standard alias `np` and print the installed version.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2: Create a 1D Array with Integer Range\n", "\n", "Create a 1D array of integers from 10 to 49 (inclusive).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3: Reverse an Array\n", "\n", "Reverse the 1D array created in Problem 2 using slicing (no loops or functions).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4: Create an Identity Matrix with Specific Dtype\n", "\n", "Create a 3x3 identity matrix where each element is a 32-bit floating point (`float32`).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 5: Evenly Spaced Points (Linspace)\n", "\n", "Create a 1D array of 30 evenly spaced numbers between 0 and 1 (inclusive). Print the step size between consecutive elements.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 6: Construct a Diagonal Matrix with Offset\n", "\n", "Create a 5x5 matrix with values `[1, 2, 3, 4]` positioned immediately below the main diagonal (offset k = -1).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 7: Standard Normal Matrix & Aggregate Statistics\n", "\n", "Using `np.random.default_rng(42)`, generate an 8x8 matrix drawn from a standard normal distribution. Compute and print its:\n", "- Minimum value\n", "- Maximum value\n", "- Mean\n", "- Standard Deviation\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 8: Replace Odd Numbers Without Modifying Original\n", "\n", "Given `arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])`, create a new array `out` where all odd numbers are replaced with -1, while leaving `arr` untouched.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 9: Multi-Dimensional Slicing & Shapes\n", "\n", "Given `mat = np.arange(36).reshape(6, 6)`, extract:\n", "1. The 4 corner elements as a (2, 2) array.\n", "2. The inner 4x4 matrix (excluding border rows and columns).\n", "3. The 3rd column, but keeping its shape as (6, 1) rather than (6,).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 10: Create an 8x8 Checkerboard Pattern\n", "\n", "Create an 8x8 matrix filled with 0s and 1s arranged in a checkerboard pattern (where 0 and 1 alternate both horizontally and vertically) using slicing and striding.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 11: Find Common Elements and Matching Positions\n", "\n", "Given two arrays:\n", "`a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])`\n", "`b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])`\n", "1. Find the common unique elements between `a` and `b`.\n", "2. Find the index positions where elements of `a` and `b` match exactly.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 12: Filter Array with Multiple Conditions\n", "\n", "Given `x = np.array([1, 5, 10, 15, 20, 25, 30, 35, 40])`, extract all values that are between 10 and 30 (inclusive) and strictly divisible by 5.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 13: Swap Rows in a 2D Matrix\n", "\n", "Given `mat = np.arange(16).reshape(4, 4)`, swap row 0 and row 2 in-place or via index reordering.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 14: Column-Wise Mean Subtraction (Broadcasting)\n", "\n", "Given a 5x3 matrix with random floats, subtract each column's mean from the corresponding column so that each resulting column has a mean of 0. Verify the result.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 15: Replace the Maximum Value with Zero\n", "\n", "Given a 1D array of 10 random numbers between 0 and 1, find the index of the maximum value using `argmax()` and set that value to 0.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 16: Find Mode and Frequency using `np.unique`\n", "\n", "Given `arr = np.array([4, 2, 7, 2, 5, 2, 9, 7, 7, 7, 3, 1, 7])`, find the most frequently occurring element (the mode) and its frequency using `np.unique`.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 17: Flattening: View vs Copy Demonstration\n", "\n", "Create a 3x3 matrix. Obtain a 1D version using `ravel()` and another using `flatten()`. Modify the first element of both. Observe and explain which one altered the original matrix.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 18: Stacking Arrays Horizontally, Vertically and Depth-wise\n", "\n", "Given `A = np.arange(4).reshape(2, 2)` and `B = np.ones((2, 2))`, produce:\n", "1. Vertical stack `(4, 2)`\n", "2. Horizontal stack `(2, 4)`\n", "3. Depth-wise stack `(2, 2, 2)`\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 19: Cumulative Sum and Product Along Axes\n", "\n", "Given `M = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])`, compute:\n", "1. Cumulative sum down columns (axis=0).\n", "2. Cumulative product across rows (axis=1).\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 20: Vectorized Value Clipping\n", "\n", "Given `arr = np.array([-2.5, -0.5, 0.2, 1.8, -1.2, 0.9, 2.3])`, clip all values to the range `[-1.0, 1.0]`:\n", "1. Using boolean masking / indexing.\n", "2. Using `np.clip`.\n", "Verify both yield identical results.\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Write your code below\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.11" } }, "nbformat": 4, "nbformat_minor": 4 }