{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0f04f20-1482-4134-bb7f-89556f201c6f",
   "metadata": {},
   "source": [
    "### <center>San Jose State University<br>Department of Applied Data Science<br><br>**DATA 200<br>Computational Programming for Data Analytics**<br><br>Spring 2024<br>Instructor: Ron Mak</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1a540de-57a9-4f39-8684-33f3d2b36681",
   "metadata": {},
   "source": [
    "# Iterating and filtering `numpy` arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25e2310c-6bf1-4753-9935-da7f9f3effc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51c828d1-c709-45a8-9fda-564608ecb194",
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix = np.array([i for i in range(1, 16)]).reshape(3,5)\n",
    "matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b353a9f-1e68-4b22-b7e2-6136baa943eb",
   "metadata": {},
   "source": [
    "## Iterate over the rows of the matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4707e890-4237-4321-a6cf-2ca082fac603",
   "metadata": {},
   "outputs": [],
   "source": [
    "for row in matrix:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66a7c1c1-81d1-44b7-8cd8-e60eb2abfb5f",
   "metadata": {},
   "source": [
    "## Iterate over the values of the matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03abe149-4dfc-440b-aab5-2c90cbe59f14",
   "metadata": {},
   "outputs": [],
   "source": [
    "for value in np.nditer(matrix):\n",
    "    print(value, end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd927042-9274-48f4-bc4a-8dc18b6aeab4",
   "metadata": {},
   "outputs": [],
   "source": [
    "for index, value in np.ndenumerate(matrix):\n",
    "    print(f'{index = }, {value = }')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d2c122d-fb21-4e59-ba6d-518477f52444",
   "metadata": {},
   "source": [
    "## Filter a matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c4b16a9-bece-41ba-8ee3-e3c8148b5e50",
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27587340-e06c-4cfc-975c-db657adab966",
   "metadata": {},
   "source": [
    "#### Values >= 11."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5b5eadb-d5dc-4305-8623-a9cc7791be9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix[matrix >= 11]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fa1435e-d152-4f03-a3ba-62acdddfba98",
   "metadata": {},
   "source": [
    "#### Values >= 11 and <= 14."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "778fa8b9-e4b8-4552-a8d8-cb5ec70e5ef2",
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix[(matrix >= 11) & (matrix <= 14)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cf25610-bae5-4232-9305-c869a3c18dbb",
   "metadata": {},
   "source": [
    "#### Values < 5 or >= 11."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89c50d96-b200-4f5e-af42-2c9e2177f7bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.extract((matrix < 5) | (matrix >= 11), matrix)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1ace7de-01ba-47da-9e89-a114e31ac458",
   "metadata": {},
   "source": [
    "#### An array of row indices and an array of column indices of all the values >= 5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fca9dabf-8b13-4495-a816-4ff05ce6ec54",
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00dfcef8-c450-44b1-9fb2-05dfbe962f53",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.where(matrix >= 14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f2bf339-a84a-43f4-a201-21061354be7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (C) Copyright 2023 by Ronald Mak"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0849b637-b1d7-4e70-9a99-b37a598afdbe",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
