Create an application to predict your insurance premium cost with AutoAI

Login/Sign-up to IBM Cloud: http://ibm.biz/insuranceChargesUsingAutoAI

finalDemo

As shown above, this application leverages machine learning models to predict your insurance charges, and helps the customer understand how smoking or decreasing your BMI affects insurance premiums.

As we see the value of gross insurance premiums worldwide continue to skyrocket past 5 trillion dollars, we know that most of these costs are preventable. For example, just by eliminating smoking, and lowering your BMI by a few points could mean shaving thousands of dollars off of your premium charges. In this application, we study the effects of age, smoking, BMI, gender, and region to determine how much of a difference these factors can make on your insurance premium. By using our application, customers see the radical difference their lifestyle choices make on their insurance charges. By leveraging AI and machine learning, we help customers understand just how much smoking increases their premium, by predicting how much they will have to pay within seconds.

Description

Using IBM AutoAI, you automate all the tasks involved in building predictive models for different requirements. You see how AutoAI generates great models quickly which save time and effort and aid in faster decision-making process. You create a model that from a data set that includes the age, sex, BMI, number-of-children, smoking preferences, region and charges to predict the health insurance premium cost that an individual pays.

When you have completed this code pattern, you understand how to:

  • Setup, quickly, the services on IBM Cloud for building the model.
  • Ingest the data and initiate the AutoAI process.
  • Build different models using AutoAI and evaluate the performance.
  • Choose the best model and complete the deployment.
  • Generate predictions using the deployed model by making REST calls.
  • Compare the process of using AutoAI and building the model manually.
  • Visualize the deployed model using a front-end application.

Architecture Components

Architecture Components

Flow Description

  1. The user creates an IBM Watson Studio Service on IBM Cloud.
  2. The user creates an IBM Cloud Object Storage Service and adds that to Watson Studio.
  3. The user uploads the insurance premium data file into Watson Studio.
  4. The user creates an AutoAI Experiment to predict insurance premium on Watson Studio
  5. AutoAI uses Watson Machine Learning to create several models, and the user deploys the best performing model.
  6. The user uses the Flask web-application to connect to the deployed model and predict an insurance charge.

Included components

  • IBM Watson Studio - IBM Watson® Studio helps data scientists and analysts prepare data and build models at scale across any cloud.
  • IBM Watson Machine Learning - IBM Watson® Machine Learning helps data scientists and developers accelerate AI and machine-learning deployment.
  • IBM Cloud Object Storage - IBM Cloud™ Object Storage makes it possible to store practically limitless amounts of data, simply and cost effectively.

Featured technologies

  • artificial-intelligence - Build and train models, and create apps, with a trusted AI-infused platform.
  • Python - Python is an interpreted, high-level, general-purpose programming language.

Prerequisites

This Cloud pattern assumes you have an IBM Cloud account. Go to the link below to sign up for a no-charge trial account - no credit card required.

Steps

  1. Download the data set
  2. Clone the repo
  3. Explore the data (optional)
  4. Create IBM Cloud services
  5. Create and Run AutoAI experiment
  6. Create a deployment and test your model
  7. Create a notebook from your model (optional)
  8. Run the application

Step 1. Create IBM Cloud services

Creating-Watson-Service

  1. Login to your IBM Cloud account: http://ibm.biz/insuranceChargesUsingAutoAI

  2. Within your IBM Cloud account, click on the top search bar to search for cloud services and offerings. Type in “Watson Studio” and then click on Watson Studio under “Catalog Results”.

  3. This takes you to the Watson Studio service page. Select a region, “Lite” plan (Free) and give your service a unique name. Click on “Create” and this creates a Watson Studio instance for you.

  4. Once the service instance is ready, you will be redirected to the Watson Studio page. Click on the “Get Started” button to launch Watson Studio in a new tab. This might take few minutes to set up the service.

Creating-empty-project

  1. Under the heading “work with data” you will find a link that says, “Create a project”. Click on “Create a project”. Next, click on “Create an empty project”.

  2. On the new project page, give your project a name. You will also need to associate an IBM Cloud Object Storage instance to store the data set.

  3. Under “Select Storage Service”, click on the “Add” button. This takes you to the IBM Cloud Object Store service page. Leave the service on the “Lite” tier and then click the “Create” button at the bottom of the page. You are prompted to name the service and choose the resource group. Once you give a name, click “Create”.

  4. Once the instance is created, you’re taken back to the project page. Click on “refresh” and you should see your newly created Cloud Object Storage instance under Storage.

  5. Click the “Create” button at the bottom right of the page to create your project.

Add-dataset

  1. Click on the “Add to project” button on the top right corner. From the pop-up window select “Data”.

  2. In the column, on the right, click on “browse”. Navigate to the folder where you downloaded the data set to and select “insurance.csv”

  3. Watson Studio takes a couple of seconds to load the data, and then you should see the import has completed. To make sure it has worked properly, you can click on “Assets” on the top of the page, and you should see your insurance file under “Data Assets”.

Step 4. Create and Run AutoAI experiment

create-AutoAI-project

  1. Once you have added the data set, click on the “Add to project” button on the top right corner. This time select “AutoAI experiment”.

  2. On the New AutoAI Experiment page, give a name to your project.

  3. Next, you need to add a Watson Machine Learning instance. On the right side of the screen, click on “Associate a Machine Learning service instance”.

  4. On the Associate service page, click on the “New Service” button on the right side of the screen. From the pop-up screen, select “Machine Learning”.

  5. Select the appropriate region. It is recommended to build your machine learning service instance in the same region that you created your Watson Studio service. Select the “Lite” (free) plan. Give your instance a unique name. Click on “Create”.

  6. Once you create your machine learning service, on the next page, check the box with your machine learning service instance. Next, click on “associate service” on the right corner.

  7. Once the service is successfully associated, you will be redirected to new AutoAI experiment page. Click on “Reload” on the right side of the screen. You should see your newly created machine learning instance. Click on “Create” on the bottom right part of your screen to create your first AutoAI experiment!

settings-of-project

  1. After you create your experiment, you are taken to a page to add a data source to your project. Click on “Select from project” and then add the insurance.csv file. Click on Select asset to confirm your data source.

  2. Next, you see that AutoAI processes your data, and you see a What do you want to predict section. Select the expenses as the Prediction column.

  3. Next, let's explore the AutoAI settings to see what you can customize when running your experiment. Click on Experiment settings.First, you see the data source tab, which lets you omit certain columns from your experiment. You choose to leave all columns. You can also select the training data split. It defaults to 85% training data. The data source tab also shows which metric you optimize for. For the regression, it is RMSE (Root Mean Squared Error), and for other types of experiments, such as Binary Classification, AutoAI defaults to Accuracy. Either way, you can change the metric from this tab depending on your use case.

  4. Click on the Prediction tab from within the Experiment settings. There you can select from Binary Classification, Regression, and Multiclass Classification.

  5. Lastly, you can see the Runtime tab from the Experiment settings this shows you other experiment details you may want to change depending on your use case.

  6. Once you are happy with your settings, ensure you are predicting for the expense’s column, and click on the run Run Experiment button on the bottom-right corner of the screen.

  7. Next, your AutoAI experiment runs on its own. You see a progress map on the right side of the screen which shows which stage of the experiment is running. This may be Hyper Parameter Optimization, feature engineering, or some other stage.

  8. You have different pipelines that are created, and you see the rankings of each model. Each model is ranked based on the metric that you selected. In the specific case that is the RMSE(Root mean squared error). Given that you want that number to be as small as possible, you can see that in the experiment, the model with the smallest RMSE is at the top of the leaderboard.

completed-pipeline

  1. Once the experiment is done, you see Experiment completed under the Progress map on the right-hand side of the screen.

best-pipeline

  1. Now that AutoAI has successfully generated eight different models, you can rank the models by different metrics, by clicking on the drop-down next to “Rank by:” on the top right corner of the screen, such as explained variance, root mean squared error, R-Squared, and mean absolute error. Each time you select a different metric, the models are re-ranked by that metric.

best-pipeline-features

  1. In our case, we have RMSE as the experiment's metric. You see the smallest RMSE value is 4444.108, from Pipeline 4. Click on “Pipeline 4”.

  2. On the left-hand side, you can see different “Model Evaluation Measures”. For this particular model, you can view the metrics, such as explained variance, RMSE, and other metrics.

  3. On the left-hand side, you can also see “Feature Transformations”, and “Feature Importance”.

  4. On the left-hand side, click on “Feature Importance”. You can see here that the most important predictor of the insurance premium is whether you are a “smoker” or a “non-smoker”. This is by far the most important feature, with “bmi” coming in as the second most important. This makes sense, given that many companies offer discounts for employees who do not smoke.

Step 5. Create a deployment and test your model

deployment-space

  1. Once you are ready to deploy one of the models, click on “Save As” at the top-right corner of the model you want to deploy. Save it as a “Model” and name your model as you want. Click on “Create” Note: We show you how to save it as a notebook in step 6.

  2. Once the model is successfully saved, click on the “View in project” in the green notification on the right side of the screen. Alternatively, you can also find your model saved in the “Assets” tab under “Models”.

  3. Next, you are taken to a screen that has the overview of the model you just saved. Click on “Promote to deployment space” on the top right corner of your screen. Alternatively, if you’re doing it from the Assets tab, then under the “Models” section, click on the 3 dots on the right side of your screen and click “promote”.

  4. On the Promote to space page, you need a target space to promote your model. Click on “New space +” on the right side of your screen.

  5. Next, on the Create a deployment space screen, give your space a name, make sure the right cloud object storage is selected, and select your machine learning service instance. For this experiment, selecting the machine learning service is mandatory as we need to build a prediction model. Then click on “Create”.

  6. Once the space is ready, click on “Close” in the pop-up and you will be redirected to the promote to space page. You see your newly created space under the “Target space”. Once you’re happy with your selections, click on “Promote”.

deploy

  1. Once the model is successfully promoted, you will see a green notification box, click on “deployment space” in the notification. Alternatively, you can also find your deployment spaces when you click on the hamburger sign on the top left most side on your screen.

  2. You will be redirected to the deployments page, where you will find your promoted model. Hover over the row, to see a rocket shaped icon, click on the icon to deploy you model.

  3. In the dialog box, select “Online” as your deployment type, give your deployment a name and click “Create”.

  4. Click on the “Deployments” tab to see the status of your deployment.

deployment-completed

test

  1. Once the deployment is completed, click on the name your deployment.

  2. On this page you find the API references, endpoint and code snippets to help you integrate your model with your applications.

  3. To test your model, click on the “Test” tab. You can select a row from the data set and enter the data in the fields. Enter the age, sex, bmi, children, smoker and region and then click on the “Predict” button at the bottom.

  4. To validate the prediction, you check the data file that you used to train the model. As you can see, the model predicted a premium of 18524.04, when you enter age 19, bmi: 27.9, children: 0, smoker: yes, region: southwest. This is relatively close to the model's prediction, so we know the model is working properly.

Step 6. Create a notebook from your model (optional)

With AutoAI's latest features, the code that is run to create these models is no more a black box. One or more of these models can be saved as a Jupyter notebook and the Python code can be run and enhanced from within.

6.1 Create notebook

create notebook

  • Click on Save As at the top-right corner of the model, and click Notebook.

  • This opens a new tab (be sure to enable pop-up for this website) titled New Notebook where in you can edit the default name if you choose to and then click on Create. This might take a few minutes to load for the first time.

also create notebook

  • Alternatively, you can also create the notebook from the Pipeline leaderboard view (shown above) by clicking on the Save as option against the model you want to save followed by selecting Notebook. The steps are very similar to the first method discussed above.

Step 7. Run the application

The driver code to run the application can be found under the web-app folder within the git repository that was cloned from Step 1. To run and test your deployed model through this Python-based user-interface, you need to replace the following information within web-app/app.py:

  1. Your Watson Machine Learning (which is associated with this deployed model) Instance ID and apikey.
  2. Your deployed model's deployment URL, so you can make a POST request.
  3. Your IBM Cloud IAM token, to authorize yourself.

Now, you go into detail on how to gather these credentials. If you already know how to do this, you can skip the steps below, and go straight to running the application.

7.1 Get IBM Cloud API key

  • Generate an IBM Cloud apikey by going to cloud.ibm.com and then from the top-right part of the screen click on Manage-> IAM.

manage

  • Next, click on API keys from the left side-bar. Next click on Create an IBM Cloud API key.

create-api-key

  • Name the key as you wish, and then click Create.

create

  • Once the key is created, click on the Download button.

download