{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 结构化数组" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "假设我们要保存这样的数据:\n", "\n", "|name|age|wgt\n", "--|--|--|--\n", "0|dan|1|23.1\n", "1|ann|0|25.1\n", "2|sam|2|8.3\n", "\n", "希望定义一个一维数组,每个元素有三个属性 `name, age, wgt`,此时我们需要使用结构化数组。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "定义数组 `a`:\n", "\n", "0|1|2|3\n", "-|-|-|-\n", "1.0|2.0|3.0|4.0" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = np.array([1.0,2.0,3.0,4.0], np.float32)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用 `view` 方法,将 `a` 对应的内存按照复数来解释:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.+2.j, 3.+4.j], dtype=complex64)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.view(np.complex64)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "0|1|2|3\n", "-|-|-|-\n", "1.0|2.0|3.0|4.0\n", "real|imag|real|imag\n", "\n", "事实上,我们可以把复数看成一个结构体,第一部分是实部,第二部分是虚部,这样这个数组便可以看成是一个结构化数组。\n", "\n", "换句话说,我们只需要换种方式解释这段内存,便可以得到结构化数组的效果!\n", "\n", "0|1|2|3\n", "-|-|-|-\n", "1.0|2.0|3.0|4.0\n", "mass|vol|mass|vol\n", "\n", "例如,我们可以将第一个浮点数解释为质量,第二个浮点数解释为速度,则这段内存还可以看成是包含两个域(质量和速度)的结构体。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_dtype = np.dtype([('mass', 'float32'), ('vol', 'float32')])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([(1.0, 2.0), (3.0, 4.0)], \n", " dtype=[('mass', ' \n", " position\n", " mass\n", " xy\n", "\n", "\n", "那么它的类型可以这样嵌套定义:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "particle_dtype = np.dtype([('position', [('x', 'float'), \n", " ('y', 'float')]),\n", " ('mass', 'float')\n", " ])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "假设数据文件如下:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting data.txt\n" ] } ], "source": [ "%%writefile data.txt\n", "2.0 3.0 42.0\n", "2.1 4.3 32.5\n", "1.2 4.6 32.3\n", "4.5 -6.4 23.3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "读取数据:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data = np.loadtxt('data.txt', dtype=particle_dtype)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([((2.0, 3.0), 42.0), ((2.1, 4.3), 32.5), ((1.2, 4.6), 32.3),\n", " ((4.5, -6.4), 23.3)], \n", " dtype=[('position', [('x', '