Skip to content

Instantly share code, notes, and snippets.

@rasmuse
Last active January 25, 2017 15:44
Show Gist options
  • Save rasmuse/b55fe9db407ed2f43bd4b0fa9f908c7e to your computer and use it in GitHub Desktop.
Save rasmuse/b55fe9db407ed2f43bd4b0fa9f908c7e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create the data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"nx = 2\n",
"ny = 3\n",
"nz = 4\n",
"\n",
"yvals = np.array([1, 3, 5, 7])\n",
"\n",
"Y = np.random.choice(yvals, size=(nx, ny, nz))\n",
"\n",
"X = np.zeros((len(yvals), 2))\n",
"X[:, 0] = yvals\n",
"X[:, 1] = np.array([val * 10 for val in yvals])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[7, 7, 5, 5],\n",
" [5, 3, 3, 3],\n",
" [1, 1, 1, 1]],\n",
"\n",
" [[5, 1, 5, 5],\n",
" [3, 5, 7, 5],\n",
" [3, 1, 5, 1]]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Y"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1., 10.],\n",
" [ 3., 30.],\n",
" [ 5., 50.],\n",
" [ 7., 70.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Method one: Find the factors from X, go through Y, bring out the factors"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 490., 490., 250., 250.],\n",
" [ 250., 90., 90., 90.],\n",
" [ 10., 10., 10., 10.]],\n",
"\n",
" [[ 250., 10., 250., 250.],\n",
" [ 90., 250., 490., 250.],\n",
" [ 90., 10., 250., 10.]]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def find_factor(yval):\n",
" matches = X[X[:, 0] == yval, 1]\n",
" assert len(matches) == 1 # how can we be sure?\n",
" return matches[0]\n",
"\n",
"factors = np.zeros(Y.shape)\n",
"\n",
"for index, value in np.ndenumerate(Y):\n",
" factors[index] = find_factor(value)\n",
" \n",
"Y * factors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Method two: Use numpy's vectorize for convenience"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 490., 490., 250., 250.],\n",
" [ 250., 90., 90., 90.],\n",
" [ 10., 10., 10., 10.]],\n",
"\n",
" [[ 250., 10., 250., 250.],\n",
" [ 90., 250., 490., 250.],\n",
" [ 90., 10., 250., 10.]]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def multiply_factor(yval):\n",
" return find_factor(yval) * yval\n",
"\n",
"np.vectorize(multiply_factor)(Y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Method three: Ensure that the factors actually map to one value"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 490., 490., 250., 250.],\n",
" [ 250., 90., 90., 90.],\n",
" [ 10., 10., 10., 10.]],\n",
"\n",
" [[ 250., 10., 250., 250.],\n",
" [ 90., 250., 490., 250.],\n",
" [ 90., 10., 250., 10.]]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factors = {row[0]: row[1] for row in X}\n",
"\n",
"def multiply_factor(yval):\n",
" return factors[yval] * yval\n",
"\n",
"np.vectorize(multiply_factor)(Y)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:dairy-n]",
"language": "python",
"name": "conda-env-dairy-n-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment