-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89a4164
commit d6c3acc
Showing
15 changed files
with
808 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
".lhs:\n", | ||
" y\n", | ||
".rhs:\n", | ||
" root:\n", | ||
" 1 + x_hat + `a:b_hat` + d_hat\n", | ||
" .deps:\n", | ||
" [0]:\n", | ||
" .lhs:\n", | ||
" x + a:b\n", | ||
" .rhs:\n", | ||
" 1 + z\n", | ||
" [1]:\n", | ||
" .lhs:\n", | ||
" d\n", | ||
" .rhs:\n", | ||
" 1 + z2" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from formulaic import Formula\n", | ||
"\n", | ||
"Formula(\" y ~ [x + a:b ~ z] + [d ~ z2]\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 65, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'factor', 'prior_apps'}" | ||
] | ||
}, | ||
"execution_count": 65, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from formulaic import Formula\n", | ||
"from formulaic.utils.variables import get_expression_variables\n", | ||
"f = Formula(\"apps ~ prior_apps + I(prior_apps**2) + factor + prior_apps:factor\")\n", | ||
"set(\n", | ||
" variable\n", | ||
" for term in f.rhs\n", | ||
" for factor in term.factors\n", | ||
" for variable in get_expression_variables(factor.expr, {})\n", | ||
" if \"value\" in variable.roles\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 50, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from formulaic.parser.types import Term, Factor" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 56, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "TypeError", | ||
"evalue": "unsupported operand type(s) for +: 'Term' and 'Term'", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | ||
"Cell \u001b[0;32mIn[56], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m (\u001b[43mTerm\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43mFactor\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mx\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mTerm\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43mFactor\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mz\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m) \u001b[38;5;241m*\u001b[39m Term([Factor(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124my\u001b[39m\u001b[38;5;124m\"\u001b[39m)])\n", | ||
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Term' and 'Term'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"(Term([Factor(\"x\")]*2) + Term([Factor(\"z\")])) * Term([Factor(\"y\")])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 58, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
".lhs:\n", | ||
" y\n", | ||
".rhs:\n", | ||
" [0]:\n", | ||
" 1 + w\n", | ||
" [1]:\n", | ||
" 1 + x\n", | ||
" [2]:\n", | ||
" 1 + z" | ||
] | ||
}, | ||
"execution_count": 58, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from formulaic import Formula\n", | ||
"\n", | ||
"Formula(\"y ~ w | x | z\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 52, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<style scoped>\n", | ||
" .dataframe tbody tr th:only-of-type {\n", | ||
" vertical-align: middle;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe tbody tr th {\n", | ||
" vertical-align: top;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe thead th {\n", | ||
" text-align: right;\n", | ||
" }\n", | ||
"</style>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th>Intercept</th>\n", | ||
" <th>varlist('X*')[X1]</th>\n", | ||
" <th>varlist('X*')[X2]</th>\n", | ||
" <th>varlist2('X*')[X1]</th>\n", | ||
" <th>varlist2('X*')[X2]</th>\n", | ||
" <th>varlist('X*')[X1]:varlist2('X*')[X1]</th>\n", | ||
" <th>varlist('X*')[X2]:varlist2('X*')[X1]</th>\n", | ||
" <th>varlist('X*')[X1]:varlist2('X*')[X2]</th>\n", | ||
" <th>varlist('X*')[X2]:varlist2('X*')[X2]</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th>0</th>\n", | ||
" <td>1.0</td>\n", | ||
" <td>1</td>\n", | ||
" <td>1</td>\n", | ||
" <td>1</td>\n", | ||
" <td>1</td>\n", | ||
" <td>1</td>\n", | ||
" <td>1</td>\n", | ||
" <td>1</td>\n", | ||
" <td>1</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>1.0</td>\n", | ||
" <td>2</td>\n", | ||
" <td>2</td>\n", | ||
" <td>2</td>\n", | ||
" <td>2</td>\n", | ||
" <td>4</td>\n", | ||
" <td>4</td>\n", | ||
" <td>4</td>\n", | ||
" <td>4</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>1.0</td>\n", | ||
" <td>3</td>\n", | ||
" <td>3</td>\n", | ||
" <td>3</td>\n", | ||
" <td>3</td>\n", | ||
" <td>9</td>\n", | ||
" <td>9</td>\n", | ||
" <td>9</td>\n", | ||
" <td>9</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" Intercept ... varlist('X*')[X2]:varlist2('X*')[X2]\n", | ||
"0 1.0 ... 1\n", | ||
"1 1.0 ... 4\n", | ||
"2 1.0 ... 9\n", | ||
"\n", | ||
"[3 rows x 9 columns]" | ||
] | ||
}, | ||
"execution_count": 52, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import pandas\n", | ||
"import re\n", | ||
"\n", | ||
"from formulaic import Formula\n", | ||
"from formulaic.utils.stateful_transforms import stateful_transform\n", | ||
"\n", | ||
"@stateful_transform\n", | ||
"def varlist(pattern, _context=None):\n", | ||
" pattern = re.compile(pattern)\n", | ||
" return {\n", | ||
" variable: values\n", | ||
" for variable, values in _context.named_layers.get(\"data\", {}).items()\n", | ||
" if pattern.match(variable)\n", | ||
" }\n", | ||
"\n", | ||
"Formula(\"varlist('X*') * varlist2('X*')\").get_model_matrix(pandas.DataFrame({\"X1\": [1,2,3], \"X2\": [1,2,3]}), context={\"varlist\": varlist, \"varlist2\": varlist})\n" | ||
] | ||
} | ||
], | ||
"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.11.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.