{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Course 2 week 1 lecture notebook Exercise 04\n", "# Concordance index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this week's graded assignment, you will implement the concordance index (c-index). To get some practice with what you've seen in lecture, and to prepare for this week's assignment, you will write code to find permissible pairs, concordant pairs, and risk ties.\n", "\n", "First start by importing packages and generating a small dataset. The data is small enough that you can visually check the pairs of patients." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import packages\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define the outcome `y`\n", "\n", "- You will let `y` refer to the actual health outcome of the patient.\n", "- 1 indicates disease, 0 indicates health (normal)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# define 'y', the outcome of the patient\n", "y = pd.Series([0,0,1,1,0])\n", "y.name=\"health\"\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define the risk scores\n", "Define some risk scores that some model might produce for each patient. Normally, you would run the patient features through a risk model to create these risk scores. For practice, you will use the following values in the next cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define the risk scores for each patient\n", "risk_score = pd.Series([2.2, 3.3, 4.4, 4.4])\n", "risk_score.name='risk score'\n", "risk_score" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Identify a permissible pair\n", "A pair of patients is permissible if their outcomes are different. Use code to compare the labels." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check patients 0 and 1 make a permissible pair.\n", "if y[0] != y[1]:\n", " print(f\"y[0]={y[0]} and y[1]={y[1]} is a permissible pair\")\n", "else:\n", " print(f\"y[0]={y[0]} and y[1]={y[1]} is not a permissible pair\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if patients 0 and 2 make a permissible pair\n", "if y[0] != y[2]:\n", " print(f\"y[0]={y[0]} and y[2]={y[2]} is a permissible pair\")\n", "else:\n", " print(f\"y[0]={y[0]} and y[2]={y[2]} is NOT permissible pair\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check for risk ties\n", "- For permissible pairs, check if they have the same risk score" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if patients 2 and 3 make a risk tie\n", "if risk_score[2] == risk_score[3]:\n", " print(f\"patient 2 ({risk_score[2]}) and patient 3 ({risk_score[3]}) have a risk tie\")\n", "else:\n", " print(f\"patient 2 ({risk_score[2]}) and patient 3 ({risk_score[3]}) DO NOT have a risk tie\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Concordant pairs\n", "- Check if a permissible pair is also a concordant pair\n", "- You'll check one case, where the first patient is healthy and the second has the disease." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if patient 1 and 2 make a concordant pair\n", "if y[1] == 0 and y[2] == 1:\n", " if risk_score[1] < risk_score[2]:\n", " print(f\"patient 1 and 2 is a concordant pair\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Note that you checked the situation where patient 1 is healthy and patient 2 has the disease.\n", "- You should also check the other situation where patient 1 has the disease and patient 2 is healthy.\n", "\n", "You'll practice implementing the complete algorithm for c-index in this week's assignment!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### This is the end of this practice section.\n", "\n", "Please continue on with the lecture videos!\n", "\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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }