{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "from random import randint\n", "from numpy.random import choice" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Define our parameters of interest\n", "max_try = 100000\n", "voc = [\"x\", \"x\", \" \", \"+\", \"-\", \"*\", \"/\", \"1\", \"2\", \"3\"]\n", "max_chars = 10\n", "\n", "x_array = np.arange(-3,3,0.4)\n", "real_function = lambda x: x**2 + x\n", "y_real = real_function(x_array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "tries = []\n", "funcs = []\n", "for n in range(max_try):\n", " ## make some random function using the vocabulary\n", " thefunc = \"\".join(choice(voc, randint(1,max_chars)))\n", " \n", " ## construct two python statement, declaring the lambda function and evaluating it at X\n", " mylam = \"y = lambda x: \" + thefunc + '\\n'\n", " mylam += 'rez = y(x_array)'\n", " funcs.append(thefunc)\n", " try:\n", " ## this may be volitile so be warned!\n", " ## Couch everything in error statements, and\n", " ## simply throw away functions that aren't reasonable\n", " exec(mylam)\n", " except (SyntaxError, NameError):\n", " continue\n", " except OverflowError:\n", " print(\"I couldn't even finish, the number was too big...\")\n", " continue\n", " \n", " try:\n", " err = abs(rez - y_real).sum() \n", " except OverflowError:\n", " print('Whoah, chill out dude.\\nYour number was: {0}\\nYour func was: {1}\\n'.\\\n", " format(rez, thefunc))\n", " err = np.inf\n", " tries.append( ( err ,thefunc))\n", " if (err < 0.0001):\n", " ## we got something really close\n", " break\n", " del rez\n", " del y\n", "\n", "### numpy arrays handle NaN and INF gracefully, so we put\n", "### answer into an array before sorting\n", "a = np.array(tries, dtype=[('rez','f'), (\"func\",'|S10')])\n", "a.sort()\n", "\n", "if a[0][\"rez\"] < 0.001:\n", " print(\"took us ntries = {0}, but we eventually found that '{1}' is functionally equivalent to f(X)\".format(n,a[0][\"func\"]))\n", "else:\n", " print(\"after ntries = {0}, we found that '{1}' is close to f(x) (metric = {2})\".format(n,a[0][\"func\"],a[0][\"rez\"]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for err, func in a:\n", " print('Error: {0} | Function: {1}'.format(err, func))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }