{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##内容索引\n", "1. 数组组合\n", "使用**vstack、dstack、hstack、column_stack、row_stack以及concatenate函数**完成数组的组合\n", "\n", "2. 数组分割 \n", "使用**hsplit、vsplit、dsplit和split函数**\n", "\n", "3. 数组属性\n", "\n", "4. 数组转换\n", "**tolist、astype函数**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using matplotlib backend: TkAgg\n", "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##1. 数组的组合" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy数组有水平组合、垂直组合和深度组合等多种组合方式,我们将使用**vstack、dstack、hstack、column_stack、row_stack以及concatenate函数**完成数组的组合" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a: \n", "[[0 1 2]\n", " [3 4 5]\n", " [6 7 8]]\n", "b: \n", "[[ 0 2 4]\n", " [ 6 8 10]\n", " [12 14 16]]\n" ] } ], "source": [ "a = arange(9).reshape(3,3)\n", "b = 2*a\n", "print 'a: \\n',a\n", "print 'b: \\n',b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(1)水平组合\n", "将ndarray对象构成的元组作为参数,传给hstack函数\n", "或者使用concatenate函数实现该功能" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 0, 2, 4],\n", " [ 3, 4, 5, 6, 8, 10],\n", " [ 6, 7, 8, 12, 14, 16]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hstack((a, b))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 0, 2, 4],\n", " [ 3, 4, 5, 6, 8, 10],\n", " [ 6, 7, 8, 12, 14, 16]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "concatenate((a,b), axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(2)垂直组合\n", "vstack函数\n", "concatenate函数,axis参数设置为0" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2],\n", " [ 3, 4, 5],\n", " [ 6, 7, 8],\n", " [ 0, 2, 4],\n", " [ 6, 8, 10],\n", " [12, 14, 16]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vstack((a,b))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2],\n", " [ 3, 4, 5],\n", " [ 6, 7, 8],\n", " [ 0, 2, 4],\n", " [ 6, 8, 10],\n", " [12, 14, 16]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "concatenate((a,b), axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(3)深度组合\n", "深度组合,就是将一系列数组沿着纵轴(深度)方向进行层叠组合。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 0],\n", " [ 1, 2],\n", " [ 2, 4]],\n", "\n", " [[ 3, 6],\n", " [ 4, 8],\n", " [ 5, 10]],\n", "\n", " [[ 6, 12],\n", " [ 7, 14],\n", " [ 8, 16]]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dstack((a,b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(4)列组合\n", "column_stack函数。\n", "\n", "对于一维数组将按列方向进行组合。\n", "\n", "对于二维数组,效果和hstack一样。" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 0],\n", " [1, 2]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oned = arange(2)\n", "twice_oned = 2 * oned\n", "\n", "column_stack((oned, twice_oned))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 0, 2, 4],\n", " [ 3, 4, 5, 6, 8, 10],\n", " [ 6, 7, 8, 12, 14, 16]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "column_stack((a,b))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ True, True, True, True, True, True],\n", " [ True, True, True, True, True, True],\n", " [ True, True, True, True, True, True]], dtype=bool)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "column_stack((a,b)) == hstack((a,b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(5)行组合\n", "按行方向进行组合。对于两个一维数组,将直接层叠起来合成一个二维数组;对于二维数组,row_stack和vstack的效果一样。" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2],\n", " [ 3, 4, 5],\n", " [ 6, 7, 8],\n", " [ 0, 2, 4],\n", " [ 6, 8, 10],\n", " [12, 14, 16]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row_stack((a,b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##2. 数组的分割\n", "Numpy数组可以进行水平、垂直和深度分割,相关的函数有hsplit、vsplit、dsplit和split。我们可以将数组分割成相同大小的子数组,也可以指定原数组中需要分割的位置。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(1)水平分割" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[array([[0],\n", " [3],\n", " [6]]), array([[1],\n", " [4],\n", " [7]]), array([[2],\n", " [5],\n", " [8]])]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#将数组沿水平方向分割为3个相同大小的子数组\n", "hsplit(a, 3)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[array([[0],\n", " [3],\n", " [6]]), array([[1],\n", " [4],\n", " [7]]), array([[2],\n", " [5],\n", " [8]])]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#调用split函数,指定参数axis=1\n", "split(a, 3, axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(2)垂直分割" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vsplit(a, 3)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "split(a, 3, axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###(3)深度分割" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c = arange(27).reshape(3,3,3)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1, 2],\n", " [ 3, 4, 5],\n", " [ 6, 7, 8]],\n", "\n", " [[ 9, 10, 11],\n", " [12, 13, 14],\n", " [15, 16, 17]],\n", "\n", " [[18, 19, 20],\n", " [21, 22, 23],\n", " [24, 25, 26]]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[array([[[ 0],\n", " [ 3],\n", " [ 6]],\n", " \n", " [[ 9],\n", " [12],\n", " [15]],\n", " \n", " [[18],\n", " [21],\n", " [24]]]), array([[[ 1],\n", " [ 4],\n", " [ 7]],\n", " \n", " [[10],\n", " [13],\n", " [16]],\n", " \n", " [[19],\n", " [22],\n", " [25]]]), array([[[ 2],\n", " [ 5],\n", " [ 8]],\n", " \n", " [[11],\n", " [14],\n", " [17]],\n", " \n", " [[20],\n", " [23],\n", " [26]]])]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dsplit(c, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##3. 数组的属性" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* ndim属性,给出数组的维度\n", "* size属性,给出数组元素的总个数\n", "* itemsize属性,给出数组元素在内存中所占的字节数\n", "* nbytes属性,给出整个数组所占存储空间,即itemsize和size属性的乘积\n", "* T属性,和transpose函数一样\n", "* real属性给出复数数组的实部,imag属性给出复数数组的虚部" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "flat属性将返回numpy.flatiter对象,这是获得flatiter对象的唯一方式。\n", "这个所谓的“扁平迭代器”可以让我们像遍历一维数组一样遍历任意的多维数组。" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = arange(4).reshape(2,2)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [2, 3]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = b.flat\n", "f" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "for item in f:\n", " print item" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#使用flatiter对象直接获取一个数组的元素\n", "b.flat[2]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 3])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.flat[[1,3]]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#对flat属性赋值将导致整个数组的元素被覆盖\n", "b.flat = 7" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[7, 7],\n", " [7, 7]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[7, 1],\n", " [7, 1]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.flat[[1,3]] = 1\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##4. 数组的转换\n", "**tolist函数将numpy数组转换成python列表**" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[7, 1],\n", " [7, 1]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[7, 1], [7, 1]]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.tolist()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**astype函数可以在转换数组时指定数据类型**" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 7., 1.],\n", " [ 7., 1.]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.astype(float)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.5" } }, "nbformat": 4, "nbformat_minor": 0 }