{ "cells": [ { "cell_type": "markdown", "id": "2a113e8a", "metadata": {}, "source": [ "# Grama: Model Building\n", "\n", "*Purpose*: Grama provides tools to work with models, but in order to use these tools we need to be able to buidl models in grama! This exercise will introduce fundamental concepts for building and sanity-checking models. We'll build on these skills in future exercises.\n" ] }, { "cell_type": "markdown", "id": "067569a6", "metadata": {}, "source": [ "## Setup\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "5b289c69", "metadata": { "collapsed": false }, "outputs": [], "source": [ "import grama as gr\n", "import pandas as pd\n", "DF = gr.Intention()\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "id": "012c4621", "metadata": {}, "source": [ "# Composition\n", "\n", "Recall that there are four classes of verb in grama; *composition* verbs take a model as an input and produce a new model as an output. Using compositions we can add information to a model, such as input metadata and functions.\n" ] }, { "cell_type": "markdown", "id": "8f4ada0b", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "id": "14f502c6", "metadata": {}, "source": [ "# Useful Programming Tools\n", "\n", "To build grama models, we'll need to use a few simple programming tools.\n" ] }, { "cell_type": "markdown", "id": "64e04f54", "metadata": {}, "source": [ "## Lambda functions\n", "\n", "In an earlier exercise, we learned how to define Python functions using the `def` keyword:\n", "\n", "```python\n", "def fcn(x):\n", " return x ** 2\n", "```\n", "\n", "However, we can define the same function with `lambda` syntax:\n", "\n", "```python\n", "fcn = lambda x: x ** 2\n", "```\n", "\n", "A `lambda` function starts with the keyword `lambda`, and is followed by its arguments. The example above has just one argument `x`. After the arguments comes a colon `:`, which signals that what follows is the output of the function.\n", "\n", "The advantage of this `lambda` syntax is that it is more compact, and can be incorporated with a grama model building pipeline. Let's get some practice defining lambda functions.\n" ] }, { "cell_type": "markdown", "id": "c5a56de8", "metadata": {}, "source": [ "### __q1__ Implement a `lambda` function\n", "\n", "Use the `lambda` syntax to implement the following function:\n", "\n", "$$f(x) = x + 1$$\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "f6435619", "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success!\n" ] } ], "source": [ "# TASK: Create a lambda function to implement the function above\n", "\n", "fcn = lambda x: x + 1\n", "# Use the following to check your work\n", "assert \\\n", " fcn(1) == 2, \\\n", " \"Incorrect value\"\n", "\n", "print(\"Success!\")" ] }, { "cell_type": "markdown", "id": "0176dd11", "metadata": {}, "source": [ "## Working with DataFrames\n", "\n", "Grama uses DataFrames to represent data and to interface with models. The constructor `gr.df_make()` is a convenient way to make a simple DataFrame:\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "d339625c", "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | x | \n", "y | \n", "z | \n", "
---|---|---|---|
0 | \n", "1 | \n", "a | \n", "recycled value | \n", "
1 | \n", "2 | \n", "b | \n", "recycled value | \n", "
2 | \n", "3 | \n", "c | \n", "recycled value | \n", "