Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Matplotlib] Ndarray
{
"cells": [
{
"cell_type": "code",
"execution_count": 117,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Nx = 3\n",
"Ny = 5\n",
"Nz = 2\n",
"a = np.ones((Nx,Ny,Nz), np.float)"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.]],\n",
"\n",
" [[ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.]],\n",
"\n",
" [[ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.]]])"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 5L, 2L)"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.ndim"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float64')"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.dtype"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Other attributs"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.]],\n",
"\n",
" [[ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.],\n",
" [ 1., 1., 1.]]])"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.T"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.]],\n",
"\n",
" [[ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.]],\n",
"\n",
" [[ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.],\n",
" [ 1., 1.]]])"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.real"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.]],\n",
"\n",
" [[ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.]],\n",
"\n",
" [[ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.],\n",
" [ 0., 0.]]])"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.imag"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<numpy.flatiter at 0x48ca540>"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.flat"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<numpy.core._internal._ctypes at 0x47ee6a0>"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.ctypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Methods"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0]],\n",
" [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0]],\n",
" [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0]]]"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.tolist()"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?'"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.tostring()"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'\\x80\\x02cnumpy.core.multiarray\\n_reconstruct\\nq\\x01cnumpy\\nndarray\\nq\\x02K\\x00\\x85U\\x01b\\x87Rq\\x03(K\\x01\\x8a\\x01\\x03\\x8a\\x01\\x05\\x8a\\x01\\x02\\x87cnumpy\\ndtype\\nq\\x04U\\x02f8K\\x00K\\x01\\x87Rq\\x05(K\\x03U\\x01<NNNJ\\xff\\xff\\xff\\xffJ\\xff\\xff\\xff\\xffK\\x00tb\\x89U\\xf0\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?\\x00\\x00\\x00\\x00\\x00\\x00\\xf0?tb.'"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.dumps()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Shpae manipulation"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 5L, 2L)"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2L, 15L)"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nNx = 2\n",
"nNy = (Nx*Ny*Nz)/ nNx\n",
"ar = a.reshape((nNx, nNy))\n",
"ar.shape"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2L, 15L)"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = a.copy()\n",
"b.resize((nNx, nNy))\n",
"b.shape"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 5L, 2L)"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2L, 5L, 3L)"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.transpose().shape"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 2L, 5L)"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.swapaxes(1,2).shape"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(30L,)"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Return a \"copy\" of the array collapsed into one dimension.\n",
"b = a.flatten()\n",
"b.shape"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(30L,)"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Return a flattened array.\n",
"b = a.ravel()\n",
"b.shape"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 5L, 2L)"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "an integer is required for the axis",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-141-7432835b6c92>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msqueeze\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: an integer is required for the axis"
]
}
],
"source": [
"a.squeeze([1])"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "an integer is required for the axis",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-142-0cfca2919c8c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msqueeze\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: an integer is required for the axis"
]
}
],
"source": [
"a.squeeze([1,1,1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Item selection and manipulation"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 5L, 2L)"
]
},
"execution_count": 143,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,\n",
" 2, 2, 2, 2, 2, 2, 2], dtype=int64),\n",
" array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 0, 0, 1,\n",
" 1, 2, 2, 3, 3, 4, 4], dtype=int64),\n",
" array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,\n",
" 1, 0, 1, 0, 1, 0, 1], dtype=int64))"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.nonzero()"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.argmax()"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = np.identity(5)"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.argmax()"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.argmin()"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.0"
]
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.min()"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.max()"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.ptp()"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.5, 0. , 0. , 0. , 0. ],\n",
" [ 0. , 0.5, 0. , 0. , 0. ],\n",
" [ 0. , 0. , 0.5, 0. , 0. ],\n",
" [ 0. , 0. , 0. , 0.5, 0. ],\n",
" [ 0. , 0. , 0. , 0. , 0.5]])"
]
},
"execution_count": 155,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.clip(a.min(), a.max()/2.0)"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 0., 0., 0., 0.],\n",
" [ 0., 1., 0., 0., 0.],\n",
" [ 0., 0., 1., 0., 0.],\n",
" [ 0., 0., 0., 1., 0.],\n",
" [ 0., 0., 0., 0., 1.]])"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.conj()"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 0., 0., 0., 0.],\n",
" [ 0., 1., 0., 0., 0.],\n",
" [ 0., 0., 1., 0., 0.],\n",
" [ 0., 0., 0., 1., 0.],\n",
" [ 0., 0., 0., 0., 1.]])"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.round()"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.trace()"
]
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 159,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.sum()"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 3.,\n",
" 3., 3., 3., 3., 3., 4., 4., 4., 4., 4., 4., 5.])"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.cumsum()"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.2, 0.2, 0.2, 0.2, 0.2])"
]
},
"execution_count": 163,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.mean(1)"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.2, 0.2, 0.2, 0.2, 0.2])"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.mean(0)"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.16, 0.16, 0.16, 0.16, 0.16])"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.var(0)"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.4, 0.4, 0.4, 0.4, 0.4])"
]
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.std(0)"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 0., 0., 0., 0.])"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.prod(1)"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.all()"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 172,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.any()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Arithmetic and comparison operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ndarray.__lt__\tx.__lt__(y) <==> x<y\n",
"ndarray.__le__\tx.__le__(y) <==> x<=y\n",
"ndarray.__gt__\tx.__gt__(y) <==> x>y\n",
"ndarray.__ge__\tx.__ge__(y) <==> x>=y\n",
"ndarray.__eq__\tx.__eq__(y) <==> x==y\n",
"ndarray.__ne__\tx.__ne__(y) <==> x!=y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Truth value of an array (bool):\n",
"\n",
"ndarray.__nonzero__\tx.__nonzero__() <==> x != 0\n",
"Note\n",
"Truth-value testing of an array invokes ndarray.__nonzero__, which raises an error if the number of elements in the the array is larger than 1, because the truth value of such arrays is ambiguous. Use .any() and .all() instead to be clear about what is meant in such cases. (If the number of elements is 0, the array evaluates to False.)\n",
"\n",
"Unary operations:\n",
"\n",
"ndarray.__neg__\tx.__neg__() <==> -x\n",
"ndarray.__pos__\tx.__pos__() <==> +x\n",
"ndarray.__abs__() <==> abs(x)\t\n",
"ndarray.__invert__\tx.__invert__() <==> ~x\n",
"Arithmetic:\n",
"\n",
"ndarray.__add__\tx.__add__(y) <==> x+y\n",
"ndarray.__sub__\tx.__sub__(y) <==> x-y\n",
"ndarray.__mul__\tx.__mul__(y) <==> x*y\n",
"ndarray.__div__\tx.__div__(y) <==> x/y\n",
"ndarray.__truediv__\tx.__truediv__(y) <==> x/y\n",
"ndarray.__floordiv__\tx.__floordiv__(y) <==> x//y\n",
"ndarray.__mod__\tx.__mod__(y) <==> x%y\n",
"ndarray.__divmod__(y) <==> divmod(x, y)\t\n",
"ndarray.__pow__(y[, z]) <==> pow(x, y[, z])\t\n",
"ndarray.__lshift__\tx.__lshift__(y) <==> x<<y\n",
"ndarray.__rshift__\tx.__rshift__(y) <==> x>>y\n",
"ndarray.__and__\tx.__and__(y) <==> x&y\n",
"ndarray.__or__\tx.__or__(y) <==> x|y\n",
"ndarray.__xor__\tx.__xor__(y) <==> x^y\n",
"Note\n",
"Any third argument to pow is silently ignored, as the underlying ufunc takes only two arguments.\n",
"The three division operators are all defined; div is active by default, truediv is active when __future__ division is in effect.\n",
"Because ndarray is a built-in type (written in C), the __r{op}__ special methods are not directly defined.\n",
"The functions called to implement many arithmetic special methods for arrays can be modified using set_numeric_ops.\n",
"Arithmetic, in-place:\n",
"\n",
"ndarray.__iadd__\tx.__iadd__(y) <==> x+=y\n",
"ndarray.__isub__\tx.__isub__(y) <==> x-=y\n",
"ndarray.__imul__\tx.__imul__(y) <==> x*=y\n",
"ndarray.__idiv__\tx.__idiv__(y) <==> x/=y\n",
"ndarray.__itruediv__\tx.__itruediv__(y) <==> x/y\n",
"ndarray.__ifloordiv__\tx.__ifloordiv__(y) <==> x//y\n",
"ndarray.__imod__\tx.__imod__(y) <==> x%=y\n",
"ndarray.__ipow__\tx.__ipow__(y) <==> x**=y\n",
"ndarray.__ilshift__\tx.__ilshift__(y) <==> x<<=y\n",
"ndarray.__irshift__\tx.__irshift__(y) <==> x>>=y\n",
"ndarray.__iand__\tx.__iand__(y) <==> x&=y\n",
"ndarray.__ior__\tx.__ior__(y) <==> x|=y\n",
"ndarray.__ixor__\tx.__ixor__(y) <==> x^=y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Special methods"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For standard library functions:\n",
"\n",
"ndarray.__copy__([order])\tReturn a copy of the array.\n",
"ndarray.__deepcopy__(() -> Deep copy of array.)\tUsed if copy.deepcopy is called on an array.\n",
"ndarray.__reduce__()\tFor pickling.\n",
"ndarray.__setstate__(version, shape, dtype, ...)\tFor unpickling.\n",
"Basic customization:\n",
"\n",
"ndarray.__new__((S, ...)\t\n",
"ndarray.__array__(...)\tReturns either a new reference to self if dtype is not given or a new array of provided data type if dtype is different from the current dtype of the array.\n",
"ndarray.__array_wrap__(...)\t\n",
"Container customization: (see Indexing)\n",
"\n",
"ndarray.__len__() <==> len(x)\t\n",
"ndarray.__getitem__\tx.__getitem__(y) <==> x[y]\n",
"ndarray.__setitem__\tx.__setitem__(i, y) <==> x[i]=y\n",
"ndarray.__getslice__\tx.__getslice__(i, j) <==> x[i:j]\n",
"ndarray.__setslice__\tx.__setslice__(i, j, y) <==> x[i:j]=y\n",
"ndarray.__contains__\tx.__contains__(y) <==> y in x\n",
"Conversion; the operations complex, int, long, float, oct, and hex. They work only on arrays that have one element in them and return the appropriate scalar.\n",
"\n",
"ndarray.__int__() <==> int(x)\t\n",
"ndarray.__long__() <==> long(x)\t\n",
"ndarray.__float__() <==> float(x)\t\n",
"ndarray.__oct__() <==> oct(x)\t\n",
"ndarray.__hex__() <==> hex(x)\t\n",
"String representations:\n",
"\n",
"ndarray.__str__() <==> str(x)\t\n",
"ndarray.__repr__() <==> repr(x)\t"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.