data-science-ipython-notebooks/numpy/numpy.ipynb

450 lines
8.3 KiB
Plaintext

{
"metadata": {
"name": "",
"signature": "sha256:cb8fc4454a69123dcb745c323968d06c15444cee91494edb720893b06e98c249"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NumPy\n",
"\n",
"* NumPy Arrays, dtype, and shape\n",
"* Common Array Operations\n",
"* Reshaping and In-Place Updating\n",
"* Combining Arrays\n",
"* Creating Fake Data and Adding Noise"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## NumPy Arrays, dtypes, and shapes"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = np.array([1, 2, 3])\n",
"print(a)\n",
"print(a.shape)\n",
"print(a.dtype)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[1 2 3]\n",
"(3,)\n",
"int64\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"b = np.array([[0, 2, 4], [1, 3, 5]])\n",
"print(b)\n",
"print(b.shape)\n",
"print(b.dtype)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[0 2 4]\n",
" [1 3 5]]\n",
"(2, 3)\n",
"int64\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.zeros(5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"array([ 0., 0., 0., 0., 0.])"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.ones(shape=(3, 4), dtype=np.int32)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"array([[1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 1]], dtype=int32)"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Common Array Operations"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c = b * 0.5\n",
"print(c)\n",
"print(c.shape)\n",
"print(c.dtype)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0. 1. 2. ]\n",
" [ 0.5 1.5 2.5]]\n",
"(2, 3)\n",
"float64\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d = a + c\n",
"print(d)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 1. 3. 5. ]\n",
" [ 1.5 3.5 5.5]]\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"array([ 1., 3., 5.])"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d[0, 0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"1.0"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d[:, 0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"array([ 1. , 1.5])"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d.sum()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"19.5"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d.mean()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"3.25"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d.sum(axis=0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"array([ 2.5, 6.5, 10.5])"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d.mean(axis=1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"array([ 3. , 3.5])"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reshaping and In-Place Updating"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"e = np.arange(12)\n",
"print(e)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11]\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# f is a view of contents of e\n",
"f = e.reshape(3, 4)\n",
"print(f)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Set last five values of e to zero\n",
"e[5:] = 0\n",
"print(e)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0 1 2 3 4 0 0 0 0 0 0 0]\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# f is also updated\n",
"f"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
"array([[0, 1, 2, 3],\n",
" [4, 0, 0, 0],\n",
" [0, 0, 0, 0]])"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# OWNDATA shows f does not own its data\n",
"f.flags"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"text": [
" C_CONTIGUOUS : True\n",
" F_CONTIGUOUS : False\n",
" OWNDATA : False\n",
" WRITEABLE : True\n",
" ALIGNED : True\n",
" UPDATEIFCOPY : False"
]
}
],
"prompt_number": 19
}
],
"metadata": {}
}
]
}