Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alonsosilvaallende/81a55cbe798ce5a33fd838af99a00a25 to your computer and use it in GitHub Desktop.
Save alonsosilvaallende/81a55cbe798ce5a33fd838af99a00a25 to your computer and use it in GitHub Desktop.
Lifelines-sklearn-adapter-predicts-the-expectation.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Lifelines-sklearn-adapter-predicts-the-expectation.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPc82HOXEGYVhL8hKHO93lu",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/alonsosilvaallende/81a55cbe798ce5a33fd838af99a00a25/lifelines-sklearn-adapter-predicts-the-expectation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "0_IujgLW_1Ia"
},
"source": [
"!pip install --quiet lifelines"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ufiyPcyx_52n"
},
"source": [
"from lifelines.utils.sklearn_adapter import sklearn_adapter\n",
"\n",
"from lifelines import WeibullAFTFitter\n",
"from lifelines.datasets import load_rossi"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "E1wEfK1_AFPZ"
},
"source": [
"X = load_rossi().drop('week', axis=1)\n",
"Y = load_rossi().pop('week')"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Q3lbnorYAJaA"
},
"source": [
"WeibullAFTRegression = sklearn_adapter(WeibullAFTFitter, event_col='arrest')\n",
"sk_waft = WeibullAFTRegression(penalizer=1e-5)\n",
"sk_waft.fit(X, Y)\n",
"pred_sk_waft = sk_waft.predict(X)"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "6ez2TJ27BJIO"
},
"source": [
"waftf = WeibullAFTFitter(penalizer=1e-5)\n",
"X['week'] = Y\n",
"waftf.fit(X, duration_col='week', event_col='arrest')\n",
"pred_waft = waftf.predict_expectation(X)"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QO71Mv5qCCz6",
"outputId": "8304d2a6-570d-440f-d830-d165c28a95a1"
},
"source": [
"all(pred_sk_waft == pred_waft)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "EDgHNSZvC6NL"
},
"source": [
"pred_waft_median = waftf.predict_median(X)"
],
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4-5fPR45CwAO",
"outputId": "1c494dcb-9b2b-4878-beaa-26c67e1a5c22"
},
"source": [
"all(pred_sk_waft == pred_waft_median)"
],
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 8
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment