{"id":811,"date":"2018-02-26T19:52:03","date_gmt":"2018-02-26T19:52:03","guid":{"rendered":"https:\/\/machinelearningplus.com\/?p=811"},"modified":"2026-03-15T07:38:17","modified_gmt":"2026-03-15T07:38:17","slug":"101-numpy-exercises-python","status":"publish","type":"post","link":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/","title":{"rendered":"101 NumPy Exercises for Data Analysis (Python)"},"content":{"rendered":"<style>\ndetails summary {\n  cursor: pointer;\n  color: #2563eb;\n  text-decoration: underline;\n  text-underline-offset: 3px;\n}\ndetails summary:hover {\n  color: #1d4ed8;\n}\n.why-matters {\n  font-size: 0.82em;\n  color: #555;\n  margin-top: 2px;\n  margin-bottom: 6px;\n}\n<\/style>\n<p><em>The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest.<\/em><\/p>\n<p><img decoding=\"async\" alt=\"101 Numpy Exercises for Data Analysis. Photo by Ana Justin Luebke.\" src=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2018\/02\/101-numpy-exercises-1024x683.jpg\" \/><\/p>\n<p>If you want a quick refresher on numpy, the following tutorial is best:<br \/>\n<a href=\"https:\/\/machinelearningplus.com\/numpy-tutorial-part1-array-python-examples\">Numpy Tutorial Part 1: Introduction<\/a><br \/>\n<a href=\"https:\/\/machinelearningplus.com\/numpy-tutorial-python-part2\">Numpy Tutorial Part 2: Advanced numpy tutorials<\/a>.<\/p>\n<p><strong>Related Post:<\/strong><br \/>\n<a href=\"https:\/\/machinelearningplus.com\/python\/101-pandas-exercises-python\/\">101 Practice exercises with pandas<\/a>.<\/p>\n<p><strong>This is an interactive version \u2014 you can edit and run every code block directly in your browser. No installation needed.<\/strong> All code runs locally in your browser and nothing is sent to any server.<\/p>\n<p><strong>Click &#8216;Run&#8217; or press Ctrl+Enter on any code block to execute it. The first run may take a few seconds to initialize.<\/strong><\/p>\n<h2 id=\"1-import-numpy-and-check-the-version\">1. Import NumPy and Check the Version<\/h2>\n<p>Import numpy as <code>np<\/code> and print the version number.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Import numpy as np and print the version number\nimport numpy as np\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">1.13.3<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Verifying the NumPy version ensures compatibility with your code and helps debug version-specific behavior differences.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nprint(np.__version__)<\/pre>\n<\/details>\n<h2 id=\"2-create-a-1d-array\">2. Create a 1D Array<\/h2>\n<p>Create a 1D array of numbers from 0 to 9.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create a 1D array of numbers from 0 to 9\nimport numpy as np\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Creating sequential arrays is the starting point for indexing exercises, test data generation, and understanding how NumPy stores data in memory.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.arange(10)\nprint(arr)<\/pre>\n<\/details>\n<h2 id=\"3-create-a-boolean-array\">3. Create a Boolean Array<\/h2>\n<p>Create a 3&#215;3 numpy array of all <code>True<\/code> values.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create a 3x3 numpy array of all True's\nimport numpy as np\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ True  True  True]\n [ True  True  True]\n [ True  True  True]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Boolean arrays are used as masks for filtering data, and understanding how to create them is essential for conditional selection in data analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nresult = np.full((3, 3), True, dtype=bool)\nprint(result)\n\n# Alternate method:\n# np.ones((3,3), dtype=bool)<\/pre>\n<\/details>\n<h2 id=\"4-extract-items-that-satisfy-a-condition\">4. Extract Items That Satisfy a Condition<\/h2>\n<p>Extract all odd numbers from <code>arr<\/code>.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Extract all odd numbers from arr\nimport numpy as np\narr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([1, 3, 5, 7, 9])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Boolean indexing is the primary way to filter rows in data cleaning and subsetting datasets before analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\nresult = arr[arr % 2 == 1]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"5-replace-items-that-satisfy-a-condition\">5. Replace Items That Satisfy a Condition<\/h2>\n<p>Replace all odd numbers in <code>arr<\/code> with -1.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Replace all odd numbers in arr with -1\nimport numpy as np\narr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([ 0, -1,  2, -1,  4, -1,  6, -1,  8, -1])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Conditional replacement is used to handle outliers, recode categories, and clean invalid entries in datasets.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\narr[arr % 2 == 1] = -1\nprint(arr)<\/pre>\n<\/details>\n<h2 id=\"6-replace-items-without-affecting-the-original-array\">6. Replace Items Without Affecting the Original Array<\/h2>\n<p>Replace all odd numbers in <code>arr<\/code> with -1 without modifying the original <code>arr<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Replace all odd numbers with -1 without changing the original arr\nimport numpy as np\narr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">out: [ 0 -1  2 -1  4 -1  6 -1  8 -1]\narr: [0 1 2 3 4 5 6 7 8 9]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Preserving the original data while creating transformed copies is critical in ML pipelines where you need both raw and processed versions of features.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.arange(10)\n\nout = np.where(arr % 2 == 1, -1, arr)\nprint(\"out:\", out)\nprint(\"arr:\", arr)<\/pre>\n<\/details>\n<h2 id=\"7-reshape-an-array\">7. Reshape an Array<\/h2>\n<p>Convert the 1D array <code>arr<\/code> to a 2D array with 2 rows.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Convert a 1D array to a 2D array with 2 rows\nimport numpy as np\narr = np.arange(10)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[0 1 2 3 4]\n [5 6 7 8 9]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Reshaping is one of the most common operations when preparing data for ML models, which expect inputs in specific shapes (e.g., batches of samples).<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.arange(10)\n\nresult = arr.reshape(2, -1)  # Setting to -1 automatically decides the number of cols\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"8-stack-two-arrays-vertically\">8. Stack Two Arrays Vertically<\/h2>\n<p>Stack arrays <code>a<\/code> and <code>b<\/code> vertically.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Stack arrays a and b vertically\nimport numpy as np\na = np.arange(10).reshape(2,-1)\nb = np.repeat(1, 10).reshape(2,-1)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[0 1 2 3 4]\n [5 6 7 8 9]\n [1 1 1 1 1]\n [1 1 1 1 1]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Vertical stacking is used to combine datasets, append new observations to existing data, or merge training and validation sets.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\na = np.arange(10).reshape(2,-1)\nb = np.repeat(1, 10).reshape(2,-1)\n\n# Method 1:\nresult = np.concatenate([a, b], axis=0)\nprint(result)\n\n# Method 2:\n# np.vstack([a, b])\n\n# Method 3:\n# np.r_[a, b]<\/pre>\n<\/details>\n<h2 id=\"9-stack-two-arrays-horizontally\">9. Stack Two Arrays Horizontally<\/h2>\n<p>Stack arrays <code>a<\/code> and <code>b<\/code> horizontally.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Stack arrays a and b horizontally\nimport numpy as np\na = np.arange(10).reshape(2,-1)\nb = np.repeat(1, 10).reshape(2,-1)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[0 1 2 3 4 1 1 1 1 1]\n [5 6 7 8 9 1 1 1 1 1]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Horizontal stacking is used to add new features (columns) to a dataset, such as appending engineered features alongside existing ones.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\na = np.arange(10).reshape(2,-1)\nb = np.repeat(1, 10).reshape(2,-1)\n\n# Method 1:\nresult = np.concatenate([a, b], axis=1)\nprint(result)\n\n# Method 2:\n# np.hstack([a, b])\n\n# Method 3:\n# np.c_[a, b]<\/pre>\n<\/details>\n<h2 id=\"10-generate-custom-sequences-without-hardcoding\">10. Generate Custom Sequences Without Hardcoding<\/h2>\n<p>Using only numpy functions and the input array <code>a<\/code>, produce an array that first repeats each element 3 times, then tiles the whole array 3 times.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create the pattern without hardcoding using numpy functions\nimport numpy as np\na = np.array([1,2,3])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Programmatic sequence generation is essential for creating repeated patterns in simulations, data augmentation, and feature engineering.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\na = np.array([1,2,3])\n\nresult = np.r_[np.repeat(a, 3), np.tile(a, 3)]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"11-get-common-items-between-two-arrays\">11. Get Common Items Between Two Arrays<\/h2>\n<p>Find the common items between arrays <code>a<\/code> and <code>b<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Get the common items between a and b\nimport numpy as np\na = np.array([1,2,3,2,3,4,3,4,5,6])\nb = np.array([7,2,10,2,7,4,9,4,9,8])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[2 4]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Set intersection is used in data cleaning to find shared records, overlapping features, or matching IDs across datasets.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\na = np.array([1,2,3,2,3,4,3,4,5,6])\nb = np.array([7,2,10,2,7,4,9,4,9,8])\n\nresult = np.intersect1d(a, b)\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"12-remove-items-present-in-another-array\">12. Remove Items Present in Another Array<\/h2>\n<p>From array <code>a<\/code>, remove all items that are present in array <code>b<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: From array a remove all items present in array b\nimport numpy as np\na = np.array([1,2,3,4,5])\nb = np.array([5,6,7,8,9])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[1 2 3 4]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Set difference operations are used to exclude known values, filter out stop words, or remove already-processed records from a pipeline.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\na = np.array([1,2,3,4,5])\nb = np.array([5,6,7,8,9])\n\nresult = np.setdiff1d(a, b)\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"13-find-positions-where-two-arrays-match\">13. Find Positions Where Two Arrays Match<\/h2>\n<p>Get the positions (indices) where elements of <code>a<\/code> and <code>b<\/code> are equal.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Get the positions where elements of a and b match\nimport numpy as np\na = np.array([1,2,3,2,3,4,3,4,5,6])\nb = np.array([7,2,10,2,7,4,9,4,9,8])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">(array([1, 3, 5, 7]),)<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Finding matching positions is used in evaluation metrics (e.g., comparing predicted vs. actual labels) and aligning paired datasets.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\na = np.array([1,2,3,2,3,4,3,4,5,6])\nb = np.array([7,2,10,2,7,4,9,4,9,8])\n\nresult = np.where(a == b)\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"14-extract-numbers-within-a-given-range\">14. Extract Numbers Within a Given Range<\/h2>\n<p>From array <code>a<\/code>, extract all items between 5 and 10 (inclusive).<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Get all items between 5 and 10 from a\nimport numpy as np\na = np.array([2, 6, 1, 9, 10, 3, 27])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 6  9 10]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Range-based filtering is used for outlier removal, selecting data within valid bounds, and binning continuous variables.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\na = np.array([2, 6, 1, 9, 10, 3, 27])\n\n# Method 1\nresult = a[(a &gt;= 5) &amp; (a &lt;= 10)]\nprint(result)\n\n# Method 2:\n# index = np.where((a &gt;= 5) &amp; (a &lt;= 10))\n# print(a[index])\n\n# Method 3:\n# index = np.where(np.logical_and(a&gt;=5, a&lt;=10))\n# print(a[index])<\/pre>\n<\/details>\n<h2 id=\"15-vectorize-a-scalar-function-to-work-on-arrays\">15. Vectorize a Scalar Function to Work on Arrays<\/h2>\n<p>Convert the scalar function <code>maxx<\/code> into a vectorized version that works element-wise on arrays <code>a<\/code> and <code>b<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Vectorize the maxx function to work on arrays\nimport numpy as np\n\ndef maxx(x, y):\n    \"\"\"Get the maximum of two items\"\"\"\n    if x &gt;= y:\n        return x\n    else:\n        return y\n\na = np.array([5, 7, 9, 8, 6, 4, 5])\nb = np.array([6, 3, 4, 8, 9, 7, 1])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[6. 7. 9. 8. 9. 7. 5.]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Vectorizing custom functions lets you apply complex business logic across entire arrays without slow Python loops, which is critical for performance in large-scale data processing.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\ndef maxx(x, y):\n    \"\"\"Get the maximum of two items\"\"\"\n    if x &gt;= y:\n        return x\n    else:\n        return y\n\npair_max = np.vectorize(maxx, otypes=[float])\n\na = np.array([5, 7, 9, 8, 6, 4, 5])\nb = np.array([6, 3, 4, 8, 9, 7, 1])\n\nresult = pair_max(a, b)\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"16-swap-two-columns-in-a-2d-array\">16. Swap Two Columns in a 2D Array<\/h2>\n<p>Swap columns 1 and 2 in the array <code>arr<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Swap columns 1 and 2 in the array arr\nimport numpy as np\narr = np.arange(9).reshape(3,3)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[1 0 2]\n [4 3 5]\n [7 6 8]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Reordering columns is a common step when aligning feature order across datasets or when a model expects features in a specific sequence.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.arange(9).reshape(3,3)\n\nresult = arr[:, [1,0,2]]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"17-swap-two-rows-in-a-2d-array\">17. Swap Two Rows in a 2D Array<\/h2>\n<p>Swap rows 1 and 2 in the array <code>arr<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Swap rows 1 and 2 in the array arr\nimport numpy as np\narr = np.arange(9).reshape(3,3)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[3 4 5]\n [0 1 2]\n [6 7 8]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Row swapping is used in data shuffling, reordering observations, and implementing custom sorting logic for training data.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.arange(9).reshape(3,3)\n\nresult = arr[[1,0,2], :]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"18-reverse-the-rows-of-a-2d-array\">18. Reverse the Rows of a 2D Array<\/h2>\n<p>Reverse the row order of the 2D array <code>arr<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Reverse the rows of a 2D array\nimport numpy as np\narr = np.arange(9).reshape(3,3)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[6 7 8]\n [3 4 5]\n [0 1 2]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Reversing rows is used in image flipping for data augmentation and in time series analysis to process data from most recent to oldest.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.arange(9).reshape(3,3)\n\nresult = arr[::-1]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"19-reverse-the-columns-of-a-2d-array\">19. Reverse the Columns of a 2D Array<\/h2>\n<p>Reverse the column order of the 2D array <code>arr<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Reverse the columns of a 2D array\nimport numpy as np\narr = np.arange(9).reshape(3,3)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[2 1 0]\n [5 4 3]\n [8 7 6]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Column reversal is used in image mirroring for data augmentation and when reordering features for visualization.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\narr = np.arange(9).reshape(3,3)\n\nresult = arr[:, ::-1]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"20-create-a-2d-array-of-random-floats-between-5-and-10\">20. Create a 2D Array of Random Floats Between 5 and 10<\/h2>\n<p>Create a 2D array of shape 5&#215;3 containing random decimal numbers between 5 and 10.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create a 2D array of shape 5x3 with random floats between 5 and 10\nimport numpy as np\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 8.501  9.105  6.859]\n [ 9.763  9.877  7.135]\n [ 7.49   8.334  6.168]\n [ 7.75   9.945  5.274]\n [ 8.085  5.562  7.312]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Generating random arrays within a specific range is used in weight initialization for neural networks and in Monte Carlo simulations.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\n# Method 1:\nrand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3))\n# print(rand_arr)\n\n# Method 2:\nrand_arr = np.random.uniform(5, 10, size=(5,3))\nprint(rand_arr)<\/pre>\n<\/details>\n<h2 id=\"21-print-only-3-decimal-places\">21. Print Only 3 Decimal Places<\/h2>\n<p>Set NumPy print options so that the array <code>rand_arr<\/code> displays only 3 decimal places.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Print only 3 decimal places of the numpy array\nimport numpy as np\nrand_arr = np.random.random((5,3))\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 0.443  0.109  0.97 ]\n [ 0.388  0.447  0.191]\n [ 0.891  0.474  0.212]\n [ 0.609  0.518  0.403]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Controlling decimal precision makes array output readable during debugging and prevents noisy floating-point digits from cluttering your analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nrand_arr = np.random.random([5,3])\n\n# Limit to 3 decimal places\nnp.set_printoptions(precision=3)\nprint(rand_arr[:4])<\/pre>\n<\/details>\n<h2 id=\"22-suppress-scientific-notation-in-print-output\">22. Suppress Scientific Notation in Print Output<\/h2>\n<p>Pretty print <code>rand_arr<\/code> by suppressing scientific notation (like 1e10).<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Pretty print by suppressing scientific notation\nimport numpy as np\nnp.random.seed(100)\nrand_arr = np.random.random([3,3])\/1e3\nprint(rand_arr)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 0.000543  0.000278  0.000425]\n [ 0.000845  0.000005  0.000122]\n [ 0.000671  0.000826  0.000137]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Suppressing scientific notation makes small or large numbers human-readable in reports and logs, especially during exploratory data analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nnp.random.seed(100)\nrand_arr = np.random.random([3,3])\/1e3\n\nnp.set_printoptions(suppress=True, precision=6)\nprint(rand_arr)<\/pre>\n<\/details>\n<h2 id=\"23-limit-the-number-of-printed-items\">23. Limit the Number of Printed Items<\/h2>\n<p>Set NumPy print options so that array <code>a<\/code> displays a maximum of 6 elements, with the rest replaced by ellipsis.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Limit the number of items printed to a maximum of 6 elements\nimport numpy as np\na = np.arange(15)\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 0  1  2 ... 12 13 14]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Limiting printed output prevents your console from flooding when working with large arrays, making debugging faster and more manageable.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nnp.set_printoptions(threshold=6)\na = np.arange(15)\nprint(a)<\/pre>\n<\/details>\n<h2 id=\"24-print-the-full-array-without-truncating\">24. Print the Full Array Without Truncating<\/h2>\n<p>Print the full numpy array <code>a<\/code> without truncation, even when the print threshold is set low.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Print the full numpy array without truncating\nimport numpy as np\nnp.set_printoptions(threshold=6)\na = np.arange(15)\nprint(a)  # This truncates\n\n# Write your code below to print the full array\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Seeing every element is sometimes necessary for validating data integrity or inspecting small-to-medium arrays during debugging.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nimport sys\nnp.set_printoptions(threshold=6)\na = np.arange(15)\n\n# Solution\nnp.set_printoptions(threshold=sys.maxsize)\nprint(a)<\/pre>\n<\/details>\n<h2 id=\"25-import-a-dataset-with-mixed-types\">25. Import a Dataset With Mixed Types<\/h2>\n<p>Import the iris dataset from the URL, keeping the text (species) column intact alongside the numeric columns.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Import the iris dataset keeping the text intact\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[b'5.1' b'3.5' b'1.4' b'0.2' b'Iris-setosa']\n [b'4.9' b'3.0' b'1.4' b'0.2' b'Iris-setosa']\n [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Real-world datasets almost always mix numeric and categorical data, and loading them correctly is the first step in any data analysis pipeline.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Print the first 3 rows\nprint(iris[:3])<\/pre>\n<\/details>\n<h2 id=\"26-extract-a-particular-column-from-a-1d-structured-array\">26. Extract a Particular Column From a 1D Structured Array<\/h2>\n<p>Extract the text column <code>species<\/code> (5th field) from the 1D structured array <code>iris_1d<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Extract the species column from 1D iris array\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_1d = np.genfromtxt(url, delimiter=',', dtype=None)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">(150,)\n[b'Iris-setosa' b'Iris-setosa' b'Iris-setosa' b'Iris-setosa'\n b'Iris-setosa']<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Extracting specific columns from structured arrays is essential for isolating target labels or categorical features before model training.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_1d = np.genfromtxt(url, delimiter=',', dtype=None)\nprint(iris_1d.shape)\n\nspecies = np.array([row[4] for row in iris_1d])\nprint(species[:5])<\/pre>\n<\/details>\n<h2 id=\"27-convert-a-1d-structured-array-to-a-2d-numeric-array\">27. Convert a 1D Structured Array to a 2D Numeric Array<\/h2>\n<p>Convert the 1D structured array <code>iris_1d<\/code> to a 2D array <code>iris_2d<\/code> by omitting the <code>species<\/code> text field and keeping only the four numeric columns.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Convert 1D iris to 2D array by omitting species text field\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_1d = np.genfromtxt(url, delimiter=',', dtype=None)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 5.1  3.5  1.4  0.2]\n [ 4.9  3.   1.4  0.2]\n [ 4.7  3.2  1.3  0.2]\n [ 4.6  3.1  1.5  0.2]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> ML models require pure numeric feature matrices, so stripping text columns and converting to a 2D float array is a routine preprocessing step.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_1d = np.genfromtxt(url, delimiter=',', dtype=None)\n\n# Method 1: Convert each row to a list and get the first 4 items\niris_2d = np.array([row.tolist()[:4] for row in iris_1d])\nprint(iris_2d[:4])\n\n# Alt Method 2: Import only the first 4 columns from source url\n# iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n# print(iris_2d[:4])<\/pre>\n<\/details>\n<h2 id=\"28-compute-mean-median-and-standard-deviation\">28. Compute Mean, Median, and Standard Deviation<\/h2>\n<p>Find the mean, median, and standard deviation of the <code>sepallength<\/code> column (1st column) from the iris dataset.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find the mean, median, standard deviation of sepallength\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">5.84333333333 5.8 0.825301291785<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> These three summary statistics are the foundation of exploratory data analysis and are used to understand data distribution before building any model.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nsepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])\n\nmu, med, sd = np.mean(sepallength), np.median(sepallength), np.std(sepallength)\nprint(mu, med, sd)<\/pre>\n<\/details>\n<h2 id=\"29-normalize-an-array-to-the-0-1-range\">29. Normalize an Array to the 0-1 Range<\/h2>\n<p>Normalize the <code>sepallength<\/code> array so the minimum maps to 0 and the maximum maps to 1.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Normalize sepallength to range between 0 and 1\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nsepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 0.222  0.167  0.111  0.083  0.194  0.306  0.083  0.194  0.028  0.167\n  0.306  0.139  0.139  0.     0.417  0.389  0.306  0.222  0.389  0.222\n  0.306  0.222  0.083  0.222  0.139  0.194  0.194  0.25   0.25   0.111\n  0.139  0.306  0.25   0.333  0.167  0.194  0.333  0.167  0.028  0.222\n  0.194  0.056  0.028  0.194  0.222  0.139  0.222  0.083  0.278  0.194\n  0.75   0.583  0.722  0.333  0.611  0.389  0.556  0.167  0.639  0.25\n  0.194  0.444  0.472  0.5    0.361  0.667  0.361  0.417  0.528  0.361\n  0.444  0.5    0.556  0.5    0.583  0.639  0.694  0.667  0.472  0.389\n  0.333  0.333  0.417  0.472  0.306  0.472  0.667  0.556  0.361  0.333\n  0.333  0.5    0.417  0.194  0.361  0.389  0.389  0.528  0.222  0.389\n  0.556  0.417  0.778  0.556  0.611  0.917  0.167  0.833  0.667  0.806\n  0.611  0.583  0.694  0.389  0.417  0.583  0.611  0.944  0.944  0.472\n  0.722  0.361  0.944  0.556  0.667  0.806  0.528  0.5    0.583  0.806\n  0.861  1.     0.583  0.556  0.5    0.944  0.556  0.583  0.472  0.722\n  0.667  0.722  0.417  0.694  0.667  0.667  0.556  0.611  0.528  0.444]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Min-max normalization is a standard preprocessing step for ML algorithms (e.g., KNN, SVM) that are sensitive to feature scales.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nsepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])\n\nSmax, Smin = sepallength.max(), sepallength.min()\nS = (sepallength - Smin) \/ (Smax - Smin)\nprint(S)<\/pre>\n<\/details>\n<h2 id=\"30-compute-the-softmax-score\">30. Compute the Softmax Score<\/h2>\n<p>Compute the softmax scores for the <code>sepallength<\/code> array.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute the softmax score of sepallength\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nsepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 0.002  0.002  0.001  0.001  0.002  0.003  0.001  0.002  0.001  0.002\n  0.003  0.002  0.002  0.001  0.004  0.004  0.003  0.002  0.004  0.002\n  0.003  0.002  0.001  0.002  0.002  0.002  0.002  0.002  0.002  0.001\n  0.002  0.003  0.002  0.003  0.002  0.002  0.003  0.002  0.001  0.002\n  0.002  0.001  0.001  0.002  0.002  0.002  0.002  0.001  0.003  0.002\n  0.015  0.008  0.013  0.003  0.009  0.004  0.007  0.002  0.01   0.002\n  0.002  0.005  0.005  0.006  0.004  0.011  0.004  0.004  0.007  0.004\n  0.005  0.006  0.007  0.006  0.008  0.01   0.012  0.011  0.005  0.004\n  0.003  0.003  0.004  0.005  0.003  0.005  0.011  0.007  0.004  0.003\n  0.003  0.006  0.004  0.002  0.004  0.004  0.004  0.007  0.002  0.004\n  0.007  0.004  0.016  0.007  0.009  0.027  0.002  0.02   0.011  0.018\n  0.009  0.008  0.012  0.004  0.004  0.008  0.009  0.03   0.03   0.005\n  0.013  0.004  0.03   0.007  0.011  0.018  0.007  0.006  0.008  0.018\n  0.022  0.037  0.008  0.007  0.006  0.03   0.007  0.008  0.005  0.013\n  0.011  0.013  0.004  0.012  0.011  0.011  0.007  0.009  0.007  0.005]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Softmax converts raw scores into probabilities and is the final activation function in most classification neural networks.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nsepallength = np.array([float(row[0]) for row in iris])\n\ndef softmax(x):\n    \"\"\"Compute softmax values for each sets of scores in x.\"\"\"\n    e_x = np.exp(x - np.max(x))\n    return e_x \/ e_x.sum(axis=0)\n\nprint(softmax(sepallength))<\/pre>\n<\/details>\n<h2 id=\"31-find-percentile-scores\">31. Find Percentile Scores<\/h2>\n<p>Find the 5th and 95th percentile of the <code>sepallength<\/code> array.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find the 5th and 95th percentile of sepallength\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nsepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 4.6    7.255]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Percentiles are used to detect outliers, set clipping thresholds, and define confidence intervals in statistical analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nsepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])\n\nresult = np.percentile(sepallength, q=[5, 95])\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"32-insert-values-at-random-positions\">32. Insert Values at Random Positions<\/h2>\n<p>Insert <code>np.nan<\/code> at 20 random positions in the <code>iris_2d<\/code> dataset.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Insert np.nan at 20 random positions in iris_2d\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[b'5.1' b'3.5' b'1.4' b'0.2' b'Iris-setosa']\n [b'4.9' b'3.0' b'1.4' b'0.2' b'Iris-setosa']\n [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']\n [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']\n [b'5.0' b'3.6' b'1.4' b'0.2' b'Iris-setosa']\n [b'5.4' b'3.9' b'1.7' b'0.4' b'Iris-setosa']\n [b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']\n [b'5.0' b'3.4' b'1.5' b'0.2' b'Iris-setosa']\n [b'4.4' nan b'1.4' b'0.2' b'Iris-setosa']\n [b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Injecting missing values is used to simulate real-world incomplete data when testing how your cleaning pipeline handles gaps.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Method 1\nnp.random.seed(100)\niris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan\n\n# Print first 10 rows\nprint(iris_2d[:10])<\/pre>\n<\/details>\n<h2 id=\"33-find-the-position-of-missing-values\">33. Find the Position of Missing Values<\/h2>\n<p>Find the number and position of missing values in <code>iris_2d<\/code>&#8216;s <code>sepallength<\/code> (1st column).<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find number and position of missing values in sepallength\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\nnp.random.seed(100)\niris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Number of missing values:\n 5\nPosition of missing values:\n (array([ 39,  88,  99, 130, 147]),)<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Locating missing values is the first step in any data cleaning workflow &#8212; you need to know where gaps exist before deciding how to fill or drop them.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\nnp.random.seed(100)\niris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan\n\nprint(\"Number of missing values: \\n\", np.isnan(iris_2d[:, 0]).sum())\nprint(\"Position of missing values: \\n\", np.where(np.isnan(iris_2d[:, 0])))<\/pre>\n<\/details>\n<h2 id=\"34-filter-a-numpy-array-based-on-multiple-conditions\">34. Filter a NumPy Array Based on Multiple Conditions<\/h2>\n<p>Filter the rows of <code>iris_2d<\/code> where <code>petallength<\/code> (3rd column) &gt; 1.5 and <code>sepallength<\/code> (1st column) &lt; 5.0.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Filter rows where petallength &gt; 1.5 and sepallength &lt; 5.0\nimport numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 4.8  3.4  1.6  0.2]\n [ 4.8  3.4  1.9  0.2]\n [ 4.7  3.2  1.6  0.2]\n [ 4.8  3.1  1.6  0.2]\n [ 4.9  2.4  3.3  1. ]\n [ 4.9  2.5  4.5  1.7]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Multi-condition filtering is used constantly in data analysis to subset records that meet specific criteria, such as selecting high-risk patients or underperforming products.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n\ncondition = (iris_2d[:, 2] &gt; 1.5) &amp; (iris_2d[:, 0] &lt; 5.0)\nresult = iris_2d[condition]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"35-drop-rows-containing-missing-values\">35. Drop Rows Containing Missing Values<\/h2>\n<p>Select the rows of <code>iris_2d<\/code> that do not have any <code>nan<\/code> value, keeping only complete rows.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Drop rows containing any nan value from iris_2d\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n\n# Introduce some nan values for the exercise\niris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([[ 4.9,  3. ,  1.4,  0.2],\n       [ 4.7,  3.2,  1.3,  0.2],\n       [ 4.6,  3.1,  1.5,  0.2],\n       [ 5. ,  3.6,  1.4,  0.2],\n       [ 5.4,  3.9,  1.7,  0.4]])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Dropping rows with missing values is a standard data cleaning step before training ML models that cannot handle NaN inputs.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\niris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan\n\n# Method 1:\nany_nan_in_row = np.array([~np.any(np.isnan(row)) for row in iris_2d])\nprint(iris_2d[any_nan_in_row][:5])\n\n# Method 2: (By Rong)\nresult = iris_2d[np.sum(np.isnan(iris_2d), axis=1) == 0][:5]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"36-find-correlation-between-two-columns\">36. Find Correlation Between Two Columns<\/h2>\n<p>Compute the Pearson correlation between SepalLength (1st column) and PetalLength (3rd column) in <code>iris_2d<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find correlation between SepalLength and PetalLength\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">0.871754157305<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Correlation analysis is a core step in exploratory data analysis and feature selection for ML models.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nfrom scipy.stats.stats import pearsonr\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n\n# Solution 1\nprint(np.corrcoef(iris[:, 0], iris[:, 2])[0, 1])\n\n# Solution 2\ncorr, p_value = pearsonr(iris[:, 0], iris[:, 2])\nprint(corr)<\/pre>\n<\/details>\n<h2 id=\"37-check-for-missing-values-in-an-array\">37. Check for Missing Values in an Array<\/h2>\n<p>Determine whether <code>iris_2d<\/code> has any missing (<code>nan<\/code>) values and print the boolean result.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Check if iris_2d has any missing values\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">False<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Detecting missing values early prevents silent errors in downstream computations like aggregation and model training.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\n\nprint(np.isnan(iris_2d).any())<\/pre>\n<\/details>\n<h2 id=\"38-replace-all-missing-values-with-zero\">38. Replace All Missing Values with Zero<\/h2>\n<p>Replace all <code>nan<\/code> values in <code>iris_2d<\/code> with <code>0<\/code> and print the first four rows.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Replace all nan values with 0\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\niris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([[ 5.1,  3.5,  1.4,  0. ],\n       [ 4.9,  3. ,  1.4,  0.2],\n       [ 4.7,  3.2,  1.3,  0.2],\n       [ 4.6,  3.1,  1.5,  0.2]])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Imputing missing values with zero is a quick baseline strategy in data cleaning pipelines before more sophisticated imputation.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])\niris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan\n\n# Solution\niris_2d[np.isnan(iris_2d)] = 0\nprint(iris_2d[:4])<\/pre>\n<\/details>\n<h2 id=\"39-count-unique-values-in-an-array\">39. Count Unique Values in an Array<\/h2>\n<p>Find the unique values and their counts in the <code>species<\/code> column of the iris dataset.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find unique values and their counts in iris species\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">(array([b'Iris-setosa', b'Iris-versicolor', b'Iris-virginica'],\n       dtype='|S15'), array([50, 50, 50]))<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Checking class distribution is essential before training classifiers to detect imbalanced datasets.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Extract the species column\nspecies = np.array([row.tolist()[4] for row in iris])\n\n# Get the unique values and the counts\nprint(np.unique(species, return_counts=True))<\/pre>\n<\/details>\n<h2 id=\"40-bin-a-numeric-column-into-categories\">40. Bin a Numeric Column into Categories<\/h2>\n<p>Bin the petal length (3rd column) of <code>iris<\/code> into a text array using these rules: less than 3 becomes <code>'small'<\/code>, 3 to 5 becomes <code>'medium'<\/code>, and 5 or above becomes <code>'large'<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Bin petal length into 'small', 'medium', 'large' categories\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">['small', 'small', 'small', 'small']<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Binning continuous features into categories is a common feature engineering technique for decision trees and rule-based models.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Bin petallength\npetal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10])\n\n# Map it to respective category\nlabel_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}\npetal_length_cat = [label_map[x] for x in petal_length_bin]\n\n# View\nprint(petal_length_cat[:4])<\/pre>\n<\/details>\n<h2 id=\"41-create-a-new-column-from-existing-columns\">41. Create a New Column from Existing Columns<\/h2>\n<p>Add a new column to <code>iris_2d<\/code> for volume, computed as <code>(pi x petallength x sepallength^2) \/ 3<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create a volume column from existing columns\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa', 38.13265162927291],\n       [b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa', 35.200498485922445],\n       [b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa', 30.0723720777127],\n       [b'4.6', b'3.1', b'1.5', b'0.2', b'Iris-setosa', 33.238050274980004]], dtype=object)<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Deriving new features from existing columns is a fundamental feature engineering step that can improve model performance.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris_2d = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Compute volume\nsepallength = iris_2d[:, 0].astype('float')\npetallength = iris_2d[:, 2].astype('float')\nvolume = (np.pi * petallength * (sepallength**2)) \/ 3\n\n# Introduce new dimension to match iris_2d's\nvolume = volume[:, np.newaxis]\n\n# Add the new column\nout = np.hstack([iris_2d, volume])\n\n# View\nprint(out[:4])<\/pre>\n<\/details>\n<h2 id=\"42-probabilistic-sampling-from-a-categorical-array\">42. Probabilistic Sampling from a Categorical Array<\/h2>\n<p>Randomly sample from <code>iris<\/code>&#8216;s <code>species<\/code> column such that <code>setosa<\/code> appears twice as often as <code>versicolor<\/code> and <code>virginica<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Probabilistic sampling with setosa twice as likely\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">(array([b'Iris-setosa', b'Iris-versicolor', b'Iris-virginica'], dtype=object), array([77, 37, 36]))<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Probabilistic sampling is used in class balancing, bootstrapping, and data augmentation for imbalanced datasets.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Get the species column\nspecies = iris[:, 4]\n\n# Approach 1: Generate Probabilistically\nnp.random.seed(100)\na = np.array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'])\nspecies_out = np.random.choice(a, 150, p=[0.5, 0.25, 0.25])\n\n# Approach 2: Probabilistic Sampling (preferred)\nnp.random.seed(100)\nprobs = np.r_[np.linspace(0, 0.500, num=50), np.linspace(0.501, .750, num=50), np.linspace(.751, 1.0, num=50)]\nindex = np.searchsorted(probs, np.random.random(150))\nspecies_out = species[index]\nprint(np.unique(species_out, return_counts=True))<\/pre>\n<\/details>\n<h2 id=\"43-get-the-second-largest-value-by-group\">43. Get the Second Largest Value by Group<\/h2>\n<p>Find the second longest <code>petallength<\/code> among species <code>setosa<\/code> in the <code>iris<\/code> dataset.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find the second longest petallength of species setosa\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">1.7<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Computing grouped statistics beyond simple max\/min is common in exploratory data analysis and outlier detection.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Get the species and petal length columns\npetal_len_setosa = iris[iris[:, 4] == b'Iris-setosa', [2]].astype('float')\n\n# Get the second last value\nprint(np.unique(np.sort(petal_len_setosa))[-2])<\/pre>\n<\/details>\n<h2 id=\"44-sort-a-2d-array-by-a-column\">44. Sort a 2D Array by a Column<\/h2>\n<p>Sort the <code>iris<\/code> dataset based on the <code>sepallength<\/code> column (1st column) in ascending order.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Sort iris dataset by sepallength (1st column)\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[b'4.3' b'3.0' b'1.1' b'0.1' b'Iris-setosa']\n [b'4.4' b'3.2' b'1.3' b'0.2' b'Iris-setosa']\n [b'4.4' b'3.0' b'1.3' b'0.2' b'Iris-setosa']\n [b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa']\n [b'4.5' b'2.3' b'1.3' b'0.3' b'Iris-setosa']\n [b'4.6' b'3.6' b'1.0' b'0.2' b'Iris-setosa']\n [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']\n [b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']\n [b'4.6' b'3.2' b'1.4' b'0.2' b'Iris-setosa']\n [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Sorting datasets by a specific feature is fundamental for ranking, binary search, and ordered visualizations.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Sort by column position 0: SepalLength\nprint(iris[iris[:,0].argsort()][:20])<\/pre>\n<\/details>\n<h2 id=\"45-find-the-most-frequent-value-in-an-array\">45. Find the Most Frequent Value in an Array<\/h2>\n<p>Find the most frequent value of petal length (3rd column) in the <code>iris<\/code> dataset.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find the most frequent petal length value\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">b'1.5'<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Identifying the mode of a feature helps detect dominant patterns and is used in imputation strategies.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Solution\nvals, counts = np.unique(iris[:, 2], return_counts=True)\nprint(vals[np.argmax(counts)])<\/pre>\n<\/details>\n<h2 id=\"46-find-the-first-occurrence-greater-than-a-value\">46. Find the First Occurrence Greater Than a Value<\/h2>\n<p>Find the position of the first occurrence of a value greater than 1.0 in the petalwidth (4th column) of the <code>iris<\/code> dataset.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find position of first value > 1.0 in petalwidth column\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">50<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Locating threshold crossings is used in signal processing, event detection, and conditional filtering of datasets.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\n\n# Solution\nprint(np.argwhere(iris[:, 3].astype(float) > 1.0)[0])<\/pre>\n<\/details>\n<h2 id=\"47-clip-array-values-to-a-range\">47. Clip Array Values to a Range<\/h2>\n<p>From the array <code>a<\/code>, replace all values greater than 30 with 30 and all values less than 10 with 10.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Clip values to range [10, 30]\nimport numpy as np\n\nnp.random.seed(100)\na = np.random.uniform(1, 50, 20)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 27.63  14.64  21.8   30.    10.    10.    30.    30.    10.    29.18  30.\n  11.25  10.08  10.    11.77  30.    30.    10.    30.    14.43]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Clipping values to a valid range is essential for outlier handling and ensuring inputs stay within expected bounds for models.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.set_printoptions(precision=2)\nnp.random.seed(100)\na = np.random.uniform(1, 50, 20)\n\n# Solution 1: Using np.clip\nprint(np.clip(a, a_min=10, a_max=30))\n\n# Solution 2: Using np.where\nprint(np.where(a < 10, 10, np.where(a > 30, 30, a)))<\/pre>\n<\/details>\n<h2 id=\"48-get-the-positions-of-top-n-values\">48. Get the Positions of Top N Values<\/h2>\n<p>Get the positions of the top 5 maximum values in the array <code>a<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find positions of top 5 maximum values\nimport numpy as np\n\nnp.random.seed(100)\na = np.random.uniform(1, 50, 20)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[18 7 3 10 15]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Finding top-N indices is used in ranking systems, recommendation engines, and selecting the highest-confidence predictions.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(100)\na = np.random.uniform(1, 50, 20)\n\n# Solution 1:\nprint(a.argsort()[-5:])\n\n# Solution 2:\nprint(np.argpartition(-a, 5)[:5])\n\n# Get the actual values:\nprint(a[a.argsort()][-5:])<\/pre>\n<\/details>\n<h2 id=\"49-compute-row-wise-counts-of-all-possible-values\">49. Compute Row-Wise Counts of All Possible Values<\/h2>\n<p>For each row in <code>arr<\/code>, count the occurrences of every value from 1 to 10.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Count occurrences of each value (1-10) per row\nimport numpy as np\n\nnp.random.seed(100)\narr = np.random.randint(1, 11, size=(6, 10))\nprint(arr)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 1  2  3  4  5  6  7  8  9 10]\n[[1, 0, 2, 1, 1, 1, 0, 2, 2, 0],\n [2, 1, 3, 0, 1, 0, 1, 0, 1, 1],\n [0, 3, 0, 2, 3, 1, 0, 1, 0, 0],\n [1, 0, 2, 1, 0, 1, 0, 2, 1, 2],\n [2, 2, 2, 0, 0, 1, 1, 1, 1, 0],\n [1, 1, 1, 1, 1, 2, 0, 0, 2, 1]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Row-wise value counting is used in bag-of-words representations and histogram-based feature extraction.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(100)\narr = np.random.randint(1, 11, size=(6, 10))\nprint(arr)\n\n# Solution\ndef counts_of_all_values_rowwise(arr2d):\n    # Unique values and its counts row wise\n    num_counts_array = [np.unique(row, return_counts=True) for row in arr2d]\n\n    # Counts of all values row wise\n    return([[int(b[a==i]) if i in a else 0 for i in np.unique(arr2d)] for a, b in num_counts_array])\n\n# Print\nprint(np.arange(1, 11))\nresult = counts_of_all_values_rowwise(arr)\nfor row in result:\n    print(row)<\/pre>\n<\/details>\n<h2 id=\"50-flatten-an-array-of-arrays-into-a-1d-array\">50. Flatten an Array of Arrays into a 1D Array<\/h2>\n<p>Convert <code>array_of_arrays<\/code> (which contains <code>arr1<\/code>, <code>arr2<\/code>, and <code>arr3<\/code>) into a single flat 1D array.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Flatten an array of arrays into a 1d array\nimport numpy as np\n\narr1 = np.arange(3)\narr2 = np.arange(3, 7)\narr3 = np.arange(7, 10)\n\narray_of_arrays = np.array([arr1, arr2, arr3], dtype=object)\nprint(array_of_arrays)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[0 1 2 3 4 5 6 7 8 9]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Flattening nested array structures is a routine step when combining data from multiple sources into a single feature vector.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\narr1 = np.arange(3)\narr2 = np.arange(3, 7)\narr3 = np.arange(7, 10)\n\narray_of_arrays = np.array([arr1, arr2, arr3], dtype=object)\nprint('array_of_arrays: ', array_of_arrays)\n\n# Solution 1\narr_2d = np.array([a for arr in array_of_arrays for a in arr])\nprint(arr_2d)\n\n# Solution 2:\narr_2d = np.concatenate(array_of_arrays)\nprint(arr_2d)<\/pre>\n<\/details>\n<h2 id=\"51-generate-one-hot-encodings-for-an-array\">51. Generate One-Hot Encodings for an Array<\/h2>\n<p>Compute one-hot encodings (dummy binary variables) for each unique value in <code>arr<\/code>.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create one-hot encodings for the array\nimport numpy as np\n\nnp.random.seed(101)\narr = np.random.randint(1, 4, size=6)\nprint(arr)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([[ 0.,  1.,  0.],\n       [ 0.,  0.,  1.],\n       [ 0.,  1.,  0.],\n       [ 0.,  1.,  0.],\n       [ 0.,  1.,  0.],\n       [ 1.,  0.,  0.]])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> One-hot encoding converts categorical integers into binary vectors, which is required by most ML algorithms that expect numeric input.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(101)\narr = np.random.randint(1, 4, size=6)\nprint(arr)\n\n# Solution 1:\ndef one_hot_encodings(arr):\n    uniqs = np.unique(arr)\n    out = np.zeros((arr.shape[0], uniqs.shape[0]))\n    for i, k in enumerate(arr):\n        out[i, k-1] = 1\n    return out\n\nprint(one_hot_encodings(arr))\n\n# Method 2:\nprint((arr[:, None] == np.unique(arr)).view(np.int8))<\/pre>\n<\/details>\n<h2 id=\"52-create-row-numbers-grouped-by-a-categorical-variable\">52. Create Row Numbers Grouped by a Categorical Variable<\/h2>\n<p>Assign within-group row numbers to each element in <code>species_small<\/code>, restarting the count at 0 for each new species.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create row numbers grouped by species\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nspecies = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)\nnp.random.seed(100)\nspecies_small = np.sort(np.random.choice(species, size=20))\nprint(species_small)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Grouped row numbering is useful for creating sequence features and ranking within categories in data analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nspecies = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)\nnp.random.seed(100)\nspecies_small = np.sort(np.random.choice(species, size=20))\n\n# Solution\nprint([i for val in np.unique(species_small) for i, grp in enumerate(species_small[species_small==val])])<\/pre>\n<\/details>\n<h2 id=\"53-create-group-ids-from-a-categorical-variable\">53. Create Group IDs from a Categorical Variable<\/h2>\n<p>Assign a numeric group ID (0, 1, 2, &#8230;) to each element in <code>species_small<\/code> based on its unique species category.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create group ids for each species category\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nspecies = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)\nnp.random.seed(100)\nspecies_small = np.sort(np.random.choice(species, size=20))\nprint(species_small)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Converting categorical labels to integer group IDs is a standard preprocessing step for label encoding in ML pipelines.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\nspecies = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)\nnp.random.seed(100)\nspecies_small = np.sort(np.random.choice(species, size=20))\n\n# Solution\noutput = []\nuniqs = np.unique(species_small)\n\nfor val in uniqs:  # uniq values in group\n    for s in species_small[species_small==val]:  # each element in group\n        groupid = np.argwhere(uniqs == s).tolist()[0][0]  # groupid\n        output.append(groupid)\n\nprint(output)<\/pre>\n<\/details>\n<h2 id=\"54-rank-items-in-a-1d-array\">54. Rank Items in a 1D Array<\/h2>\n<p>Create the ranks for each value in the numeric array <code>a<\/code>, where rank 0 is the smallest value.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create ranks for the array values\nimport numpy as np\n\nnp.random.seed(10)\na = np.random.randint(20, size=10)\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[4 2 6 0 8 7 9 3 5 1]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Ranking transforms are used in non-parametric statistics and for creating ordinal features from numeric data.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(10)\na = np.random.randint(20, size=10)\nprint('Array: ', a)\n\n# Solution\nprint(a.argsort().argsort())<\/pre>\n<\/details>\n<h2 id=\"55-rank-items-in-a-multidimensional-array\">55. Rank Items in a Multidimensional Array<\/h2>\n<p>Create a rank array of the same shape as the 2D array <code>a<\/code>, where ranking is applied globally across all elements.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create ranks for a 2D array (global ranking)\nimport numpy as np\n\nnp.random.seed(10)\na = np.random.randint(20, size=[2, 5])\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[4 2 6 0 8]\n [7 9 3 5 1]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Global ranking across a matrix is useful for percentile-based normalization and creating competition-style leaderboards.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(10)\na = np.random.randint(20, size=[2, 5])\nprint(a)\n\n# Solution\nprint(a.ravel().argsort().argsort().reshape(a.shape))<\/pre>\n<\/details>\n<h2 id=\"56-find-the-maximum-value-in-each-row\">56. Find the Maximum Value in Each Row<\/h2>\n<p>Compute the maximum value for each row in the 2D array <code>a<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find maximum value in each row\nimport numpy as np\n\nnp.random.seed(100)\na = np.random.randint(1, 10, [5, 3])\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([9, 8, 6, 3, 9])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Row-wise max is used in neural network output processing (e.g., selecting the predicted class) and feature aggregation.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(100)\na = np.random.randint(1, 10, [5, 3])\nprint(a)\n\n# Solution 1\nprint(np.amax(a, axis=1))\n\n# Solution 2\nprint(np.apply_along_axis(np.max, arr=a, axis=1))<\/pre>\n<\/details>\n<h2 id=\"57-compute-the-min-by-max-ratio-for-each-row\">57. Compute the Min-by-Max Ratio for Each Row<\/h2>\n<p>Compute the ratio of the minimum to the maximum value for each row in the 2D array <code>a<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute min\/max ratio for each row\nimport numpy as np\n\nnp.random.seed(100)\na = np.random.randint(1, 10, [5, 3])\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([ 0.44444444,  0.125     ,  0.5       ,  1.        ,  0.11111111])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Min\/max ratios help measure the spread within each observation, which is useful for anomaly detection and data quality checks.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(100)\na = np.random.randint(1, 10, [5, 3])\nprint(a)\n\n# Solution\nprint(np.apply_along_axis(lambda x: np.min(x)\/np.max(x), arr=a, axis=1))<\/pre>\n<\/details>\n<h2 id=\"58-find-duplicate-records-in-an-array\">58. Find Duplicate Records in an Array<\/h2>\n<p>Mark duplicate entries in array <code>a<\/code> as <code>True<\/code> (2nd occurrence onwards), with first occurrences marked as <code>False<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find duplicate entries (mark 2nd+ occurrences as True)\nimport numpy as np\n\nnp.random.seed(100)\na = np.random.randint(0, 5, 10)\nprint('Array: ', a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[False  True False  True False False  True  True  True  True]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Identifying duplicates is a critical data cleaning step to prevent data leakage and inflated counts in analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(100)\na = np.random.randint(0, 5, 10)\n\n# Solution\n# Create an all True array\nout = np.full(a.shape[0], True)\n\n# Find the index positions of unique elements\nunique_positions = np.unique(a, return_index=True)[1]\n\n# Mark those positions as False\nout[unique_positions] = False\n\nprint(out)<\/pre>\n<\/details>\n<h2 id=\"59-find-the-grouped-mean\">59. Find the Grouped Mean<\/h2>\n<p>Compute the mean of <code>sepalwidth<\/code> (2nd column) grouped by <code>species<\/code> (5th column) in the <code>iris<\/code> dataset.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find mean sepalwidth grouped by species\nimport numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[b'Iris-setosa', 3.418],\n [b'Iris-versicolor', 2.770],\n [b'Iris-virginica', 2.974]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Grouped aggregation is a fundamental operation in exploratory data analysis and feature engineering for structured data.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nurl = 'https:\/\/archive.ics.uci.edu\/ml\/machine-learning-databases\/iris\/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')\n\n# Solution\nnumeric_column = iris[:, 1].astype('float')  # sepalwidth\ngrouping_column = iris[:, 4]  # species\n\n# List comprehension version\noutput = [[group_val, numeric_column[grouping_column==group_val].mean()] for group_val in np.unique(grouping_column)]\n\nprint(output)<\/pre>\n<\/details>\n<h2 id=\"60-convert-a-pil-image-to-a-numpy-array\">60. Convert a PIL Image to a NumPy Array<\/h2>\n<p>Import the image from the given URL and convert it to a numpy array, printing its shape.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Import an image from URL and convert to numpy array\nimport numpy as np\nfrom io import BytesIO\nfrom PIL import Image\nimport PIL, requests\n\nURL = 'https:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/8b\/Denali_Mt_McKinley.jpg'\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">A numpy array representation of the image with shape (height, width, 3)<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Converting images to numpy arrays is the first step in any image processing or computer vision pipeline.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nfrom io import BytesIO\nfrom PIL import Image\nimport PIL, requests\n\n# Import image from URL\nURL = 'https:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/8b\/Denali_Mt_McKinley.jpg'\nresponse = requests.get(URL)\n\n# Read it as Image\nI = Image.open(BytesIO(response.content))\n\n# Optionally resize\nI = I.resize([150, 150])\n\n# Convert to numpy array\narr = np.asarray(I)\nprint(arr.shape)\n\n# Optionally convert it back to an image\nim = PIL.Image.fromarray(np.uint8(arr))\nprint(type(arr))<\/pre>\n<\/details>\n<h2 id=\"61-drop-all-missing-values-from-a-1d-array\">61. Drop All Missing Values from a 1D Array<\/h2>\n<p>Remove all <code>nan<\/code> values from the 1D array <code>a<\/code> and return only the valid elements.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Drop all nan values from a 1D array\nimport numpy as np\n\na = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan])\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([ 1.,  2.,  3.,  5.,  6.,  7.])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Stripping NaN values from arrays is necessary before computing statistics or passing data to functions that do not handle missing values.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan])\n\n# Solution\nprint(a[~np.isnan(a)])<\/pre>\n<\/details>\n<h2 id=\"62-compute-the-euclidean-distance-between-two-arrays\">62. Compute the Euclidean Distance Between Two Arrays<\/h2>\n<p>Compute the Euclidean distance between arrays <code>a<\/code> and <code>b<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute euclidean distance between two arrays\nimport numpy as np\n\na = np.array([1, 2, 3, 4, 5])\nb = np.array([4, 5, 6, 7, 8])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">6.7082039324993694<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Euclidean distance is the foundation of KNN, K-means clustering, and many similarity-based ML algorithms.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.array([1, 2, 3, 4, 5])\nb = np.array([4, 5, 6, 7, 8])\n\n# Solution\ndist = np.linalg.norm(a - b)\nprint(dist)<\/pre>\n<\/details>\n<h2 id=\"63-find-all-local-maxima-peaks-in-a-1d-array\">63. Find All Local Maxima (Peaks) in a 1D Array<\/h2>\n<p>Find the positions of all peaks in array <code>a<\/code>, where peaks are values surrounded by smaller values on both sides.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find positions of all peaks (local maxima) in the array\nimport numpy as np\n\na = np.array([1, 3, 7, 1, 2, 6, 0, 1])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([2, 5])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Peak detection is widely used in signal processing, time-series analysis, and identifying turning points in financial data.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.array([1, 3, 7, 1, 2, 6, 0, 1])\n\ndoublediff = np.diff(np.sign(np.diff(a)))\npeak_locations = np.where(doublediff == -2)[0] + 1\nprint(peak_locations)<\/pre>\n<\/details>\n<h2 id=\"64-subtract-a-1d-array-from-a-2d-array-row-wise\">64. Subtract a 1D Array from a 2D Array Row-Wise<\/h2>\n<p>Subtract each element of <code>b_1d<\/code> from the corresponding row of <code>a_2d<\/code>, so that <code>b_1d[0]<\/code> is subtracted from all elements in row 0, <code>b_1d[1]<\/code> from row 1, and so on.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Subtract 1d array from 2d array row-wise\nimport numpy as np\n\na_2d = np.array([[3, 3, 3], [4, 4, 4], [5, 5, 5]])\nb_1d = np.array([1, 2, 3])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[2 2 2]\n [2 2 2]\n [2 2 2]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Row-wise subtraction using broadcasting is essential for centering data and computing deviations from row-level baselines.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na_2d = np.array([[3, 3, 3], [4, 4, 4], [5, 5, 5]])\nb_1d = np.array([1, 2, 3])\n\n# Solution\nprint(a_2d - b_1d[:, None])<\/pre>\n<\/details>\n<h2 id=\"65-find-the-index-of-the-nth-repetition-of-a-value\">65. Find the Index of the N&#8217;th Repetition of a Value<\/h2>\n<p>Find the index of the 5th repetition of the value <code>1<\/code> in array <code>x<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Find the index of the 5th occurrence of value 1\nimport numpy as np\n\nx = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">8<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Locating the n&#8217;th occurrence of a value is useful for event-based analysis and pattern matching in sequential data.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nx = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])\nn = 5\n\n# Solution 1: List comprehension\nprint([i for i, v in enumerate(x) if v == 1][n-1])\n\n# Solution 2: Numpy version\nprint(np.where(x == 1)[0][n-1])<\/pre>\n<\/details>\n<h2 id=\"66-convert-numpy-datetime64-to-python-datetime\">66. Convert NumPy datetime64 to Python datetime<\/h2>\n<p>Convert the numpy <code>datetime64<\/code> object <code>dt64<\/code> to a Python <code>datetime.datetime<\/code> object.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Convert numpy datetime64 to Python datetime\nimport numpy as np\n\ndt64 = np.datetime64('2018-02-25 22:10:10')\nprint(dt64)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">datetime.datetime(2018, 2, 25, 22, 10, 10)<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Converting between NumPy and Python datetime types is necessary when interfacing with libraries that only accept native Python datetime objects.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nfrom datetime import datetime\n\ndt64 = np.datetime64('2018-02-25 22:10:10')\n\n# Solution 1\nprint(dt64.tolist())\n\n# Solution 2\nprint(dt64.astype(datetime))<\/pre>\n<\/details>\n<h2 id=\"67-compute-the-moving-average-of-an-array\">67. Compute the Moving Average of an Array<\/h2>\n<p>Compute the moving average of array <code>Z<\/code> with a window size of 3.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute moving average with window size 3\nimport numpy as np\n\nnp.random.seed(100)\nZ = np.random.randint(10, size=10)\nprint(Z)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array:  [8 8 3 7 7 0 4 2 5 2]\nmoving average:  [ 6.33  6.    5.67  4.67  3.67  2.    3.67  3.  ]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Moving averages smooth noisy data and are widely used in time-series forecasting, signal processing, and financial analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\ndef moving_average(a, n=3):\n    ret = np.cumsum(a, dtype=float)\n    ret[n:] = ret[n:] - ret[:-n]\n    return ret[n - 1:] \/ n\n\nnp.random.seed(100)\nZ = np.random.randint(10, size=10)\nprint('array: ', Z)\n\n# Method 1\nprint('moving average: ', moving_average(Z, n=3).round(2))\n\n# Method 2: using convolve\nprint('moving average: ', np.convolve(Z, np.ones(3)\/3, mode='valid').round(2))<\/pre>\n<\/details>\n<h2 id=\"68-create-an-array-sequence-given-start-length-and-step\">68. Create an Array Sequence Given Start, Length, and Step<\/h2>\n<p>Create a numpy array of length 10, starting from 5, with a step of 3 between consecutive numbers using the variables <code>start<\/code>, <code>length<\/code>, and <code>step<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create array sequence with start=5, length=10, step=3\nimport numpy as np\n\nlength = 10\nstart = 5\nstep = 3\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">array([ 5,  8, 11, 14, 17, 20, 23, 26, 29, 32])<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Generating custom sequences is used for creating feature grids, time steps, and index ranges in scientific computing.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nlength = 10\nstart = 5\nstep = 3\n\ndef seq(start, length, step):\n    end = start + (step * length)\n    return np.arange(start, end, step)\n\nprint(seq(start, length, step))<\/pre>\n<\/details>\n<h2 id=\"69-fill-in-missing-dates-in-an-irregular-date-series\">69. Fill in Missing Dates in an Irregular Date Series<\/h2>\n<p>Given the array <code>dates<\/code> containing every other day, fill in the missing dates to create a continuous daily sequence.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Fill in missing dates to make a continuous date sequence\nimport numpy as np\n\ndates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-25'), 2)\nprint(dates)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">['2018-02-01' '2018-02-02' '2018-02-03' '2018-02-04'\n '2018-02-05' '2018-02-06' '2018-02-07' '2018-02-08'\n '2018-02-09' '2018-02-10' '2018-02-11' '2018-02-12'\n '2018-02-13' '2018-02-14' '2018-02-15' '2018-02-16'\n '2018-02-17' '2018-02-18' '2018-02-19' '2018-02-20'\n '2018-02-21' '2018-02-22' '2018-02-23']<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Filling gaps in date sequences is a standard step in time-series preprocessing to ensure regular intervals for forecasting models.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\n# Input\ndates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-25'), 2)\nprint(dates)\n\n# Solution\nfilled_in = np.array([np.arange(date, (date+d)) for date, d in zip(dates, np.diff(dates))]).reshape(-1)\n\n# Add the last day\noutput = np.hstack([filled_in, dates[-1]])\nprint(output)<\/pre>\n<\/details>\n<h2 id=\"70-create-strides-from-a-1d-array\">70. Create Strides from a 1D Array<\/h2>\n<p>From the array <code>arr<\/code>, generate a 2D matrix using strides with a window length of 4 and stride of 2, producing rows like <code>[0,1,2,3], [2,3,4,5], [4,5,6,7]...<\/code>.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create a 2D matrix of strides with window length 4 and stride 2\nimport numpy as np\n\narr = np.arange(15)\nprint(arr)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 0  1  2  3]\n [ 2  3  4  5]\n [ 4  5  6  7]\n [ 6  7  8  9]\n [ 8  9 10 11]\n [10 11 12 13]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Sliding window views via strides are used in convolutional neural networks, rolling statistics, and efficient time-series feature extraction.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\narr = np.arange(15)\n\ndef gen_strides(a, stride_len=5, window_len=5):\n    n_strides = ((a.size - window_len) \/\/ stride_len) + 1\n    return np.array([a[s:(s+window_len)] for s in np.arange(0, n_strides*stride_len, stride_len)])\n\nprint(gen_strides(arr, stride_len=2, window_len=4))<\/pre>\n<\/details>\n<h2 id=\"71-how-to-create-an-array-using-a-function-of-its-row-and-column-indices\">71. How to create an array using a function of its row and column indices?<\/h2>\n<p>Create a 4&#215;5 array where each element equals the sum of its row and column index using <code>np.fromfunction<\/code>.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create a 4x5 array where element (i,j) = i + j using np.fromfunction\nimport numpy as np\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[0 1 2 3 4]\n [1 2 3 4 5]\n [2 3 4 5 6]\n [3 4 5 6 7]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> This is handy when the value at each array position follows a mathematical formula based on its coordinates.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\narr = np.fromfunction(lambda i, j: i + j, (4, 5), dtype=int)\nprint(arr)<\/pre>\n<\/details>\n<h2 id=\"72-how-does-array-broadcasting-work\">72. How does array broadcasting work?<\/h2>\n<p>Add the column vector <code>a<\/code> (shape 3&#215;1) and row vector <code>b<\/code> (shape 3,) using broadcasting to produce a 3&#215;3 result.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Add a column vector and row vector using broadcasting\nimport numpy as np\n\na = np.array([[1], [2], [3]])   # shape (3, 1)\nb = np.array([10, 20, 30])      # shape (3,)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[11 21 31]\n [12 22 32]\n [13 23 33]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Broadcasting lets NumPy do math on arrays of different shapes without making copies, which is fundamental to efficient vectorized computation.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.array([[1], [2], [3]])   # shape (3, 1)\nb = np.array([10, 20, 30])      # shape (3,)\n\n# Broadcasting automatically expands a (3,1) + (3,) -> (3,3)\nresult = a + b\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"73-how-to-create-coordinate-grids-with-npmeshgrid\">73. How to create coordinate grids with np.meshgrid?<\/h2>\n<p>Create coordinate matrices <code>X<\/code> and <code>Y<\/code> from arrays <code>x<\/code> and <code>y<\/code> using <code>np.meshgrid<\/code>, and print both.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create coordinate matrices X and Y from x and y arrays\nimport numpy as np\n\nx = np.array([1, 2, 3])\ny = np.array([4, 5])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">X:\n[[1 2 3]\n [1 2 3]]\nY:\n[[4 4 4]\n [5 5 5]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Coordinate grids are essential for evaluating functions over a 2D plane, such as plotting 3D surfaces or computing heatmaps.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nx = np.array([1, 2, 3])\ny = np.array([4, 5])\n\nX, Y = np.meshgrid(x, y)\nprint('X:')\nprint(X)\nprint('Y:')\nprint(Y)<\/pre>\n<\/details>\n<h2 id=\"74-how-to-standardize-columns-of-a-2d-array-zero-mean-unit-variance\">74. How to standardize columns of a 2D array (zero mean, unit variance)?<\/h2>\n<p>Standardize each column of the random array <code>a<\/code> so that each column has mean 0 and standard deviation 1, then verify by printing the resulting means and stds.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Standardize each column of a 2D array to mean=0, std=1\nimport numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 100, size=(5, 3))\nprint('Original:')\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Means after standardization: [-0.  0.  0.]\nStds after standardization: [1. 1. 1.]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Standardization is a critical preprocessing step in ML &#8212; features on different scales (e.g., age vs salary) can distort model training if not standardized.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 100, size=(5, 3))\nprint('Original:')\nprint(a)\n\n# Standardize: subtract column mean, divide by column std\ncol_mean = a.mean(axis=0)\ncol_std = a.std(axis=0)\nstandardized = (a - col_mean) \/ col_std\n\nprint('Means after standardization:', np.round(standardized.mean(axis=0), 1))\nprint('Stds after standardization:', np.round(standardized.std(axis=0), 1))<\/pre>\n<\/details>\n<h2 id=\"75-how-to-create-and-use-structured-arrays\">75. How to create and use structured arrays?<\/h2>\n<p>Create a structured array with fields <code>name<\/code> (string), <code>age<\/code> (int), and <code>weight<\/code> (float) containing three records, then extract and print the <code>name<\/code> field.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create a structured array with name, age, weight fields\nimport numpy as np\n\n# Write your code below\n# Create the dtype and the structured array, then extract names\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[('Alice', 25, 55.5) ('Bob', 30, 75. ) ('Charlie', 35, 65.2)]\nNames: ['Alice' 'Bob' 'Charlie']<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Structured arrays let you store heterogeneous data types in a single NumPy array, serving as a lightweight alternative to pandas DataFrames.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\ndt = np.dtype([('name', 'U10'), ('age', 'i4'), ('weight', 'f8')])\narr = np.array([('Alice', 25, 55.5), ('Bob', 30, 75.0), ('Charlie', 35, 65.2)], dtype=dt)\nprint(arr)\nprint('Names:', arr['name'])<\/pre>\n<\/details>\n<h2 id=\"76-how-to-use-fancy-indexing-to-select-specific-elements\">76. How to use fancy indexing to select specific elements?<\/h2>\n<p>Select elements at positions <code>(0,1)<\/code>, <code>(1,3)<\/code>, and <code>(3,4)<\/code> from the 4&#215;5 array <code>a<\/code> using fancy indexing with row and column index arrays.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Select elements at (0,1), (1,3), (3,4) using fancy indexing\nimport numpy as np\n\na = np.arange(20).reshape(4, 5)\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[ 1  8 19]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Fancy indexing lets you grab arbitrary combinations of elements from an array, unlike regular slicing which only gives contiguous blocks.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.arange(20).reshape(4, 5)\n\nrows = np.array([0, 1, 3])\ncols = np.array([1, 3, 4])\nresult = a[rows, cols]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"77-how-to-compute-cumulative-sum-and-cumulative-product\">77. How to compute cumulative sum and cumulative product?<\/h2>\n<p>Compute and print the cumulative sum and cumulative product of the array <code>a = [1, 2, 3, 4, 5]<\/code> using <code>np.cumsum<\/code> and <code>np.cumprod<\/code>.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute cumulative sum and cumulative product\nimport numpy as np\n\na = np.array([1, 2, 3, 4, 5])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Cumulative sum: [ 1  3  6 10 15]\nCumulative product: [  1   2   6  24 120]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Running totals and cumulative products are essential for time series analysis, computing factorials, and tracking cumulative returns.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.array([1, 2, 3, 4, 5])\n\nprint('Cumulative sum:', np.cumsum(a))\nprint('Cumulative product:', np.cumprod(a))<\/pre>\n<\/details>\n<h2 id=\"78-how-to-compute-cumulative-sum-along-a-specific-axis-of-a-2d-array\">78. How to compute cumulative sum along a specific axis of a 2D array?<\/h2>\n<p>Compute the cumulative sum of the 2D array <code>a<\/code> along <code>axis=0<\/code> (down columns) and <code>axis=1<\/code> (across rows), and print both results.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute cumulative sum along axis=0 and axis=1\nimport numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 10, size=(3, 4))\nprint('Array:')\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Cumsum along rows (axis=0):\n[[ 7  4  8  5]\n [14  7 15 13]\n [19 11 23 21]]\nCumsum along columns (axis=1):\n[[ 7 11 19 24]\n [ 7 10 17 25]\n [ 5  9 17 25]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Axis-specific cumulative sums are important when processing tabular data where rows and columns represent different dimensions, such as cumulative sales by month and region.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 10, size=(3, 4))\nprint('Array:')\nprint(a)\n\nprint('Cumsum along rows (axis=0):')\nprint(np.cumsum(a, axis=0))\n\nprint('Cumsum along columns (axis=1):')\nprint(np.cumsum(a, axis=1))<\/pre>\n<\/details>\n<h2 id=\"79-how-to-compute-a-dot-product-using-npeinsum\">79. How to compute a dot product using np.einsum?<\/h2>\n<p>Compute the dot product of arrays <code>a = [1, 2, 3]<\/code> and <code>b = [4, 5, 6]<\/code> using <code>np.einsum<\/code> with the subscript string <code>'i,i-&gt;'<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute the dot product using np.einsum\nimport numpy as np\n\na = np.array([1, 2, 3])\nb = np.array([4, 5, 6])\n\n# Write your code below\n# Hint: the einsum subscript string for dot product is 'i,i->'\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">32<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> `np.einsum` is a powerful one-liner for expressing array operations using index notation, and mastering it replaces dozens of specialized NumPy calls.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.array([1, 2, 3])\nb = np.array([4, 5, 6])\n\n# 'i,i->' means: multiply element-wise, then sum all (scalar output)\nresult = np.einsum('i,i->', a, b)\nprint(result)\n\n# Equivalent to:\n# np.dot(a, b)  -> 32<\/pre>\n<\/details>\n<h2 id=\"80-how-to-multiply-two-matrices-using-npeinsum\">80. How to multiply two matrices using np.einsum?<\/h2>\n<p>Multiply matrices <code>A<\/code> and <code>B<\/code> using <code>np.einsum<\/code> with the subscript string <code>'ij,jk-&gt;ik'<\/code> and print the result.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Matrix multiplication using np.einsum\nimport numpy as np\n\nA = np.array([[1, 2], [3, 4]])\nB = np.array([[5, 6], [7, 8]])\n\n# Write your code below\n# Hint: the einsum subscript string for matrix multiply is 'ij,jk->ik'\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[19 22]\n [43 50]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Understanding einsum for matrix multiplication is the gateway to expressing far more complex tensor operations (batch matmul, traces, contractions) in one readable line.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nA = np.array([[1, 2], [3, 4]])\nB = np.array([[5, 6], [7, 8]])\n\n# 'ij,jk->ik' means: sum over j (the shared index)\nresult = np.einsum('ij,jk->ik', A, B)\nprint(result)\n\n# Equivalent to:\n# A @ B  or  np.matmul(A, B)<\/pre>\n<\/details>\n<h2 id=\"81-how-to-apply-a-custom-function-to-every-row-or-column-of-an-array\">81. How to apply a custom function to every row or column of an array?<\/h2>\n<p>Compute the range (max minus min) of each row in array <code>a<\/code> using <code>np.apply_along_axis<\/code> with <code>axis=1<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute range (max - min) of each row using np.apply_along_axis\nimport numpy as np\n\nnp.random.seed(50)\na = np.random.randint(1, 20, size=(4, 5))\nprint('Array:')\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Range per row: [16  9 13 14]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> `np.apply_along_axis` lets you run any custom summary function across rows or columns when NumPy doesn&#8217;t provide a built-in for it.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(50)\na = np.random.randint(1, 20, size=(4, 5))\nprint('Array:')\nprint(a)\n\nresult = np.apply_along_axis(lambda x: x.max() - x.min(), axis=1, arr=a)\nprint('Range per row:', result)<\/pre>\n<\/details>\n<h2 id=\"82-how-to-get-the-rank-of-each-element-in-an-array\">82. How to get the rank of each element in an array?<\/h2>\n<p>Get the rank (sorted position) of each element in array <code>a<\/code> using the double argsort trick: <code>a.argsort().argsort()<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Get the rank of each element using double argsort\nimport numpy as np\n\nnp.random.seed(10)\na = np.random.randint(20, size=10)\nprint('Array:', a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Ranks: [4 2 6 0 8 7 9 3 5 1]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Ranking is used in non-parametric statistics, percentile calculations, and rank-based feature engineering for ML models.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(10)\na = np.random.randint(20, size=10)\nprint('Array:', a)\n\n# Double argsort trick: first argsort gives sort order,\n# second argsort gives the rank of each element\nranks = a.argsort().argsort()\nprint('Ranks:', ranks)<\/pre>\n<\/details>\n<h2 id=\"83-how-to-compute-matrix-determinant-and-inverse\">83. How to compute matrix determinant and inverse?<\/h2>\n<p>Compute and print the determinant and inverse of matrix <code>A = [[1, 2], [3, 4]]<\/code> using <code>np.linalg.det<\/code> and <code>np.linalg.inv<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute the determinant and inverse of matrix A\nimport numpy as np\n\nA = np.array([[1, 2], [3, 4]])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Determinant: -2.0\nInverse:\n[[-2.   1. ]\n [ 1.5 -0.5]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Determinants and matrix inverses are fundamental in linear regression, solving linear systems, and many ML algorithms like Gaussian processes.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nA = np.array([[1, 2], [3, 4]])\n\ndet = np.linalg.det(A)\nprint('Determinant:', round(det, 1))\n\ninv = np.linalg.inv(A)\nprint('Inverse:')\nprint(inv)<\/pre>\n<\/details>\n<h2 id=\"84-how-to-solve-a-system-of-linear-equations-with-numpy\">84. How to solve a system of linear equations with NumPy?<\/h2>\n<p>Solve the system <code>3x + y = 9<\/code>, <code>x + 2y = 8<\/code> by passing the coefficient matrix <code>A<\/code> and constants vector <code>b<\/code> to <code>np.linalg.solve<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Solve the system 3x + y = 9, x + 2y = 8\nimport numpy as np\n\n# Coefficient matrix and constants vector\nA = np.array([[3, 1], [1, 2]])\nb = np.array([9, 8])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Solution [x, y]: [2. 3.]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Solving `Ax = b` underpins linear regression, circuit analysis, and many optimization problems in engineering and data science.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nA = np.array([[3, 1], [1, 2]])\nb = np.array([9, 8])\n\nx = np.linalg.solve(A, b)\nprint('Solution [x, y]:', x)<\/pre>\n<\/details>\n<h2 id=\"85-how-to-compute-eigenvalues-and-eigenvectors\">85. How to compute eigenvalues and eigenvectors?<\/h2>\n<p>Compute and print the eigenvalues and eigenvectors of matrix <code>A = [[4, -2], [1, 1]]<\/code> using <code>np.linalg.eig<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute eigenvalues and eigenvectors of matrix A\nimport numpy as np\n\nA = np.array([[4, -2], [1, 1]])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Eigenvalues: [3. 2.]\nEigenvectors:\n[[ 0.89442719  0.70710678]\n [ 0.4472136   0.70710678]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Eigendecomposition is the backbone of PCA &#8212; it reveals the directions of maximum variance in your data and how much variance each direction explains.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nA = np.array([[4, -2], [1, 1]])\n\neigenvalues, eigenvectors = np.linalg.eig(A)\nprint('Eigenvalues:', eigenvalues)\nprint('Eigenvectors:')\nprint(eigenvectors)<\/pre>\n<\/details>\n<h2 id=\"86-how-to-fit-a-polynomial-curve-to-noisy-data\">86. How to fit a polynomial curve to noisy data?<\/h2>\n<p>Fit a degree-2 polynomial to the noisy data arrays <code>x<\/code> and <code>y<\/code> using <code>np.polyfit<\/code>, then evaluate it with <code>np.polyval<\/code> and print the fitted coefficients and first 5 predicted values.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Fit a quadratic polynomial to noisy data and recover coefficients\nimport numpy as np\n\nnp.random.seed(42)\nx = np.linspace(0, 5, 10)\ny = 2*x**2 - 3*x + 1 + np.random.randn(10)*0.5\n\n# Write your code below\n# Fit a degree-2 polynomial and print the coefficients\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Fitted coefficients [a, b, c]: [ 1.98 -2.89  1.15]\nFirst 5 predicted values: [1.15 0.16 0.38 1.83 4.49]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Polynomial curve fitting is used for trend lines, simple regression, and approximating nonlinear relationships in data.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\nx = np.linspace(0, 5, 10)\ny = 2*x**2 - 3*x + 1 + np.random.randn(10)*0.5\n\n# Fit a 2nd degree polynomial: y = ax^2 + bx + c\ncoeffs = np.polyfit(x, y, 2)\nprint('Fitted coefficients [a, b, c]:', np.round(coeffs, 2))\n\n# Evaluate the fitted polynomial at the original x values\ny_pred = np.polyval(coeffs, x)\nprint('First 5 predicted values:', np.round(y_pred[:5], 2))<\/pre>\n<\/details>\n<h2 id=\"87-how-to-select-rows-with-a-boolean-mask-and-specific-columns-at-the-same-time\">87. How to select rows with a boolean mask and specific columns at the same time?<\/h2>\n<p>Select rows from array <code>a<\/code> where <code>mask<\/code> is <code>True<\/code>, and take only the first 3 columns, using combined boolean and slice indexing: <code>a[mask, :3]<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Select rows by boolean mask, then first 3 columns\nimport numpy as np\n\na = np.arange(20).reshape(4, 5)\nmask = np.array([True, False, True, False])\nprint('Array:')\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 0  1  2]\n [10 11 12]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Combining boolean row filters with column slicing is a common pattern when filtering data by a condition and extracting specific features in one step.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.arange(20).reshape(4, 5)\nmask = np.array([True, False, True, False])\n\n# Boolean indexing for rows, integer slicing for columns\nresult = a[mask, :3]\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"88-how-to-compute-the-outer-product-of-two-vectors\">88. How to compute the outer product of two vectors?<\/h2>\n<p>Compute the outer product of arrays <code>a = [1, 2, 3]<\/code> and <code>b = [4, 5, 6]<\/code> using <code>np.multiply.outer<\/code> and print the resulting 3&#215;3 matrix.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute outer product using np.multiply.outer\nimport numpy as np\n\na = np.array([1, 2, 3])\nb = np.array([4, 5, 6])\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[ 4  5  6]\n [ 8 10 12]\n [12 15 18]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Outer products appear in attention mechanisms, covariance matrix computation, and rank-1 matrix updates.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.array([1, 2, 3])\nb = np.array([4, 5, 6])\n\n# np.multiply.outer computes the outer product\nresult = np.multiply.outer(a, b)\nprint(result)\n\n# Also equivalent to:\n# np.outer(a, b)\n# np.einsum('i,j->ij', a, b)<\/pre>\n<\/details>\n<h2 id=\"89-how-to-compute-a-rollingmoving-average-of-a-1d-array\">89. How to compute a rolling\/moving average of a 1D array?<\/h2>\n<p>Compute a 3-element moving average of array <code>a<\/code> using <code>np.convolve<\/code> with a uniform kernel of size 3 and <code>mode='valid'<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute a 3-element moving average of a 1D array\nimport numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 20, size=10)\nprint('Array:', a)\n\n# Write your code below\n# Compute a moving average with window size 3\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Moving average (window=3): [ 7.67  5.33  7.33  8.67  8.33 10.67 13.   12.  ]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Moving averages smooth noisy data and are widely used in time series analysis, stock price charts, and signal processing.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 20, size=10)\nprint('Array:', a)\n\n# Moving average using np.convolve with a uniform kernel\nwindow = 3\nkernel = np.ones(window) \/ window\nmoving_avg = np.convolve(a, kernel, mode='valid')\nprint('Moving average (window=3):', np.round(moving_avg, 2))<\/pre>\n<\/details>\n<h2 id=\"90-how-to-rotate-a-2d-array-like-an-image\">90. How to rotate a 2D array (like an image)?<\/h2>\n<p>Rotate the 3&#215;4 array <code>img<\/code> by 90 degrees counter-clockwise using <code>np.rot90<\/code> and print the result.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Rotate a 3x4 array 90 degrees counter-clockwise\nimport numpy as np\n\nimg = np.arange(12).reshape(3, 4)\nprint('Original:')\nprint(img)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Rotated 90 degrees:\n[[ 3  7 11]\n [ 2  6 10]\n [ 1  5  9]\n [ 0  4  8]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Array rotation is a common operation in image processing and data augmentation for training CNNs.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nimg = np.arange(12).reshape(3, 4)\nprint('Original:')\nprint(img)\n\nrotated = np.rot90(img)\nprint('Rotated 90 degrees:')\nprint(rotated)<\/pre>\n<\/details>\n<h2 id=\"91-how-to-flip-an-array-horizontally-and-vertically\">91. How to flip an array horizontally and vertically?<\/h2>\n<p>Flip the 3&#215;4 array <code>img<\/code> left-to-right using <code>np.fliplr<\/code> and top-to-bottom using <code>np.flipud<\/code>, printing both results.<\/p>\n<p><strong>Difficulty Level: L1<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Flip a 3x4 array horizontally and vertically\nimport numpy as np\n\nimg = np.arange(12).reshape(3, 4)\nprint('Original:')\nprint(img)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Flipped left-right:\n[[ 3  2  1  0]\n [ 7  6  5  4]\n [11 10  9  8]]\nFlipped up-down:\n[[ 8  9 10 11]\n [ 4  5  6  7]\n [ 0  1  2  3]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Flipping is one of the simplest and most effective data augmentation techniques for increasing training data variety in deep learning.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nimg = np.arange(12).reshape(3, 4)\nprint('Original:')\nprint(img)\n\nprint('Flipped left-right:')\nprint(np.fliplr(img))\n\nprint('Flipped up-down:')\nprint(np.flipud(img))<\/pre>\n<\/details>\n<h2 id=\"92-how-to-pad-an-array-with-zeros-on-all-sides\">92. How to pad an array with zeros on all sides?<\/h2>\n<p>Pad the 3&#215;3 array of ones <code>a<\/code> with one layer of zeros on all sides using <code>np.pad<\/code> with <code>mode='constant'<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Pad a 3x3 array of ones with zeros on all sides\nimport numpy as np\n\na = np.ones((3, 3))\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[0. 0. 0. 0. 0.]\n [0. 1. 1. 1. 0.]\n [0. 1. 1. 1. 0.]\n [0. 1. 1. 1. 0.]\n [0. 0. 0. 0. 0.]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Zero-padding is essential in convolution operations (CNNs use it to control output size) and when aligning arrays of different sizes.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\na = np.ones((3, 3))\n\npadded = np.pad(a, pad_width=1, mode='constant', constant_values=0)\nprint(padded)<\/pre>\n<\/details>\n<h2 id=\"93-how-to-create-sliding-windows-over-an-array\">93. How to create sliding windows over an array?<\/h2>\n<p>Create sliding windows of size 4 over the array <code>a = np.arange(10)<\/code> using <code>sliding_window_view<\/code> and print the resulting 2D array of windows.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create sliding windows of size 4\nimport numpy as np\nfrom numpy.lib.stride_tricks import sliding_window_view\n\na = np.arange(10)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">[[0 1 2 3]\n [1 2 3 4]\n [2 3 4 5]\n [3 4 5 6]\n [4 5 6 7]\n [5 6 7 8]\n [6 7 8 9]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Sliding windows are used for computing rolling statistics, feature engineering on time series, and understanding how 1D convolution works under the hood.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nfrom numpy.lib.stride_tricks import sliding_window_view\n\na = np.arange(10)\n\nwindows = sliding_window_view(a, window_shape=4)\nprint(windows)<\/pre>\n<\/details>\n<h2 id=\"94-how-to-compute-min-max-normalization-to-scale-values-between-0-and-1\">94. How to compute min-max normalization to scale values between 0 and 1?<\/h2>\n<p>Min-max normalize each column of the 2D array <code>a<\/code> to the range [0, 1] by subtracting the column min and dividing by the column range, then verify the min and max of each column.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Min-max normalize each column of a 2D array to [0, 1]\nimport numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 100, size=(5, 3))\nprint('Original:')\nprint(a)\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Min after normalization: [0. 0. 0.]\nMax after normalization: [1. 1. 1.]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Min-max normalization is commonly used in neural networks and distance-based algorithms like KNN that are sensitive to feature magnitudes.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\na = np.random.randint(1, 100, size=(5, 3))\nprint('Original:')\nprint(a)\n\ncol_min = a.min(axis=0)\ncol_max = a.max(axis=0)\nnormalized = (a - col_min) \/ (col_max - col_min)\n\nprint('Normalized:')\nprint(np.round(normalized, 3))\nprint('Min after normalization:', normalized.min(axis=0))\nprint('Max after normalization:', normalized.max(axis=0))<\/pre>\n<\/details>\n<h2 id=\"95-how-to-compute-a-weighted-average\">95. How to compute a weighted average?<\/h2>\n<p>Compute the weighted average of <code>scores<\/code> using <code>weights<\/code> with <code>np.average<\/code>, and compare it against the simple mean from <code>np.mean<\/code>.<\/p>\n<p><strong>Difficulty Level: L2<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute the weighted average of exam scores\nimport numpy as np\n\nscores = np.array([85, 90, 78, 92, 88])\nweights = np.array([0.1, 0.2, 0.3, 0.25, 0.15])  # must sum to 1\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Weighted average: 85.9\nSimple average: 86.6<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Weighted averages assign different importance to values and are used in portfolio returns, ensemble model predictions, and survey analysis.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nscores = np.array([85, 90, 78, 92, 88])\nweights = np.array([0.1, 0.2, 0.3, 0.25, 0.15])\n\nweighted_avg = np.average(scores, weights=weights)\nsimple_avg = np.mean(scores)\n\nprint('Weighted average:', weighted_avg)\nprint('Simple average:', simple_avg)<\/pre>\n<\/details>\n<h2 id=\"96-how-to-find-the-top-k-similar-items-using-cosine-similarity\">96. How to find the top-k similar items using cosine similarity?<\/h2>\n<p>Compute the cosine similarity between the <code>query<\/code> vector and each row in <code>items<\/code> using dot products and norms, then find the index of the most similar item with <code>np.argmax<\/code>.<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute cosine similarity between a query vector and 5 item vectors\nimport numpy as np\n\nnp.random.seed(42)\nitems = np.random.rand(5, 4)  # 5 items, 4 features each\nquery = np.random.rand(4)      # query vector\n\nprint('Items shape:', items.shape)\nprint('Query:', np.round(query, 3))\n\n# Write your code below\n# Find the most similar item to the query\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Cosine similarities: [0.896 0.928 0.811 0.852 0.866]\nMost similar item index: 1<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Cosine similarity is the standard metric for comparing embeddings in NLP, recommendation systems, and information retrieval.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\nitems = np.random.rand(5, 4)\nquery = np.random.rand(4)\n\n# Cosine similarity = dot(a,b) \/ (||a|| * ||b||)\ndot_products = items @ query\nitem_norms = np.linalg.norm(items, axis=1)\nquery_norm = np.linalg.norm(query)\n\nsimilarities = dot_products \/ (item_norms * query_norm)\n\nprint('Cosine similarities:', np.round(similarities, 3))\nprint('Most similar item index:', np.argmax(similarities))<\/pre>\n<\/details>\n<h2 id=\"97-how-to-compute-the-covariance-matrix-of-a-dataset\">97. How to compute the covariance matrix of a dataset?<\/h2>\n<p>Compute the covariance matrix of the 3-feature <code>data<\/code> array using <code>np.cov(data.T)<\/code>, then print the covariance between feature 0 and feature 1 (positive) and between feature 0 and feature 2 (negative).<\/p>\n<p><strong>Difficulty Level: L3<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute the covariance matrix of a dataset with 3 features\nimport numpy as np\n\nnp.random.seed(42)\n# Simulate 100 samples with 3 correlated features\ndata = np.random.randn(100, 3)\ndata[:, 1] = data[:, 0] * 2 + np.random.randn(100) * 0.5  # feature 1 correlated with feature 0\ndata[:, 2] = -data[:, 0] + np.random.randn(100) * 0.3      # feature 2 negatively correlated\n\n# Write your code below\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Covariance matrix shape: (3, 3)\nFeature 0-1 covariance (should be positive): 1.88\nFeature 0-2 covariance (should be negative): -0.96<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> The covariance matrix reveals how features co-vary and is the starting point for PCA, Mahalanobis distance, and multivariate statistics.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\ndata = np.random.randn(100, 3)\ndata[:, 1] = data[:, 0] * 2 + np.random.randn(100) * 0.5\ndata[:, 2] = -data[:, 0] + np.random.randn(100) * 0.3\n\n# np.cov expects each row to be a variable, so transpose\ncov_matrix = np.cov(data.T)\n\nprint('Covariance matrix shape:', cov_matrix.shape)\nprint('Feature 0-1 covariance (should be positive):', round(cov_matrix[0, 1], 2))\nprint('Feature 0-2 covariance (should be negative):', round(cov_matrix[0, 2], 2))<\/pre>\n<\/details>\n<h2 id=\"98-how-to-perform-one-step-pca-using-eigendecomposition\">98. How to perform one-step PCA using eigendecomposition?<\/h2>\n<p>Reduce the 4-feature <code>data<\/code> array to 2 principal components by centering the data, computing the covariance matrix, performing eigendecomposition with <code>np.linalg.eig<\/code>, selecting the top-2 eigenvectors, and projecting the data.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Reduce a 4-feature dataset to 2 principal components using PCA\nimport numpy as np\n\nnp.random.seed(42)\n# 50 samples, 4 features\ndata = np.random.randn(50, 4)\ndata[:, 1] = data[:, 0] * 1.5 + np.random.randn(50) * 0.3\ndata[:, 3] = data[:, 2] * -0.8 + np.random.randn(50) * 0.2\n\n# Write your code below\n# 1. Center the data\n# 2. Compute covariance matrix\n# 3. Get eigenvalues\/eigenvectors\n# 4. Project data onto top-2 components\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Original shape: (50, 4)\nProjected shape: (50, 2)\nVariance explained by top 2 components: 89.2%<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> PCA via eigendecomposition is the foundational dimensionality reduction technique in ML, and implementing it from scratch builds deep understanding of how it works.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\ndata = np.random.randn(50, 4)\ndata[:, 1] = data[:, 0] * 1.5 + np.random.randn(50) * 0.3\ndata[:, 3] = data[:, 2] * -0.8 + np.random.randn(50) * 0.2\n\n# 1. Center the data (subtract mean of each feature)\ncentered = data - data.mean(axis=0)\n\n# 2. Compute covariance matrix\ncov_matrix = np.cov(centered.T)\n\n# 3. Eigendecomposition\neigenvalues, eigenvectors = np.linalg.eig(cov_matrix)\n\n# 4. Sort by eigenvalue (largest first) and pick top 2\nsorted_idx = np.argsort(eigenvalues)[::-1]\ntop2_vectors = eigenvectors[:, sorted_idx[:2]]\n\n# 5. Project data onto top-2 components\nprojected = centered @ top2_vectors\n\nprint('Original shape:', data.shape)\nprint('Projected shape:', projected.shape)\nvariance_explained = eigenvalues[sorted_idx[:2]].sum() \/ eigenvalues.sum() * 100\nprint(f'Variance explained by top 2 components: {variance_explained:.1f}%')<\/pre>\n<\/details>\n<h2 id=\"99-how-to-compute-2d-sliding-windows-over-a-matrix\">99. How to compute 2D sliding windows over a matrix?<\/h2>\n<p>Create 3&#215;3 sliding windows over the 5&#215;5 array <code>a<\/code> using <code>sliding_window_view(a, (3, 3))<\/code>, then print the shape of the result and the first window (top-left corner).<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Create 3x3 sliding windows over a 5x5 array\nimport numpy as np\nfrom numpy.lib.stride_tricks import sliding_window_view\n\na = np.arange(25).reshape(5, 5)\nprint('Array:')\nprint(a)\n\n# Write your code below\n# Print the shape of the result and the first window (top-left corner)\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Windows shape: (3, 3, 3, 3)\nFirst window (top-left):\n[[ 0  1  2]\n [ 5  6  7]\n [10 11 12]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> 2D sliding windows are exactly what a convolution layer does in a CNN &#8212; understanding this builds intuition for how neural network filters work.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\nfrom numpy.lib.stride_tricks import sliding_window_view\n\na = np.arange(25).reshape(5, 5)\nprint('Array:')\nprint(a)\n\n# 2D sliding window: window_shape=(rows, cols)\nwindows = sliding_window_view(a, (3, 3))\nprint('Windows shape:', windows.shape)\nprint('First window (top-left):')\nprint(windows[0, 0])<\/pre>\n<\/details>\n<h2 id=\"100-how-to-do-batch-matrix-multiplication-with-npeinsum\">100. How to do batch matrix multiplication with np.einsum?<\/h2>\n<p>Multiply each pair of matrices in batches <code>A<\/code> (shape 3x2x3) and <code>B<\/code> (shape 3x3x2) using <code>np.einsum('bij,bjk-&gt;bik', A, B)<\/code> so that <code>result[i] = A[i] @ B[i]<\/code>.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Batch matrix multiplication using np.einsum\nimport numpy as np\n\nnp.random.seed(42)\nA = np.random.randint(1, 5, size=(3, 2, 3))  # 3 matrices of shape 2x3\nB = np.random.randint(1, 5, size=(3, 3, 2))  # 3 matrices of shape 3x2\n\nprint('A shape:', A.shape)\nprint('B shape:', B.shape)\n\n# Write your code below\n# Multiply each pair: result[i] = A[i] @ B[i]\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Result shape: (3, 2, 2)\nResult:\n[[[22 17]\n  [26 28]]\n\n [[18 16]\n  [26 22]]\n\n [[23 13]\n  [25 13]]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Batch matrix multiplication is essential in deep learning for computing operations like attention scores across all samples in a batch simultaneously.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\nA = np.random.randint(1, 5, size=(3, 2, 3))\nB = np.random.randint(1, 5, size=(3, 3, 2))\n\n# 'bij,bjk->bik' means: for each batch b, multiply matrix (i,j) by (j,k)\nresult = np.einsum('bij,bjk->bik', A, B)\nprint('Result shape:', result.shape)\nprint('Result:')\nprint(result)<\/pre>\n<\/details>\n<h2 id=\"101-how-to-compute-a-pairwise-euclidean-distance-matrix-without-loops\">101. How to compute a pairwise Euclidean distance matrix without loops?<\/h2>\n<p>Compute the 5&#215;5 pairwise Euclidean distance matrix for the <code>points<\/code> array (5 points in 3D) using broadcasting and <code>np.einsum<\/code>, without any Python loops.<\/p>\n<p><strong>Difficulty Level: L4<\/strong><\/p>\n<p><strong>Solve:<\/strong><\/p>\n<pre class=\"python-runnable\"># Task: Compute pairwise Euclidean distance matrix (no loops)\nimport numpy as np\n\nnp.random.seed(42)\npoints = np.random.rand(5, 3)  # 5 points in 3D\nprint('Points:')\nprint(np.round(points, 4))\n\n# Write your code below\n# Compute the 5x5 distance matrix using broadcasting\n\n<\/pre>\n<p><strong>Desired Output:<\/strong><\/p>\n<pre style=\"background:#f5f5f5;padding:12px;border-radius:6px;font-size:13px;overflow-x:auto;border:1px solid #e0e0e0;color:#333\">Distance matrix:\n[[0.     1.0068 0.3527 1.0164 1.0284]\n [1.0068 0.     0.9973 0.8323 0.2419]\n [0.3527 0.9973 0.     1.1285 1.0968]\n [1.0164 0.8323 1.1285 0.     0.8206]\n [1.0284 0.2419 1.0968 0.8206 0.    ]]<\/pre>\n<div class=\"why-matters\"><strong>Why this matters:<\/strong> Pairwise distance matrices are the foundation of KNN, hierarchical clustering, and DBSCAN, and computing them vectorized is critical for performance.<\/div>\n<details class=\"blogv4-expand\">\n<summary class=\"blogv4-expand__toggle\"><strong>Show Solution<\/strong><\/summary>\n<pre class=\"python-runnable\">import numpy as np\n\nnp.random.seed(42)\npoints = np.random.rand(5, 3)\n\n# Broadcasting: diff[i,j,k] = points[i,k] - points[j,k]\ndiff = points[:, np.newaxis, :] - points[np.newaxis, :, :]\n\n# Sum squared differences over the last axis, then square root\ndist_matrix = np.sqrt(np.einsum('ijk,ijk->ij', diff, diff))\n\nprint('Distance matrix:')\nprint(np.round(dist_matrix, 4))<\/pre>\n<\/details>\n","protected":false},"excerpt":{"rendered":"<p>101 interactive NumPy exercises with solutions. Edit and run every code block directly in your browser \u2014 no installation needed.<\/p>\n","protected":false},"author":1,"featured_media":847,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[21],"tags":[],"class_list":["post-811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>101 NumPy Exercises for Data Analysis (Python) - ML+<\/title>\n<meta name=\"description\" content=\"The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"101 Numpy Exercises for Data Analysis\" \/>\n<meta property=\"og:description\" content=\"The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/\" \/>\n<meta property=\"og:site_name\" content=\"machinelearningplus\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/rtipaday\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-26T19:52:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T07:38:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2018\/02\/101_numpy_exercises_feature_img.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"560\" \/>\n\t<meta property=\"og:image:height\" content=\"315\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Selva Prabhakaran\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/R_Programming\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Selva Prabhakaran\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"34 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/\"},\"author\":{\"name\":\"Selva Prabhakaran\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\"},\"headline\":\"101 NumPy Exercises for Data Analysis (Python)\",\"datePublished\":\"2018-02-26T19:52:03+00:00\",\"dateModified\":\"2026-03-15T07:38:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/\"},\"wordCount\":5280,\"commentCount\":35,\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/101_numpy_exercises_feature_img.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/\",\"name\":\"101 NumPy Exercises for Data Analysis (Python) - ML+\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/101_numpy_exercises_feature_img.jpg\",\"datePublished\":\"2018-02-26T19:52:03+00:00\",\"dateModified\":\"2026-03-15T07:38:17+00:00\",\"description\":\"The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/python\\\/101-numpy-exercises-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/101_numpy_exercises_feature_img.jpg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2018\\\/02\\\/101_numpy_exercises_feature_img.jpg\",\"width\":560,\"height\":315,\"caption\":\"101 numpy exercises feature\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"name\":\"machinelearningplus\",\"description\":\"Learn Data Science (AI \\\/ ML) Online\",\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/machinelearningplus.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\",\"name\":\"machinelearningplus\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"width\":348,\"height\":36,\"caption\":\"machinelearningplus\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\",\"name\":\"Selva Prabhakaran\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"caption\":\"Selva Prabhakaran\"},\"description\":\"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \\\/ ML \\\/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.\",\"sameAs\":[\"https:\\\/\\\/localhost:8080\\\/\",\"https:\\\/\\\/www.facebook.com\\\/rtipaday\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/R_Programming\"],\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/author\\\/selva86\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"101 NumPy Exercises for Data Analysis (Python) - ML+","description":"The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/","og_locale":"en_US","og_type":"article","og_title":"101 Numpy Exercises for Data Analysis","og_description":"The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest.","og_url":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/","og_site_name":"machinelearningplus","article_author":"https:\/\/www.facebook.com\/rtipaday\/","article_published_time":"2018-02-26T19:52:03+00:00","article_modified_time":"2026-03-15T07:38:17+00:00","og_image":[{"width":560,"height":315,"url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2018\/02\/101_numpy_exercises_feature_img.jpg","type":"image\/jpeg"}],"author":"Selva Prabhakaran","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/R_Programming","twitter_misc":{"Written by":"Selva Prabhakaran","Est. reading time":"34 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/#article","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/"},"author":{"name":"Selva Prabhakaran","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e"},"headline":"101 NumPy Exercises for Data Analysis (Python)","datePublished":"2018-02-26T19:52:03+00:00","dateModified":"2026-03-15T07:38:17+00:00","mainEntityOfPage":{"@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/"},"wordCount":5280,"commentCount":35,"publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"image":{"@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2018\/02\/101_numpy_exercises_feature_img.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/","url":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/","name":"101 NumPy Exercises for Data Analysis (Python) - ML+","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/#primaryimage"},"image":{"@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2018\/02\/101_numpy_exercises_feature_img.jpg","datePublished":"2018-02-26T19:52:03+00:00","dateModified":"2026-03-15T07:38:17+00:00","description":"The goal of the numpy exercises is to serve as a reference as well as to get you to apply numpy beyond the basics. The questions are of 4 levels of difficulties with L1 being the easiest to L4 being the hardest.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/python\/101-numpy-exercises-python\/#primaryimage","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2018\/02\/101_numpy_exercises_feature_img.jpg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2018\/02\/101_numpy_exercises_feature_img.jpg","width":560,"height":315,"caption":"101 numpy exercises feature"},{"@type":"WebSite","@id":"https:\/\/machinelearningplus.com\/#website","url":"https:\/\/machinelearningplus.com\/","name":"machinelearningplus","description":"Learn Data Science (AI \/ ML) Online","publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/machinelearningplus.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/machinelearningplus.com\/#organization","name":"machinelearningplus","url":"https:\/\/machinelearningplus.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","width":348,"height":36,"caption":"machinelearningplus"},"image":{"@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e","name":"Selva Prabhakaran","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","url":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","caption":"Selva Prabhakaran"},"description":"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \/ ML \/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.","sameAs":["https:\/\/localhost:8080\/","https:\/\/www.facebook.com\/rtipaday\/","https:\/\/x.com\/https:\/\/twitter.com\/R_Programming"],"url":"https:\/\/machinelearningplus.com\/author\/selva86\/"}]}},"_links":{"self":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/comments?post=811"}],"version-history":[{"count":1,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/811\/revisions"}],"predecessor-version":[{"id":35617,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/811\/revisions\/35617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media\/847"}],"wp:attachment":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media?parent=811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/categories?post=811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/tags?post=811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}