From bd26ba2f2248bd2bc76e01eb07e02c1ec0d044ec Mon Sep 17 00:00:00 2001 From: Silvia Gonzalez <80603632+silviagonzalez98@users.noreply.github.com> Date: Fri, 15 Oct 2021 19:48:49 +0200 Subject: [PATCH 1/3] Add files via upload --- Solutions (11).ipynb | 177 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 Solutions (11).ipynb diff --git a/Solutions (11).ipynb b/Solutions (11).ipynb new file mode 100644 index 0000000..bb306e9 --- /dev/null +++ b/Solutions (11).ipynb @@ -0,0 +1,177 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "438bf20f", + "metadata": {}, + "source": [ + "# Lab | Random Forests" + ] + }, + { + "cell_type": "markdown", + "id": "c2bbe11c", + "metadata": {}, + "source": [ + "- For this lab, you will be using the CSV files provided in the files_for_lab folder." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "b3286904", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "pd.set_option('display.max_columns', None)\n", + "import warnings\n", + "warnings.filterwarnings('ignore')\n", + "from sklearn.preprocessing import OneHotEncoder\n", + "from sklearn.model_selection import train_test_split\n", + "from imblearn.over_sampling import SMOTE\n", + "from sklearn.ensemble import RandomForestClassifier\n", + "from sklearn.model_selection import cross_val_score" + ] + }, + { + "cell_type": "markdown", + "id": "40546e90", + "metadata": {}, + "source": [ + "### 1. Apply the Random Forests algorithm but this time only by upscaling the data using SMOTE. Note that since SMOTE works on numerical data only, we will first encode the categorical variables in this case." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "64274b33", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 90569\n", + "1 4843\n", + "Name: TARGET_B, dtype: int64" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numerical = pd.read_csv('./files_for_lab/numerical.csv')\n", + "categorical = pd.read_csv('./files_for_lab/categorical.csv')\n", + "targets = pd.read_csv('./files_for_lab/target.csv')\n", + "data = pd.concat([numerical, categorical, targets], axis = 1)\n", + "data['TARGET_B'].value_counts()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6f71a011", + "metadata": {}, + "outputs": [], + "source": [ + "y = data['TARGET_B']\n", + "X = data.drop(['TARGET_B'], axis = 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2c120e30", + "metadata": {}, + "outputs": [], + "source": [ + "numericalX = X.select_dtypes(np.number)\n", + "categorcalX = X.select_dtypes(np.object)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "86bdc530", + "metadata": {}, + "outputs": [], + "source": [ + "encoder = OneHotEncoder(drop='first').fit(categorcalX)\n", + "encoded_categorical = encoder.transform(categorcalX).toarray()\n", + "encoded_categorical = pd.DataFrame(encoded_categorical)\n", + "X = pd.concat([numericalX, encoded_categorical], axis = 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fe7eb394", + "metadata": {}, + "outputs": [], + "source": [ + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "af3ca41b", + "metadata": {}, + "outputs": [], + "source": [ + "smote = SMOTE()\n", + "\n", + "X_train_sm, y_train_sm = smote.fit_resample(X_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "a2161028", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.9474279964784303\n", + "0.9498455836369397\n" + ] + } + ], + "source": [ + "clf = RandomForestClassifier(max_depth=2, random_state=0)\n", + "clf.fit(X_train, y_train)\n", + "print(clf.score(X_test, y_test))\n", + "\n", + "clf = RandomForestClassifier(max_depth=2, random_state=0)\n", + "cross_val_scores = cross_val_score(clf, X_train, y_train, cv=10)\n", + "print(np.mean(cross_val_scores))" + ] + } + ], + "metadata": { + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 19a8125cf15db3de9bb7d64213c1fc575d94e64a Mon Sep 17 00:00:00 2001 From: Silvia Gonzalez <80603632+silviagonzalez98@users.noreply.github.com> Date: Fri, 15 Oct 2021 19:48:59 +0200 Subject: [PATCH 2/3] Delete Solutions.ipynb --- Solutions.ipynb | 73 ------------------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 Solutions.ipynb diff --git a/Solutions.ipynb b/Solutions.ipynb deleted file mode 100644 index 71f3b3f..0000000 --- a/Solutions.ipynb +++ /dev/null @@ -1,73 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "438bf20f", - "metadata": {}, - "source": [ - "# Lab | Random Forests" - ] - }, - { - "cell_type": "markdown", - "id": "c2bbe11c", - "metadata": {}, - "source": [ - "- For this lab, you will be using the CSV files provided in the files_for_lab folder." - ] - }, - { - "cell_type": "markdown", - "id": "40546e90", - "metadata": {}, - "source": [ - "### 1. Apply the Random Forests algorithm but this time only by upscaling the data using SMOTE." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "64274b33", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "9100309b", - "metadata": {}, - "source": [ - "### 2. Note that since SMOTE works on numerical data only, we will first encode the categorical variables in this case." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0fedb9b1", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "ironhac", - "language": "python", - "name": "ironhac" - }, - "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.2" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 84357a963a60f9355c444f7b3561d700d1b72b64 Mon Sep 17 00:00:00 2001 From: Silvia Gonzalez <80603632+silviagonzalez98@users.noreply.github.com> Date: Fri, 15 Oct 2021 19:49:10 +0200 Subject: [PATCH 3/3] Rename Solutions (11).ipynb to Solutions.ipynb --- Solutions (11).ipynb => Solutions.ipynb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Solutions (11).ipynb => Solutions.ipynb (100%) diff --git a/Solutions (11).ipynb b/Solutions.ipynb similarity index 100% rename from Solutions (11).ipynb rename to Solutions.ipynb