Files
mlessentials/Lab06/Exercise6.09/Exercise6_09.ipynb
T
Your Name 54ccb1423f added
2021-02-08 11:17:02 +00:00

205 lines
4.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "yWqvA2_fE2nk"
},
"source": [
"# Compute F1 Score for a Classification Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "sBGMX-ryE2nm"
},
"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\n",
"\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 194
},
"colab_type": "code",
"id": "wbul_wmIE2nq",
"outputId": "2cae4071-42ce-4e2b-d5c7-0bdfe83dc35b"
},
"outputs": [],
"source": [
"# data doesn't have headers, so let's create headers\n",
"_headers = ['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety', 'car']\n",
"# read in cars dataset\n",
"df = pd.read_csv('../Dataset/car.data', names=_headers, index_col=None)\n",
"df.head()\n",
"\n",
"# target column is 'car'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 214
},
"colab_type": "code",
"id": "HrN1L8P3E2nu",
"outputId": "2236ae8d-e435-496e-b51a-2d85eab568b2"
},
"outputs": [],
"source": [
"# encode categorical variables\n",
"_df = pd.get_dummies(df, columns=['buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety'])\n",
"_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "DDAG8hVfE2ny"
},
"outputs": [],
"source": [
"# target column is 'car'\n",
"\n",
"features = _df.drop(['car'], axis=1).values\n",
"labels = _df[['car']].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.3, 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": 161
},
"colab_type": "code",
"id": "GA8hpmM9E2n1",
"outputId": "b9ff6429-38ba-4e1c-d9be-e246270d1c37"
},
"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": "gDS1hANyE2n5"
},
"outputs": [],
"source": [
"# make predictions for the validation dataset\n",
"y_pred = model.predict(X_val)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Kx-raue1E2n7"
},
"outputs": [],
"source": [
"#import libraries\n",
"from sklearn.metrics import f1_score"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "BsCGsj0ZE2n9",
"outputId": "d6ee47ff-2742-48d1-f18c-da51ba3e1bb7"
},
"outputs": [],
"source": [
"f1_score = f1_score(y_val, y_pred, average='macro')\n",
"print(f1_score)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "BVZPdGEYE2n_"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"name": "Exercise6_09.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
}