{ "cells": [ { "cell_type": "markdown", "id": "534b3332", "metadata": {}, "source": [ "## `dtype`" ] }, { "cell_type": "markdown", "id": "7c7f69f2", "metadata": {}, "source": [ "`ndarray` is a container for homogeneous data, i.e. all elements must be of the same type. Each array has a `dtype`, an object that describes the data type of the array:" ] }, { "cell_type": "code", "execution_count": 1, "id": "04e4b903", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:01:57.826369Z", "iopub.status.busy": "2026-05-17T15:01:57.826144Z", "iopub.status.idle": "2026-05-17T15:01:57.866663Z", "shell.execute_reply": "2026-05-17T15:01:57.866405Z", "shell.execute_reply.started": "2026-05-17T15:01:57.826345Z" } }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "rng = np.random.default_rng()\n", "data = rng.random((7, 3))\n", "dt = data.dtype\n", "dt" ] }, { "cell_type": "markdown", "id": "2a09a4c5", "metadata": {}, "source": [ "NumPy data types:\n", "\n", "Type | Type code | Description\n", ":---------------- | :------------ | :----------\n", "`int8`, `uint8` | `i1`, `u1` | Signed and unsigned 8-bit (1-byte) integer types\n", "`int16`, `uint16` | `i2`, `u2` | Signed and unsigned 16-Bit (2 Byte) integer types\n", "`int32`, `uint32` | `i4`, `u4` | Signed and unsigned 32-Bit (4 Byte) integer types\n", "`int64`, `uint64` | `i8`, `u8` | Signed and unsigned 64-Bit (8 Byte) integer types\n", "`float16` | `f2` | Standard floating point with half precision\n", "`float32` | `f4` or `f` | Standard floating point with single precision; compatible with C `float`\n", "`float64` | `f8` or `d` | Standard floating point with double precision; compatible with C `double` and Python `float` object\n", "`complex64`, `complex128`, `complex256` | `c8`, `c16`, `c32` | Complex numbers represented by two 32, 64 or 128 floating point numbers respectively\n", "`bool` | `?` | Boolean type that stores the values `True` and `False`\n", "`object` | `O` | Python object type; a value can be any Python object\n", "`string_` | `S` | ASCII string type with fixed length (1 byte per character); to create a string type with length 7, for example, use `S7`; longer inputs are truncated without warning\n", "`unicode_` | `U` | Unicode type with fixed length where the number of bytes is platform-specific; uses the same specification semantics as `string_`, e.g. `U7`" ] }, { "cell_type": "markdown", "id": "f4a0b6b2", "metadata": {}, "source": [ "Determine the number of elements with `itemsize`:" ] }, { "cell_type": "code", "execution_count": 2, "id": "ba581655", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:01:57.867084Z", "iopub.status.busy": "2026-05-17T15:01:57.866960Z", "iopub.status.idle": "2026-05-17T15:01:57.869673Z", "shell.execute_reply": "2026-05-17T15:01:57.869423Z", "shell.execute_reply.started": "2026-05-17T15:01:57.867058Z" } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.itemsize" ] }, { "cell_type": "markdown", "id": "4bc3981f", "metadata": {}, "source": [ "Determine the name of the data type:" ] }, { "cell_type": "code", "execution_count": 3, "id": "465e5fbd", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:01:57.870067Z", "iopub.status.busy": "2026-05-17T15:01:57.870003Z", "iopub.status.idle": "2026-05-17T15:01:57.872216Z", "shell.execute_reply": "2026-05-17T15:01:57.871962Z", "shell.execute_reply.started": "2026-05-17T15:01:57.870060Z" } }, "outputs": [ { "data": { "text/plain": [ "'float64'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.name" ] }, { "cell_type": "markdown", "id": "e1bbf89d", "metadata": {}, "source": [ "Check data type:" ] }, { "cell_type": "code", "execution_count": 4, "id": "b8771580", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:01:57.872615Z", "iopub.status.busy": "2026-05-17T15:01:57.872529Z", "iopub.status.idle": "2026-05-17T15:01:57.874755Z", "shell.execute_reply": "2026-05-17T15:01:57.874530Z", "shell.execute_reply.started": "2026-05-17T15:01:57.872608Z" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.type is np.float64" ] }, { "cell_type": "markdown", "id": "8d304951", "metadata": {}, "source": [ "Change data type with `astype`:" ] }, { "cell_type": "code", "execution_count": 5, "id": "372a4baf", "metadata": { "execution": { "iopub.execute_input": "2026-05-17T15:01:57.875104Z", "iopub.status.busy": "2026-05-17T15:01:57.875008Z", "iopub.status.idle": "2026-05-17T15:01:57.877163Z", "shell.execute_reply": "2026-05-17T15:01:57.876975Z", "shell.execute_reply.started": "2026-05-17T15:01:57.875093Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.09647385, 0.18739372, 0.19808419],\n", " [0.90809816, 0.6350481 , 0.9251651 ],\n", " [0.11977272, 0.6560006 , 0.8847805 ],\n", " [0.33568636, 0.6303831 , 0.3406715 ],\n", " [0.66093606, 0.06444538, 0.9164912 ],\n", " [0.20784943, 0.8299043 , 0.5469985 ],\n", " [0.24248955, 0.39786914, 0.8835176 ]], dtype=float32)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_float32 = data.astype(np.float32)\n", "data_float32" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }