From 06ce1825b3e940630b0f62f15cbc3dd88f6c9ed3 Mon Sep 17 00:00:00 2001 From: Axel Barck-Holst <102673853+AxelHolst@users.noreply.github.com> Date: Fri, 5 Jul 2024 15:15:16 +0200 Subject: [PATCH] Add Python Syntax Medical Insurance Project and Solution notebooks --- ...hon Syntax Medical Insurance Project.ipynb | 496 ++++++++++++++++++ ...x Medical Insurance Project_Solution.ipynb | 451 ++++++++++++++++ 2 files changed, 947 insertions(+) create mode 100644 Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project.ipynb create mode 100644 Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project_Solution.ipynb diff --git a/Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project.ipynb b/Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project.ipynb new file mode 100644 index 0000000..9b3bae3 --- /dev/null +++ b/Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project.ipynb @@ -0,0 +1,496 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "7ac921d1", + "metadata": {}, + "source": [ + "# Python Syntax: Medical Insurance Project" + ] + }, + { + "cell_type": "markdown", + "id": "84c02393", + "metadata": {}, + "source": [ + "Suppose you are a medical professional curious about how certain factors contribute to medical insurance costs. Using a formula that estimates a person's yearly insurance costs, you will investigate how different factors such as age, sex, BMI, etc. affect the prediction." + ] + }, + { + "cell_type": "markdown", + "id": "05392afb", + "metadata": {}, + "source": [ + "## Setting up Factors" + ] + }, + { + "cell_type": "markdown", + "id": "e433bd9f", + "metadata": {}, + "source": [ + "1. Our first step is to create the variables for each factor we will consider when estimating medical insurance costs.\n", + "\n", + " These are the variables we will need to create:\n", + " - `age`: age of the individual in years\n", + " - `sex`: 0 for female, 1 for male*\n", + " - `bmi`: individual's body mass index\n", + " - `num_of_children`: number of children the individual has\n", + " - `smoker`: 0 for a non-smoker, 1 for a smoker\n", + " \n", + " In the code block below, create the following variables for a **28**-year-old, **nonsmoking woman** who has **three children** and a **BMI** of **26.2**.\n", + " \n", + " **Note**: We are using this [medical insurance dataset](https://www.kaggle.com/mirichoi0218/insurance) as a guide, which unfortunately does not include data for non-binary individuals." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7f311028", + "metadata": {}, + "outputs": [], + "source": [ + "# create the initial variables below\n", + "# Initializing variables\n", + "age = 28\n", + "smoker = 0 # 1 if smoker, 0 if not a smoker\n", + "sex = 0 # 0 if female, 1 if male\n", + "num_of_children = 3\n", + "bmi = 26.2\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "2b7c2cbc", + "metadata": {}, + "source": [ + "## Working with the Formula" + ] + }, + { + "cell_type": "markdown", + "id": "6db974a5", + "metadata": {}, + "source": [ + "2. After the declaration of the variables, create a variable called `insurance_cost` that utilizes the following formula:\n", + "\n", + " $$\n", + " \\begin{aligned}\n", + " insurance\\_cost = 250*age - 128*sex \\\\\n", + " + 370*bmi + 425*num\\_of\\_children \\\\\n", + " + 24000*smoker - 12500 \\\\\n", + " \\end{aligned}\n", + " $$" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "91f86188", + "metadata": {}, + "outputs": [], + "source": [ + "# Add insurance estimate formula below\n", + "# Insurance formula\n", + "insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "90e6bbe0", + "metadata": {}, + "source": [ + "3. Let's display this value in an informative way. Print out the following string in the kernel:\n", + "\n", + " ```\n", + " This person's insurance cost is {insurance_cost} dollars.\n", + " ```\n", + " \n", + " You will need to use string concatenation, including the `str()` function to print out the `insurance_cost`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "cf6d3790", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This person's insurance cost is 5469.0 dollars.\n" + ] + } + ], + "source": [ + "# Print out the insurance cost\n", + "print(\"This person's insurance cost is \" + str(insurance_cost) + \" dollars.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "203e7e61", + "metadata": {}, + "source": [ + "## Looking at Age Factor" + ] + }, + { + "cell_type": "markdown", + "id": "5c2f65d1", + "metadata": {}, + "source": [ + "4. We have seen how our formula can estimate costs for one individual. Now let's play with some individual factors to see what role each one plays in our estimation!\n", + "\n", + " Let's start with the `age` factor. Using a plus-equal operator, add 4 years to our `age` variable." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "eb23f0c0", + "metadata": {}, + "outputs": [], + "source": [ + "# Add 4 years to age\n", + "age += 4\n" + ] + }, + { + "cell_type": "markdown", + "id": "a18a8926", + "metadata": {}, + "source": [ + "5. Now that we have changed our `age` value, we want to recalculate our insurance cost. Declare a new variable called `new_insurance_cost` in the code block below.\n", + "\n", + " Make sure you leave the `insurance_cost` variable the same as in Task 2. We will use it later in our program!" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "50f6a44c", + "metadata": {}, + "outputs": [], + "source": [ + "# Calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n" + ] + }, + { + "cell_type": "markdown", + "id": "d6c2393a", + "metadata": {}, + "source": [ + "6. Next, we want to find the difference between our `new_insurance_cost` and `insurance_cost`. To do this, let's create a new variable called `change_in_insurance_cost` and set it equal to the difference between `new_insurance_cost` and `insurance_cost`.\n", + "\n", + " Note: depending on the order that we subtract (eg., `new_insurance_cost - insurance_cost` vs. `insurance_cost - new_insurance_cost`), we'll get a positive or negative version of the same number. To make this difference interpretable, let's calculate `new_insurance cost - insurance_cost`. Then we can say, \"people who are four years older have estimated insurance costs that are `change_in_insurance_cost` dollars different, where the sign of `change_in_insurance_cost` tells us whether the cost is higher or lower\"." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6b72279c", + "metadata": {}, + "outputs": [], + "source": [ + "# Calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost\n" + ] + }, + { + "cell_type": "markdown", + "id": "2e6df15c", + "metadata": {}, + "source": [ + "7. We want to display this information in an informative way similar to the output from instruction 3. In the code block below, print the following string, where `XXX` is replaced by the value of `change_in_insurance_cost`:\n", + "\n", + " ```\n", + " The change in cost of insurance after increasing the age by 4 years is XXX dollars.\n", + " ```\n", + " \n", + " Doing this will tell us how 4 years in age affects medical insurance cost estimates assuming that all other variables remain the same.\n", + " \n", + " You will need to concatenate strings and use the `str()` method." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "0a999d40", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in cost of insurance after increasing the age by 4 years is 1000.0 dollars.\n" + ] + } + ], + "source": [ + "print(\"The change in cost of insurance after increasing the age by 4 years is \" + str(change_in_insurance_cost) + \" dollars.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "23bc90cf", + "metadata": {}, + "source": [ + "## Looking at BMI Factor" + ] + }, + { + "cell_type": "markdown", + "id": "81747d10", + "metadata": {}, + "source": [ + "8. Now that you have looked at the age factor, let's move onto another one: BMI. First, we have to redefine our `age` variable to be its original value.\n", + "\n", + " Set `age` to `28`. This will reset its value and allow us to focus on just the change in the BMI factor moving forward.\n", + " \n", + " On the next line, using the plus-equal operator, add `3.1` to our `bmi` variable." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "53ea7df3", + "metadata": {}, + "outputs": [], + "source": [ + "# Reset age to 28\n", + "age = 28\n", + "\n", + "# Add 3.1 to BMI\n", + "bmi += 3.1\n" + ] + }, + { + "cell_type": "markdown", + "id": "febe2c07", + "metadata": {}, + "source": [ + "9. Now let's find out how a change in BMI affects insurance costs. Our next steps are pretty much the same as we have done before when looking at `age`.\n", + " 1. Below the line where `bmi` was increased by `3.1`, rewrite the insurance cost formula and assign it to the variable name `new_insurance_cost`.\n", + " 2. Save the difference between `new_insurance_cost` and `insurance_cost` in a variable called `change_in_insurance_cost`.\n", + " 3. Display the following string in the output terminal, where `XXX` is replaced by the value of `change_in_insurance_cost`:\n", + " \n", + " ```py\n", + " The change in estimated insurance cost after increasing BMI by 3.1 is XXX dollars.\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "19d121c8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in estimated insurance cost after increasing BMI by 3.1 is 1147.0 dollars.\n" + ] + } + ], + "source": [ + "# Calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n", + "\n", + "# Calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost\n", + "\n", + "print(\"The change in estimated insurance cost after increasing BMI by 3.1 is \" + str(change_in_insurance_cost) + \" dollars.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "26c3c473", + "metadata": {}, + "source": [ + "## Looking at Male vs. Female Factor" + ] + }, + { + "cell_type": "markdown", + "id": "e0c96041", + "metadata": {}, + "source": [ + "10. Let's look at the effect sex has on medical insurance costs. Before we make any additional changes, first reassign your `bmi` variable back to its original value of `26.2`.\n", + "\n", + " On a new line of code in the code block below, reassign the value of `sex` to `1`. A reminder that `1` identifies male individuals and `0` identifies female individuals." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "ae87bfec", + "metadata": {}, + "outputs": [], + "source": [ + "# Reset BMI to original value\n", + "bmi = 26.2\n", + "\n", + "# Change sex to male\n", + "sex = 1\n" + ] + }, + { + "cell_type": "markdown", + "id": "da20c656", + "metadata": {}, + "source": [ + "11. Perform the steps below!\n", + " 1. Rewrite the insurance cost formula and assign it to the variable name `new_insurance_cost`.\n", + " 2. Save the difference between `new_insurance_cost` and `insurance_cost` in a variable called `change_in_insurance_cost`.\n", + " 3. Display the following string, where `XXX` is replaced by the value of `change_in_insurance_cost`:\n", + " ```\n", + " The change in estimated cost for being male instead of female is XXX dollars.\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "ce2da0e8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in estimated insurance cost for being male instead of female is -128.0 dollars.\n" + ] + } + ], + "source": [ + "# Calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n", + "\n", + "# Calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost\n", + "\n", + "print(\"The change in estimated insurance cost for being male instead of female is \" + str(change_in_insurance_cost) + \" dollars.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "f652d964", + "metadata": {}, + "source": [ + "12. Notice that this time you got a negative value for `change_in_insurance_cost`. Let's think about what that means. We changed the sex variable from `0` (female) to `1` (male) and it decreased the estimated insurance costs.\n", + "\n", + " This means that men tend to have lower medical costs on average than women. Reflect on the other findings you have dug up from this investigation so far." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1190661d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "deae95f4", + "metadata": {}, + "source": [ + "## Extra Practice" + ] + }, + { + "cell_type": "markdown", + "id": "2e44bb53", + "metadata": {}, + "source": [ + "13. Great job on the project!!!\n", + "\n", + " So far we have looked at 3 of the 5 factors in the insurance costs formula. The two remaining are `smoker` and `num_of_children`. If you want to keep challenging yourself, spend some time investigating these factors!\n", + " 1. Rewrite the insurance cost formula and assign it to the variable name `new_insurance_cost`.\n", + " 2. Save the difference between `new_insurance_cost` in a variable called `change_in_insurance_cost`.\n", + " 3. Display the information below!" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "ee321873", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in estimated cost for being a smoker is 23872.0 dollars.\n" + ] + } + ], + "source": [ + "# Change smoker status to 1 (smoker)\n", + "smoker = 1\n", + "\n", + "# Calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n", + "\n", + "# Calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost\n", + "\n", + "print(\"The change in estimated cost for being a smoker is \" + str(change_in_insurance_cost) + \" dollars.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "de87f356", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in estimated cost for having 5 children is 24722.0 dollars.\n" + ] + } + ], + "source": [ + "# Change number of children to 5\n", + "num_of_children = 5\n", + "\n", + "# Calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n", + "\n", + "# Calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost\n", + "\n", + "print(\"The change in estimated cost for having 5 children is \" + str(change_in_insurance_cost) + \" dollars.\")\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project_Solution.ipynb b/Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project_Solution.ipynb new file mode 100644 index 0000000..70b5f0d --- /dev/null +++ b/Python Syntax Medical Insurance Project/Python Syntax Medical Insurance Project_Solution.ipynb @@ -0,0 +1,451 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "95c4ebc5", + "metadata": {}, + "source": [ + "# Python Syntax: Medical Insurance Project" + ] + }, + { + "cell_type": "markdown", + "id": "97508254", + "metadata": {}, + "source": [ + "Suppose you are a medical professional curious about how certain factors contribute to medical insurance costs. Using a formula that estimates a person's yearly insurance costs, you will investigate how different factors such as age, sex, BMI, etc. affect the prediction." + ] + }, + { + "cell_type": "markdown", + "id": "c908a017", + "metadata": {}, + "source": [ + "## Setting up Factors" + ] + }, + { + "cell_type": "markdown", + "id": "da275cba", + "metadata": {}, + "source": [ + "1. Our first step is to create the variables for each factor we will consider when estimating medical insurance costs.\n", + "\n", + " These are the variables we will need to create:\n", + " - `age`: age of the individual in years\n", + " - `sex`: 0 for female, 1 for male*\n", + " - `bmi`: individual's body mass index\n", + " - `num_of_children`: number of children the individual has\n", + " - `smoker`: 0 for a non-smoker, 1 for a smoker\n", + " \n", + " In the code block below, create the following variables for a **28**-year-old, **nonsmoking woman** who has **three children** and a **BMI** of **26.2**.\n", + " \n", + " **Note**: We are using this [medical insurance dataset](https://www.kaggle.com/mirichoi0218/insurance) as a guide, which unfortunately does not include data for non-binary individuals." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3f11ddb3", + "metadata": {}, + "outputs": [], + "source": [ + "# initializing variables\n", + "age = 28\n", + "smoker = 0 # 1 if smoker 0 if not a smoker\n", + "sex = 0 # 0 if female 1 if male\n", + "num_of_children = 3\n", + "bmi = 26.2" + ] + }, + { + "cell_type": "markdown", + "id": "6f182a4c", + "metadata": {}, + "source": [ + "## Working with the Formula" + ] + }, + { + "cell_type": "markdown", + "id": "b23a6f40", + "metadata": {}, + "source": [ + "2. After the declaration of the variables, create a variable called `insurance_cost` that utilizes the following formula:\n", + "\n", + " $$\n", + " \\begin{aligned}\n", + " insurance\\_cost = 250*age - 128*sex \\\\\n", + " + 370*bmi + 425*num\\_of\\_children \\\\\n", + " + 24000*smoker - 12500 \\\\\n", + " \\end{aligned}\n", + " $$" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c8305f0d", + "metadata": {}, + "outputs": [], + "source": [ + "# insurance formula written out\n", + "insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n" + ] + }, + { + "cell_type": "markdown", + "id": "611ab52c", + "metadata": {}, + "source": [ + "3. Let's display this value in an informative way. Print out the following string in the kernel:\n", + "\n", + " ```\n", + " This person's insurance cost is {insurance_cost} dollars.\n", + " ```\n", + " \n", + " You will need to use string concatenation, including the `str()` function to print out the `insurance_cost`." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "7b182d95", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This person's insurance cost is 5469.0 dollars.\n" + ] + } + ], + "source": [ + "# print out the insurance cost\n", + "print(\"This person's insurance cost is \" + str(insurance_cost) + \" dollars.\")" + ] + }, + { + "cell_type": "markdown", + "id": "dad95da1", + "metadata": {}, + "source": [ + "## Looking at Age Factor" + ] + }, + { + "cell_type": "markdown", + "id": "91b3c35e", + "metadata": {}, + "source": [ + "4. We have seen how our formula can estimate costs for one individual. Now let's play with some individual factors to see what role each one plays in our estimation!\n", + "\n", + " Let's start with the `age` factor. Using a plus-equal operator, add 4 years to our `age` variable." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "893b53ae", + "metadata": {}, + "outputs": [], + "source": [ + "# add 4 years to age\n", + "age += 4" + ] + }, + { + "cell_type": "markdown", + "id": "86297956", + "metadata": {}, + "source": [ + "5. Now that we have changed our `age` value, we want to recalculate our insurance cost. Declare a new variable called `new_insurance_cost` in the code block below.\n", + "\n", + " Make sure you leave the `insurance_cost` variable the same as in Task 2. We will use it later in our program!" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f5318944", + "metadata": {}, + "outputs": [], + "source": [ + "# calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500" + ] + }, + { + "cell_type": "markdown", + "id": "91b3a2de", + "metadata": {}, + "source": [ + "6. Next, we want to find the difference between our `new_insurance_cost` and `insurance_cost`. To do this, let's create a new variable called `change_in_insurance_cost` and set it equal to the difference between `new_insurance_cost` and `insurance_cost`.\n", + "\n", + " Note: depending on the order that we subtract (eg., `new_insurance_cost - insurance_cost` vs. `insurance_cost - new_insurance_cost`), we'll get a positive or negative version of the same number. To make this difference interpretable, let's calculate `new_insurance cost - insurance_cost`. Then we can say, \"people who are four years older have estimated insurance costs that are `change_in_insurance_cost` dollars different, where the sign of `change_in_insurance_cost` tells us whether the cost is higher or lower\"." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b5c1e242", + "metadata": {}, + "outputs": [], + "source": [ + "# calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost" + ] + }, + { + "cell_type": "markdown", + "id": "9a5432c0", + "metadata": {}, + "source": [ + "7. We want to display this information in an informative way similar to the output from instruction 3. In the code block below, print the following string, where `XXX` is replaced by the value of `change_in_insurance_cost`:\n", + "\n", + " ```\n", + " The change in cost of insurance after increasing the age by 4 years is XXX dollars.\n", + " ```\n", + " \n", + " Doing this will tell us how 4 years in age affects medical insurance cost estimates assuming that all other variables remain the same.\n", + " \n", + " You will need to concatenate strings and use the `str()` method." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "e48f76ca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in cost of insurance after increasing the age by 4 years is 1000.0 dollars.\n" + ] + } + ], + "source": [ + "print(\"The change in cost of insurance after increasing the age by 4 years is \" + str(change_in_insurance_cost) + \" dollars.\")" + ] + }, + { + "cell_type": "markdown", + "id": "7d5d05f3", + "metadata": {}, + "source": [ + "## Looking at BMI Factor" + ] + }, + { + "cell_type": "markdown", + "id": "1ca1f623", + "metadata": {}, + "source": [ + "8. Now that you have looked at the age factor, let's move onto another one: BMI. First, we have to redefine our `age` variable to be its original value.\n", + "\n", + " Set `age` to `28`. This will reset its value and allow us to focus on just the change in the BMI factor moving forward.\n", + " \n", + " On the next line, using the plus-equal operator, add `3.1` to our `bmi` variable." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "8063ff67", + "metadata": {}, + "outputs": [], + "source": [ + "# rewrite age as original age\n", + "age = 28\n", + "\n", + "# add 3.1 to bmi\n", + "bmi += 3.1" + ] + }, + { + "cell_type": "markdown", + "id": "da2ef25a", + "metadata": {}, + "source": [ + "9. Now let's find out how a change in BMI affects insurance costs. Our next steps are pretty much the same as we have done before when looking at `age`.\n", + " 1. Below the line where `bmi` was increased by `3.1`, rewrite the insurance cost formula and assign it to the variable name `new_insurance_cost`.\n", + " 2. Save the difference between `new_insurance_cost` and `insurance_cost` in a variable called `change_in_insurance_cost`.\n", + " 3. Display the following string in the output terminal, where `XXX` is replaced by the value of `change_in_insurance_cost`:\n", + " \n", + " ```py\n", + " The change in estimated insurance cost after increasing BMI by 3.1 is XXX dollars.\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "1ece3cdf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in estimated insurance cost after increasing BMI by 3.1 is 1147.0 dollars.\n" + ] + } + ], + "source": [ + "# calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n", + "# calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost\n", + "print(\"The change in estimated insurance cost after increasing BMI by 3.1 is \" + str(change_in_insurance_cost) + \" dollars.\")" + ] + }, + { + "cell_type": "markdown", + "id": "11e37d92", + "metadata": {}, + "source": [ + "## Looking at Male vs. Female Factor" + ] + }, + { + "cell_type": "markdown", + "id": "2a5d9108", + "metadata": {}, + "source": [ + "10. Let's look at the effect sex has on medical insurance costs. Before we make any additional changes, first reassign your `bmi` variable back to its original value of `26.2`.\n", + "\n", + " On a new line of code in the code block below, reassign the value of `sex` to `1`. A reminder that `1` identifies male individuals and `0` identifies female individuals." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "1fcf7d9f", + "metadata": {}, + "outputs": [], + "source": [ + "# rewrite bmi as original value\n", + "bmi = 26.2\n", + "\n", + "# male vs. female factor\n", + "# change value of sex to 1\n", + "sex = 1" + ] + }, + { + "cell_type": "markdown", + "id": "e1e43161", + "metadata": {}, + "source": [ + "11. Perform the steps below!\n", + " 1. Rewrite the insurance cost formula and assign it to the variable name `new_insurance_cost`.\n", + " 2. Save the difference between `new_insurance_cost` and `insurance_cost` in a variable called `change_in_insurance_cost`.\n", + " 3. Display the following string, where `XXX` is replaced by the value of `change_in_insurance_cost`:\n", + " ```\n", + " The change in estimated cost for being male instead of female is XXX dollars.\n", + " ```" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "1f2ae3c0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The change in estimated insurance cost for being male instead of female is -128.0 dollars.\n" + ] + } + ], + "source": [ + "# calculate the new insurance cost\n", + "new_insurance_cost = 250 * age - 128 * sex + 370 * bmi + 425 * num_of_children + 24000 * smoker - 12500\n", + "# calculate change between the new insurance cost and original insurance cost\n", + "change_in_insurance_cost = new_insurance_cost - insurance_cost\n", + "print(\"The change in estimated insurance cost for being male instead of female is \" + str(change_in_insurance_cost) + \" dollars.\")" + ] + }, + { + "cell_type": "markdown", + "id": "b7bd3d8e", + "metadata": {}, + "source": [ + "12. Notice that this time you got a negative value for `change_in_insurance_cost`. Let's think about what that means. We changed the sex variable from `0` (female) to `1` (male) and it decreased the estimated insurance costs.\n", + "\n", + " This means that men tend to have lower medical costs on average than women. Reflect on the other findings you have dug up from this investigation so far." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5a0ab991", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "2220ee2e", + "metadata": {}, + "source": [ + "## Extra Practice" + ] + }, + { + "cell_type": "markdown", + "id": "b457067e", + "metadata": {}, + "source": [ + "13. Great job on the project!!!\n", + "\n", + " So far we have looked at 3 of the 5 factors in the insurance costs formula. The two remaining are `smoker` and `num_of_children`. If you want to keep challenging yourself, spend some time investigating these factors!\n", + " 1. Rewrite the insurance cost formula and assign it to the variable name `new_insurance_cost`.\n", + " 2. Save the difference between `new_insurance_cost` in a variable called `change_in_insurance_cost`.\n", + " 3. Display the information below!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1e360256", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9cc366d9", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}