{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "aT6GeRByoY5A" }, "source": [ "# Scores for 5-Fold Cross Validation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "abZunjb_oY5C" }, "outputs": [], "source": [ "# import libraries\n", "import pandas as pd\n", "from sklearn.model_selection import train_test_split" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 266 }, "colab_type": "code", "id": "wpkTWO-coY5L", "outputId": "6290fca0-0e4e-48dc-f85a-b6c7cdada9b2" }, "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.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 214 }, "colab_type": "code", "id": "oDsc5i1foY5U", "outputId": "f92db29c-6b5b-4878-f98d-4e3d7d239862" }, "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": "0hxHxwL-oY5Y" }, "outputs": [], "source": [ "# separate features and labels DataFrames\n", "features = _df.drop(['car'], axis=1).values\n", "labels = _df[['car']].values" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "44fH2w1toY5d" }, "outputs": [], "source": [ "from sklearn.linear_model import LogisticRegression\n", "# create an instance of LogisticRegression\n", "_lr = LogisticRegression()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "FirJx1IYoY5m" }, "outputs": [], "source": [ "from sklearn.model_selection import cross_val_score" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 215 }, "colab_type": "code", "id": "_VaCDyJaoY5r", "outputId": "349887df-5d97-4049-c489-270209e9d782" }, "outputs": [], "source": [ "_scores = cross_val_score(_lr, features, labels, cv=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "colab_type": "code", "id": "-SN0OdcJoY5y", "outputId": "47eb0616-4523-4df3-bfba-7530846c20e9" }, "outputs": [], "source": [ "print(_scores)" ] } ], "metadata": { "colab": { "name": "Exercise7.05.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 }