{ "cells": [ { "cell_type": "markdown", "id": "b54830fc", "metadata": {}, "source": [ "# NumPy Tutorial 02: Array Creation, Shape, and Memory Model" ] }, { "cell_type": "markdown", "id": "e76399d1", "metadata": {}, "source": [ "## Download Notebook\n", "\n", "{download}`Download this notebook <02_array_creation_and_attributes.ipynb>`" ] }, { "cell_type": "code", "execution_count": null, "id": "fe7b4a0c", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "1b912b0a", "metadata": {}, "source": [ "## 1. Core constructors\n", "\n", "Use specialized constructors when possible for clarity and performance." ] }, { "cell_type": "code", "execution_count": null, "id": "d7500161", "metadata": {}, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)\n", "z = np.zeros((2, 4), dtype=np.float64)\n", "o = np.ones((2, 4), dtype=np.float64)\n", "r = np.arange(0, 10, 2)\n", "l = np.linspace(0, 1, 6)\n", "\n", "print('a:\\n', a)\n", "print('zeros shape:', z.shape)\n", "print('ones shape:', o.shape)\n", "print('arange:', r)\n", "print('linspace:', l)" ] }, { "cell_type": "markdown", "id": "56d64418", "metadata": {}, "source": [ "## 2. Inspecting structure\n", "\n", "`shape`, `ndim`, `size`, `dtype`, `itemsize`, and `nbytes` are foundational attributes." ] }, { "cell_type": "code", "execution_count": null, "id": "19141687", "metadata": {}, "outputs": [], "source": [ "x = np.arange(24, dtype=np.int16).reshape(2, 3, 4)\n", "print('shape:', x.shape)\n", "print('ndim:', x.ndim)\n", "print('size:', x.size)\n", "print('dtype:', x.dtype)\n", "print('itemsize:', x.itemsize)\n", "print('nbytes:', x.nbytes)" ] }, { "cell_type": "markdown", "id": "9efa64d7", "metadata": {}, "source": [ "## 3. Reshape and flatten family\n", "\n", "`reshape` changes view/interpretation when possible; `ravel` prefers view; `flatten` always returns a copy." ] }, { "cell_type": "code", "execution_count": null, "id": "e74908af", "metadata": {}, "outputs": [], "source": [ "x = np.arange(12)\n", "m = x.reshape(3, 4)\n", "rv = m.ravel()\n", "fl = m.flatten()\n", "\n", "m[0, 0] = 999\n", "print('m:\\n', m)\n", "print('ravel reflects change:', rv[0])\n", "print('flatten copy value:', fl[0])" ] }, { "cell_type": "markdown", "id": "8366f16f", "metadata": {}, "source": [ "## 4. Memory order and transposition\n", "\n", "NumPy supports C-order (row-major) and Fortran-order (column-major). Some operations create views, others create copies." ] }, { "cell_type": "code", "execution_count": null, "id": "4fe069cb", "metadata": {}, "outputs": [], "source": [ "m = np.arange(12).reshape(3, 4)\n", "t = m.T\n", "\n", "print('Original shape:', m.shape, 'strides:', m.strides)\n", "print('Transposed shape:', t.shape, 'strides:', t.strides)\n", "print('Share memory?', np.shares_memory(m, t))" ] }, { "cell_type": "markdown", "id": "79d135db", "metadata": {}, "source": [ "## 5. Type casting strategies\n", "\n", "Use `astype` to cast. Prefer controlled casting (`casting='safe'`) in production pipelines." ] }, { "cell_type": "code", "execution_count": null, "id": "bb875ea4", "metadata": {}, "outputs": [], "source": [ "x = np.array([1.2, 3.7, -2.9])\n", "print('Original:', x, x.dtype)\n", "xi = x.astype(np.int32)\n", "print('Cast to int32:', xi, xi.dtype)\n", "\n", "y = np.array([1, 2, 3], dtype=np.int64)\n", "print('Safe cast int64 -> float64:', y.astype(np.float64, casting='safe'))" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }