Skip to content

Instantly share code, notes, and snippets.

@rbiswas4
Created December 28, 2015 05:11
Show Gist options
  • Save rbiswas4/53fed35263bdb3d35916 to your computer and use it in GitHub Desktop.
Save rbiswas4/53fed35263bdb3d35916 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a number of methods that allow us to build a 2D `numpy.ndarray` from a number of columns. It is a little harder to append a column to a `numpy.ndarray`. In both cases, however, `np.c_` works. Illustrated below."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy.testing.utils import assert_allclose"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x1 = np.random.random(size=10)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x2 = np.random.random(size=10)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.98365875 0.72902872 0.91876326 0.69135701 0.39505799 0.16029197\n",
" 0.91822414 0.35778621 0.96983128 0.60210966]\n"
]
}
],
"source": [
"print(x1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.4477385 0.26924824 0.37417844 0.61372395 0.53000419 0.78225189\n",
" 0.7915513 0.86612061 0.60639261 0.23050247]\n"
]
}
],
"source": [
"print(x2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x3 = np.random.random(size=len(x1))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.60961559 0.88330827 0.38948932 0.92968805 0.65306924 0.45488228\n",
" 0.30477829 0.92612084 0.85976936 0.67704528]\n"
]
}
],
"source": [
"print(x3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### To build"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"arr = np.empty(shape=(len(x1),2))\n",
"arr[:, 0] = x1\n",
"arr[:, 1] = x2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0.98365875 0.4477385 ]\n",
" [ 0.72902872 0.26924824]\n",
" [ 0.91876326 0.37417844]\n",
" [ 0.69135701 0.61372395]\n",
" [ 0.39505799 0.53000419]\n",
" [ 0.16029197 0.78225189]\n",
" [ 0.91822414 0.7915513 ]\n",
" [ 0.35778621 0.86612061]\n",
" [ 0.96983128 0.60639261]\n",
" [ 0.60210966 0.23050247]]\n"
]
}
],
"source": [
"print(arr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Other methods \n",
"\n",
"While all the different methods work on this easily, I was not aware of method1"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"method1 = np.c_[x1, x2]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"method2 = np.array([x1, x2]).T"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"method3 = np.array(zip(x1, x2))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"assert_allclose(arr, method1)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"assert_allclose(arr, method2)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"assert_allclose(arr, method3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Now to build the 2D array with 3 columns\n",
"\n",
"Again, doing this from the individual columns is easier than appending the column to the 2 column `numpy.ndarray` by all of the other methods. `method1` is quite nifty here."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"arr3c = np.empty(shape=(len(x1), 3))\n",
"arr3c[:, 0] = x1\n",
"arr3c[:, 1] = x2\n",
"arr3c[:, 2] = x3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### From individual columns"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"method1_3 = np.c_[x1, x2, x3]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"method2_3 = np.array([x1, x2, x3]).T"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"method3_3 = np.array(zip(x1, x2, x3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### From the array"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = np.c_[arr, x3]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"assert_allclose(x, arr3c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The others don't work, and probably need a layer of list comprehension"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[0.9836587455388892, 0.44773849662426657], 0.6096155922181713],\n",
" [[0.7290287205744772, 0.26924823511619966], 0.88330826766873316],\n",
" [[0.9187632649518817, 0.37417844298667813], 0.38948931921290275],\n",
" [[0.691357011711442, 0.6137239503591148], 0.92968804690670237],\n",
" [[0.39505798716349694, 0.5300041889574952], 0.65306923537717032],\n",
" [[0.16029196645129973, 0.7822518872170735], 0.45488228196852942],\n",
" [[0.9182241436937221, 0.7915512983342922], 0.30477828704136989],\n",
" [[0.35778621149379897, 0.8661206058820488], 0.92612083900837938],\n",
" [[0.9698312829207162, 0.6063926056387653], 0.85976936161382711],\n",
" [[0.6021096554913019, 0.23050246590737478], 0.67704527503701506]], dtype=object)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(zip(arr.tolist(), x3))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[0.9836587455388892, 0.44773849662426657],\n",
" [0.7290287205744772, 0.26924823511619966],\n",
" [0.9187632649518817, 0.37417844298667813],\n",
" [0.691357011711442, 0.6137239503591148],\n",
" [0.39505798716349694, 0.5300041889574952],\n",
" [0.16029196645129973, 0.7822518872170735],\n",
" [0.9182241436937221, 0.7915512983342922],\n",
" [0.35778621149379897, 0.8661206058820488],\n",
" [0.9698312829207162, 0.6063926056387653],\n",
" [0.6021096554913019, 0.23050246590737478]],\n",
" [0.6096155922181713, 0.8833082676687332, 0.38948931921290275,\n",
" 0.9296880469067024, 0.6530692353771703, 0.4548822819685294,\n",
" 0.3047782870413699, 0.9261208390083794, 0.8597693616138271,\n",
" 0.6770452750370151]], dtype=object)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array([arr.tolist(), x3])"
]
},
{
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment