Added numpy snippets for combining arrays.

This commit is contained in:
Donne Martin 2015-04-09 13:29:27 -04:00
parent 475e930f8e
commit 6b6a0dafb8

View File

@ -442,6 +442,143 @@
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Combining Arrays"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
"array([1, 2, 3])"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"array([[0, 2, 4],\n",
" [1, 3, 5]])"
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"array([[ 1. , 3. , 5. ],\n",
" [ 1.5, 3.5, 5.5]])"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.concatenate([a, a, a])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"array([1, 2, 3, 1, 2, 3, 1, 2, 3])"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Use broadcasting when needed to do this automatically\n",
"np.vstack([a, b, d])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
"array([[ 1. , 2. , 3. ],\n",
" [ 0. , 2. , 4. ],\n",
" [ 1. , 3. , 5. ],\n",
" [ 1. , 3. , 5. ],\n",
" [ 1.5, 3.5, 5.5]])"
]
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# In machine learning, useful to enrich or \n",
"# add new/concatenate features with hstack\n",
"np.hstack([b, d])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
"array([[ 0. , 2. , 4. , 1. , 3. , 5. ],\n",
" [ 1. , 3. , 5. , 1.5, 3.5, 5.5]])"
]
}
],
"prompt_number": 25
}
],
"metadata": {}