mirror of
https://github.com/fenago/data-science.git
synced 2026-05-04 00:22:32 +00:00
194 lines
4.4 KiB
Plaintext
194 lines
4.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "eH9MyFiEFbIR"
|
|
},
|
|
"source": [
|
|
"# Compute the Accuracy Score of the Classification Model"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "OQqQCWiaFbIT"
|
|
},
|
|
"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": "Eib9phN4FbIX",
|
|
"outputId": "9588f465-c1e1-46dc-9c2c-0737e874373b"
|
|
},
|
|
"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": "s0BOgelbFbIa",
|
|
"outputId": "6fb93d35-62ab-4260-dc2b-56d1fc249d97"
|
|
},
|
|
"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": "dyqJOifIFbId"
|
|
},
|
|
"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": "vmSqmEhlFbIf",
|
|
"outputId": "f3cedafd-7544-4dc7-ae9e-bf8ac2ff2ef4"
|
|
},
|
|
"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": "sXk0Qno9FbIh"
|
|
},
|
|
"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": "EHQwM9V1FbIj"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# import necessary library\n",
|
|
"from sklearn.metrics import accuracy_score"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 35
|
|
},
|
|
"colab_type": "code",
|
|
"id": "4qyAmnw7FbIl",
|
|
"outputId": "752f9f1c-1879-44fe-a9e5-1c6b8df2788f"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"_accuracy = accuracy_score(y_val, y_pred)\n",
|
|
"print(_accuracy)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"name": "Exercise6_10.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
|
|
}
|