Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 116 additions & 12 deletions Solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,148 @@
"- 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."
"### 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": null,
"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": []
"source": [
"y = data['TARGET_B']\n",
"X = data.drop(['TARGET_B'], axis = 1)"
]
},
{
"cell_type": "markdown",
"id": "9100309b",
"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": [
"### 2. Note that since SMOTE works on numerical data only, we will first encode the categorical variables in this case."
"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": null,
"id": "0fedb9b1",
"execution_count": 7,
"id": "af3ca41b",
"metadata": {},
"outputs": [],
"source": []
"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": "ironhac",
"display_name": "Python 3",
"language": "python",
"name": "ironhac"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -65,7 +169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
"version": "3.8.8"
}
},
"nbformat": 4,
Expand Down