{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "oCxk4bB6xLRw" }, "source": [ "## Building your first real machine learning model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the companion notebook for the [article available at The DataRevenue Blog](https://datarevenue.com/en-blog/how-machine-learning-works-code-example). You should read the article and follow along with the code samples provided here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each time you come to a piece of code, click on it and then press the `>| Run` button in the toolbar at the top. That code will run and the results will appear right below it.\n", "\n", "![running code cells](using-jupyter.gif)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading the dataset\n", "\n", "Here we use the `pandas` library to read a dataset of Titanic passengers and look at the first few rows of the dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 535 }, "colab_type": "code", "id": "TUu6TG2JxdV1", "outputId": "e7208030-1415-48ce-ecc6-15ed86a42d8d" }, "outputs": [], "source": [ "import pandas as pd\n", "from DRLearn import DRLearn\n", "\n", "titanic_dataset = pd.read_csv(\"titanic.csv\", index_col=0)\n", "titanic_dataset.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring the dataset\n", "\n", "Let's visualise some aspects of our dataset before we start the machine learning analysis." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting survival rate by class\n", "\n", "Here we show how which class ticket the passenger had affects their survival chance. People in 1st class are more likely to survive than those in 2nd or 3rd." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 299 }, "colab_type": "code", "id": "h3-tTeUuxqKt", "outputId": "990497e9-d253-4f80-98d1-5726d7b96428" }, "outputs": [], "source": [ "DRLearn.plot_passenger_class(titanic_dataset)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting survival rate by gender\n", "\n", "Here we show how the passenger's gender affects their survival chance. Women are far more likely to survive than men." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 319 }, "colab_type": "code", "id": "yBCUfVy2xww9", "outputId": "8cd3326d-9521-4833-f482-ff5d3f4eecac" }, "outputs": [], "source": [ "DRLearn.plot_passenger_gender(titanic_dataset)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "uifqVkTSxMQ8" }, "source": [ "## Preparing our data for the algorithm\n", "\n", "Before we can train a machine learning model, we need to prepare our data. This means reformatting some of it to be more machine-friendly, and deleting parts that are unlikely to be helpful." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 0 }, "colab_type": "code", "id": "nxobUhkZtPf-", "outputId": "2ad82cca-20c9-4efd-f975-2865c7fce3c3" }, "outputs": [], "source": [ "selected_features, target = DRLearn.extract_features(titanic_dataset)\n", "selected_features.sample(5)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "VZpiz5dUvGXC" }, "source": [ "Our data is now more difficult to read for humans, but easier for machines.\n", "\n", "## Splitting our dataset into two parts: training and test\n", "\n", "We need part of our data to train the algorithm, and part of it to evaluate how well the algorithm does. Here we split it into a training and test set." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 0 }, "colab_type": "code", "id": "5UGk1od2vLt2", "outputId": "2e4c6954-b595-4689-8a4a-9a252fdb027e" }, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = DRLearn.split_dataset(selected_features, target, split=0.2)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "1ul4yJxPx8Qt" }, "source": [ "## Training our model\n", "\n", "The part we have been waiting for. In this step, we feed the data to the algorithm and ask it to find patterns automatically." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 0 }, "colab_type": "code", "id": "8jZvpfK-dbH6", "outputId": "dad68c0b-2950-4e4a-ddba-9be58acdc65b" }, "outputs": [], "source": [ "model = DRLearn.train_model(X_train, y_train)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluating the model\n", "\n", "We need to know how much the model has learned. Here we give it the 'test' part of the dataset (which it didn't see before) and compute model accuracy." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 0 }, "colab_type": "code", "id": "6iG5FbO1eMlm", "outputId": "e7ac1b88-c377-4344-eeed-4b640330554a" }, "outputs": [], "source": [ "DRLearn.evaluate_model(model, X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "hJGYYxMl3jpP" }, "source": [ "## Analysing our model\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "j_ZKZxxrbrG_" }, "source": [ "Here we find which aspects of the data the model learned were specifically interesting. We can see that gender is very important to predicting survival rate." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "colab_type": "code", "id": "zANhJGosbSE9", "outputId": "d441ac40-71ee-4b1d-f002-2688398268af" }, "outputs": [], "source": [ "DRLearn.explain_model(model, X_train)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also analyse how it makes predictions for specific passengers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model_interpretation = DRLearn.interpret_model(model, X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "change the number in the next line to see the anaysis for different passengers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "passenger_number = 3\n", "DRLearn.analyze_passenger_prediction(model_interpretation, X_test, passenger_number)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Passenger 3 has a 93% survival chance based on the fact that she is female and not in 3rd class (`Class_3=0`). The fact that she is not in 1st class lowers her survival chances slightly (the blue section)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Understanding how the quantity of data affects our model\n", "\n", "Here we train our model multiple times with different amounts of data. We can see that the more data the model has, the better it does." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DRLearn.visualise_training_progress(model, X_train, y_train, X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusion\n", "\n", "You've built and trained your first machine model. Congratulations! Now you can\n", "\n", "* Understand what data science teams do day-to-day\n", "* Better communicate with your data science or machine learning team\n", "* Know what kind of problems machine learning is best for\n", "* See that machine learning is not so intimidating as a concept\n", "\n", "\n", "The complex part of machine learning is getting into all the nitty-gritty details of building and scaling a customized solution. And that’s exactly what we specialise in, so if you need help [let us know](https://datarevenue.com).\n" ] } ], "metadata": { "colab": { "name": "Machine_Learning_for_Managers", "provenance": [] }, "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.7.7" } }, "nbformat": 4, "nbformat_minor": 1 }