{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Application of the Principal Component Analysis on the Iris dataset" ], "metadata": { "id": "b94slaKNpac5" } }, { "cell_type": "markdown", "source": [ "I apply the PCA of the Iris dataset with different amounts of components noting the loss of accuracy" ], "metadata": { "id": "MW-CAN33pxyR" } }, { "cell_type": "markdown", "source": [ "First of all, I import the necessary libraries" ], "metadata": { "id": "e4y6CSXhqCtg" } }, { "cell_type": "code", "source": [ "from sklearn.decomposition import PCA\n", "from sklearn.preprocessing import StandardScaler\n", "import pandas as pd\n", "from sklearn.datasets import load_iris\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.model_selection import cross_val_score" ], "metadata": { "id": "wGyrwmhbq5v5" }, "execution_count": 13, "outputs": [] }, { "cell_type": "markdown", "source": [ "I load Iris dataset showing the first rows" ], "metadata": { "id": "Wl4yTdyOqQEn" } }, { "cell_type": "code", "source": [ "iris = load_iris()\n", "X = iris['data']\n", "y = iris['target']\n", "\n", "df = pd.DataFrame(iris['data'], columns=iris['feature_names'])\n", "df.head()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "_GpYFu3Rq57_", "outputId": "c74fb4a2-53c2-4879-c964-daea9d2a9007" }, "execution_count": 14, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n", "0 5.1 3.5 1.4 0.2\n", "1 4.9 3.0 1.4 0.2\n", "2 4.7 3.2 1.3 0.2\n", "3 4.6 3.1 1.5 0.2\n", "4 5.0 3.6 1.4 0.2" ], "text/html": [ "\n", "
\n", " | sepal length (cm) | \n", "sepal width (cm) | \n", "petal length (cm) | \n", "petal width (cm) | \n", "
---|---|---|---|---|
0 | \n", "5.1 | \n", "3.5 | \n", "1.4 | \n", "0.2 | \n", "
1 | \n", "4.9 | \n", "3.0 | \n", "1.4 | \n", "0.2 | \n", "
2 | \n", "4.7 | \n", "3.2 | \n", "1.3 | \n", "0.2 | \n", "
3 | \n", "4.6 | \n", "3.1 | \n", "1.5 | \n", "0.2 | \n", "
4 | \n", "5.0 | \n", "3.6 | \n", "1.4 | \n", "0.2 | \n", "