2015-04-10 01:25:59 +08:00
|
|
|
{
|
|
|
|
"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
|
2015-04-10 01:27:00 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"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
|
2015-04-10 01:25:59 +08:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|