{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Basics\n", "\n", "This tutorial is a quick introduction to training and testing your model with Vowpal Wabbit using Python. We explore passing some data to Vowpal Wabbit to learn a model and get a prediction.\n", "\n", "For more advanced Vowpal Wabbit tutorials, including how to format data and understand results, see [Tutorials](https://vowpalwabbit.org/tutorials.html).\n", "\n", "```{admonition} Prerequisites\n", "To install Vowpal Wabbit see [Get Started](https://vowpalwabbit.org/start.html).\n", "```\n", "\n", "## Getting started\n", "\n", "First, import the [Vowpal Wabbit Python package](https://github.com/VowpalWabbit/vowpal_wabbit/tree/master/python) for this tutorial:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import vowpalwabbit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we create an instance of Vowpal Wabbit, and pass the `quiet=True` option to avoid diagnostic information output to `stdout` location:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = vowpalwabbit.Workspace(quiet=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Training scenario and dataset\n", "\n", "For this tutorial scenario, we want Vowpal Wabbit to help us predict whether or not our house will require a new roof in the next 10 years.\n", "\n", "To create some examples, we use the Vowpal Wabbit text format and then learn on them:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_examples = [\n", " \"0 | price:.23 sqft:.25 age:.05 2006\",\n", " \"1 | price:.18 sqft:.15 age:.35 1976\",\n", " \"0 | price:.53 sqft:.32 age:.87 1924\",\n", "]\n", "\n", "for example in train_examples:\n", " model.learn(example)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> **Note:** For more details on Vowpal Wabbit input format and feature hashing techniques see the [Linear Regression Tutorial](cmd_linear_regression.md).\n", "\n", "Now, we create a `test_example` to use for prediction:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_example = \"| price:.46 sqft:.4 age:.10 1924\"\n", "\n", "prediction = model.predict(test_example)\n", "print(prediction)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vowpal Wabbit results\n", "\n", "The model predicted a value of **0**. According to our learning model, our house will not need a new roof in the next 10 years (at least that is the result from just three examples we used in our training dataset).\n", "\n", "## More to explore\n", "\n", "- To learn how to approach a contextual bandits problem using Vowpal Wabbit — including how to work with different contextual bandits approaches, how to format data, and understand the results — see the [Contextual Bandit Reinforcement Learning Tutorial](python_Contextual_bandits_and_Vowpal_Wabbit.ipynb).\n", "- For more on the contextual bandits approach to reinforcement learning, including a content personalization scenario, see the [Contextual Bandit Simulation Tutorial](python_Simulating_a_news_personalization_scenario_using_Contextual_Bandits.ipynb).\n", "- See the [Linear Regression Tutorial](cmd_linear_regression.md) for a different look at the roof replacement problem and learn more about Vowpal Wabbit's format and understanding the results.\n" ] } ], "metadata": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" }, "kernelspec": { "display_name": "Python 3.8.2 64-bit", "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.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }