mirror of
https://github.com/fenago/data-science.git
synced 2026-05-06 09:33:14 +00:00
159 lines
3.7 KiB
Plaintext
159 lines
3.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "QWyZQ1u8DuI_"
|
|
},
|
|
"source": [
|
|
"# Computing the ROC AUC for the Caesarian Dataset"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "p89bSWY_DuJD"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# import libraries\n",
|
|
"import pandas as pd\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"from sklearn.linear_model import LogisticRegression"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 204
|
|
},
|
|
"colab_type": "code",
|
|
"id": "vW0hhGg3DuJI",
|
|
"outputId": "a79179c0-dedd-41dc-f13e-93c1e2120617"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# data doesn't have headers, so let's create headers\n",
|
|
"_headers = ['Age', 'Delivery_Nbr', 'Delivery_Time', 'Blood_Pressure', 'Heart_Problem', 'Caesarian']\n",
|
|
"# read in cars dataset\n",
|
|
"df = pd.read_csv('../Dataset/caesarian.csv.arff', names=_headers, index_col=None, skiprows=15)\n",
|
|
"df.head()\n",
|
|
"\n",
|
|
"# target column is 'Caesarian'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "MGk_0UmkDuJN"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# target column is 'Caesarian'\n",
|
|
"\n",
|
|
"features = df.drop(['Caesarian'], axis=1).values\n",
|
|
"labels = df[['Caesarian']].values\n",
|
|
"\n",
|
|
"# split 80% for training and 20% into an evaluation set\n",
|
|
"X_train, X_eval, y_train, y_eval = train_test_split(features, labels, test_size=0.2, random_state=0)\n",
|
|
"\n",
|
|
"# further split the evaluation set into validation and test sets of 10% each\n",
|
|
"X_val, X_test, y_val, y_test = train_test_split(X_eval, y_eval, test_size=0.5, random_state=0)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 136
|
|
},
|
|
"colab_type": "code",
|
|
"id": "3qwJP9sEDuJQ",
|
|
"outputId": "de02ebb1-fb71-4283-f90c-1623e14af1f4"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# train a Logistic Regression model\n",
|
|
"model = LogisticRegression()\n",
|
|
"model.fit(X_train, y_train)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "v3vFYHI7DuJU"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# make predictions for the validation dataset\n",
|
|
"y_proba = model.predict_proba(X_val)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 34
|
|
},
|
|
"colab_type": "code",
|
|
"id": "s2mk5XKxDuJY",
|
|
"outputId": "28e3e3db-3e8f-42ea-da67-5febd5cae99b"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.metrics import roc_auc_score\n",
|
|
"_auc = roc_auc_score(y_val, y_proba[:, 0])\n",
|
|
"print(_auc)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"name": "Exercise6.12.ipynb",
|
|
"provenance": []
|
|
},
|
|
"file_extension": ".py",
|
|
"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.8.6"
|
|
},
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"npconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": 3
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|