{ "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": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "data = np.random.randn(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": {}, "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": {}, "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": {}, "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": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.44477868, 1.7366465 , -2.0396285 ],\n", " [ 0.65273875, -0.11706501, -2.3253074 ],\n", " [-1.3416812 , -1.1469622 , 0.04803479],\n", " [-0.08298384, -0.02865864, 1.0284923 ],\n", " [ 0.59293705, 0.5345401 , -1.717722 ],\n", " [-1.1971567 , -0.4091349 , -0.03829814],\n", " [ 1.030255 , 0.9890015 , -0.4749484 ]], 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 }