Skip to content

Instantly share code, notes, and snippets.

@kanungo
Created September 14, 2014 21:08
Show Gist options
  • Save kanungo/41e66ab2df9d99db6090 to your computer and use it in GitHub Desktop.
Save kanungo/41e66ab2df9d99db6090 to your computer and use it in GitHub Desktop.
Numpy attributes and methods
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"\n",
"\n",
"# The 'shape' attribute\n",
"y = np.zeros((2, 3, 4))\n",
"print y\n",
"print y.shape\n",
"\n",
"\n",
"# The transpose attribute\n",
"x = np.array([[1, 2],\n",
" [3, 4],\n",
" [5, 6]]) \n",
"print x\n",
"print x.T"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"\n",
"x = np.array([[1,2,3,4],\n",
" [2,3,4,5],\n",
" [6,7,8,9],\n",
" [8,7,6,5]])\n",
"# the 'diagonal' method\n",
"a = x.diagonal()\n",
"print a\n",
"\n",
"b = x.diagonal(offset=1)\n",
"print b\n",
"\n",
"c = x.diagonal(offset=-1)\n",
"print c\n",
"\n",
"# The 'reshape' method\n",
"import numpy as np\n",
"a = np.array(range(9))\n",
"print 'a', a\n",
"b = a.reshape(3,3)\n",
"print 'b', b"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Array calculations, quite self explanatory\n",
"import numpy as np\n",
"\n",
"a = np.array(range(4)).reshape(2,2)\n",
"\n",
"print np.cumsum(a)\n",
"print np.cumsum(a, axis=0)\n",
"print np.cumprod(a)\n",
"print np.max(a)\n",
"print np.min(a)\n",
"print np.sum(a)\n",
"print np.prod(a)\n",
"print np.mean(a)\n",
"print np.var(a)\n",
"print np.std(a)\n",
"print np.trace(a)"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment