{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##内容索引\n", "该小结主要介绍了NumPy数组的基本操作。\n", "\n", "子目1中,介绍创建和索引数组,数据类型,dtype类,自定义异构数据类型。\n", "\n", "子目2中,介绍数组的索引和切片,主要是对[]运算符的操作。\n", "\n", "子目3中,介绍如何改变数组的维度,分别介绍了**ravel函数、flatten函数、transpose函数、resize函数、reshape函数的用法。**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ndarray是一个多维数组对象,该对象由**实际的数据、描述这些数据的元数据**组成,大部分数组操作仅仅修改元数据部分,而不改变底层的实际数据。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "用arange函数创建数组" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = arange(5)\n", "a.dtype" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(5,)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "数组的shape属性返回一个元祖(tuple),元组中的元素即NumPy数组每一个维度的大小。" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "##1. 创建多维数组\n", "array函数可以依据给定的对象生成数组。\n", "给定的对象应是类数组,如python的列表、numpy的arange函数" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "m = array([arange(2), arange(2)])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1]\n", " [0 1]]\n", "(2, 2)\n", "\n", "\n" ] } ], "source": [ "print m\n", "print m.shape\n", "print type(m)\n", "print type(m.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###选取元素" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "a = array([[1,2],[3,4]])\n", "print a[0,0]\n", "print a[0,1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###NumPy数据类型\n", "Numpy除了Python支持的整型、浮点型、复数型之外,还添加了很多其他的数据类型。\n", "\n", "Type\tRemarks\t Character code\n", "bool_\tcompatible: Python bool\t'?'\n", "bool8\t8 bits\t \n", "Integers:\n", "\n", "byte\tcompatible: C char\t'b'\n", "short\tcompatible: C short\t'h'\n", "intc\tcompatible: C int\t'i'\n", "int_\tcompatible: Python int\t'l'\n", "longlong\tcompatible: C long long\t'q'\n", "intp\tlarge enough to fit a pointer\t'p'\n", "int8\t8 bits\t \n", "int16\t16 bits\t \n", "int32\t32 bits\t \n", "int64\t64 bits\t \n", "Unsigned integers:\n", "\n", "ubyte\tcompatible: C unsigned char\t'B'\n", "ushort\tcompatible: C unsigned short\t'H'\n", "uintc\tcompatible: C unsigned int\t'I'\n", "uint\tcompatible: Python int\t'L'\n", "ulonglong\tcompatible: C long long\t'Q'\n", "uintp\tlarge enough to fit a pointer\t'P'\n", "uint8\t8 bits\t \n", "uint16\t16 bits\t \n", "uint32\t32 bits\t \n", "uint64\t64 bits\t \n", "Floating-point numbers:\n", "\n", "half\t \t'e'\n", "single\tcompatible: C float\t'f'\n", "double\tcompatible: C double\t \n", "float_\tcompatible: Python float\t'd'\n", "longfloat\tcompatible: C long float\t'g'\n", "float16\t16 bits\t \n", "float32\t32 bits\t \n", "float64\t64 bits\t \n", "float96\t96 bits, platform?\t \n", "float128\t128 bits, platform?\t \n", "Complex floating-point numbers:\n", "\n", "csingle\t \t'F'\n", "complex_\tcompatible: Python complex\t'D'\n", "clongfloat\t \t'G'\n", "complex64\ttwo 32-bit floats\t \n", "complex128\ttwo 64-bit floats\t \n", "complex192\ttwo 96-bit floats, platform?\t \n", "complex256\ttwo 128-bit floats, platform?\t \n", "Any Python object:\n", "\n", "object_\tany Python object\t'O'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**每一种数据类型均有对应的类型转换函数**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42.0\n", "42\n", "True\n", "1.0\n" ] } ], "source": [ "print float64(42)\n", "print int8(42.0)\n", "print bool(42)\n", "print float(True)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7], dtype=uint16)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arange(8, dtype=uint16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**复数不能转换成整数和浮点数**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy数组中每一个元素均为相同的数据类型,现在给出单个元素所占字节" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype.itemsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**dtype类的属性**" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d\n", "\n", "表示;与之相反,小端序是将最低位字节存储在最低的内存地址处,用<表示。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**创建自定义数据类型**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "自定义数据类型是一种异构数据类型,可以当做用来记录电子表格或数据库中一行数据的结构。\n", "\n", "下面我们创建一种自定义的异构数据类型,该数据类型包括一个用字符串记录的名字、一个用整数记录的数字以及一个用浮点数记录的价格。" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t = dtype([('name', str_, 40), ('numitems', int32), ('price', float32)])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype([('name', 'S40'), ('numitems', '