You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

937 lines
79 KiB

2 years ago
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"___\n",
"\n",
"<a href='https://www.udemy.com/user/joseportilla/'><img src='../Pierian_Data_Logo.png'/></a>\n",
"___\n",
"<center><em>Copyright by Pierian Data Inc.</em></center>\n",
"<center><em>For more information, visit us at <a href='http://www.pieriandata.com'>www.pieriandata.com</a></em></center>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Support Vector Machines \n",
"## Exercise - Solutions\n",
"\n",
"## Fraud in Wine\n",
"\n",
"Wine fraud relates to the commercial aspects of wine. The most prevalent type of fraud is one where wines are adulterated, usually with the addition of cheaper products (e.g. juices) and sometimes with harmful chemicals and sweeteners (compensating for color or flavor).\n",
"\n",
"Counterfeiting and the relabelling of inferior and cheaper wines to more expensive brands is another common type of wine fraud.\n",
"\n",
"<img src=\"wine.jpg\">\n",
"\n",
"## Project Goals\n",
"\n",
"A distribution company that was recently a victim of fraud has completed an audit of various samples of wine through the use of chemical analysis on samples. The distribution company specializes in exporting extremely high quality, expensive wines, but was defrauded by a supplier who was attempting to pass off cheap, low quality wine as higher grade wine. The distribution company has hired you to attempt to create a machine learning model that can help detect low quality (a.k.a \"fraud\") wine samples. They want to know if it is even possible to detect such a difference.\n",
"\n",
"\n",
"Data Source: *P. Cortez, A. Cerdeira, F. Almeida, T. Matos and J. Reis. Modeling wine preferences by data mining from physicochemical properties.\n",
"In Decision Support Systems, Elsevier, 47(4):547-553, 2009.*\n",
"\n",
"---\n",
"---\n",
"\n",
"**TASK: Your overall goal is to use the wine dataset shown below to develop a machine learning model that attempts to predict if a wine is \"Legit\" or \"Fraud\" based on various chemical features. Complete the tasks below to follow along with the project.**\n",
"\n",
"---\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Complete the Tasks in bold\n",
"\n",
"**TASK: Run the cells below to import the libraries and load the dataset.**"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"../DATA/wine_fraud.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>fixed acidity</th>\n",
" <th>volatile acidity</th>\n",
" <th>citric acid</th>\n",
" <th>residual sugar</th>\n",
" <th>chlorides</th>\n",
" <th>free sulfur dioxide</th>\n",
" <th>total sulfur dioxide</th>\n",
" <th>density</th>\n",
" <th>pH</th>\n",
" <th>sulphates</th>\n",
" <th>alcohol</th>\n",
" <th>quality</th>\n",
" <th>type</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>7.4</td>\n",
" <td>0.70</td>\n",
" <td>0.00</td>\n",
" <td>1.9</td>\n",
" <td>0.076</td>\n",
" <td>11.0</td>\n",
" <td>34.0</td>\n",
" <td>0.9978</td>\n",
" <td>3.51</td>\n",
" <td>0.56</td>\n",
" <td>9.4</td>\n",
" <td>Legit</td>\n",
" <td>red</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>7.8</td>\n",
" <td>0.88</td>\n",
" <td>0.00</td>\n",
" <td>2.6</td>\n",
" <td>0.098</td>\n",
" <td>25.0</td>\n",
" <td>67.0</td>\n",
" <td>0.9968</td>\n",
" <td>3.20</td>\n",
" <td>0.68</td>\n",
" <td>9.8</td>\n",
" <td>Legit</td>\n",
" <td>red</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>7.8</td>\n",
" <td>0.76</td>\n",
" <td>0.04</td>\n",
" <td>2.3</td>\n",
" <td>0.092</td>\n",
" <td>15.0</td>\n",
" <td>54.0</td>\n",
" <td>0.9970</td>\n",
" <td>3.26</td>\n",
" <td>0.65</td>\n",
" <td>9.8</td>\n",
" <td>Legit</td>\n",
" <td>red</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>11.2</td>\n",
" <td>0.28</td>\n",
" <td>0.56</td>\n",
" <td>1.9</td>\n",
" <td>0.075</td>\n",
" <td>17.0</td>\n",
" <td>60.0</td>\n",
" <td>0.9980</td>\n",
" <td>3.16</td>\n",
" <td>0.58</td>\n",
" <td>9.8</td>\n",
" <td>Legit</td>\n",
" <td>red</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>7.4</td>\n",
" <td>0.70</td>\n",
" <td>0.00</td>\n",
" <td>1.9</td>\n",
" <td>0.076</td>\n",
" <td>11.0</td>\n",
" <td>34.0</td>\n",
" <td>0.9978</td>\n",
" <td>3.51</td>\n",
" <td>0.56</td>\n",
" <td>9.4</td>\n",
" <td>Legit</td>\n",
" <td>red</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" fixed acidity volatile acidity citric acid residual sugar chlorides \\\n",
"0 7.4 0.70 0.00 1.9 0.076 \n",
"1 7.8 0.88 0.00 2.6 0.098 \n",
"2 7.8 0.76 0.04 2.3 0.092 \n",
"3 11.2 0.28 0.56 1.9 0.075 \n",
"4 7.4 0.70 0.00 1.9 0.076 \n",
"\n",
" free sulfur dioxide total sulfur dioxide density pH sulphates \\\n",
"0 11.0 34.0 0.9978 3.51 0.56 \n",
"1 25.0 67.0 0.9968 3.20 0.68 \n",
"2 15.0 54.0 0.9970 3.26 0.65 \n",
"3 17.0 60.0 0.9980 3.16 0.58 \n",
"4 11.0 34.0 0.9978 3.51 0.56 \n",
"\n",
" alcohol quality type \n",
"0 9.4 Legit red \n",
"1 9.8 Legit red \n",
"2 9.8 Legit red \n",
"3 9.8 Legit red \n",
"4 9.4 Legit red "
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: What are the unique variables in the target column we are trying to predict (quality)?**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['Legit', 'Fraud'], dtype=object)"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['quality'].unique()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Create a countplot that displays the count per category of Legit vs Fraud. Is the label/target balanced or unbalanced?**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<AxesSubplot:xlabel='quality', ylabel='count'>"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEGCAYAAACUzrmNAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAT4ElEQVR4nO3df6zd9X3f8ecLQ4C08QqzYdSmM6u8doYFmG8ZTZQ0K1tx07VmaUgdLcFKkbwhFjXtuglWqfkxWYq0bBpkhc1LE+wtDXGTUdxsZGVeWdKGhFwTUjAEYQUCll18kyYNZCsd9L0/zsfjYB/fzzXcc+617/MhffX9ft/f7+d7Pgdd68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Q3mnJDnrcGAkOXsObc8Dtrc7mk4BdlbVZ5LcC+xMci3wJHA1QFXtTbITeJjBg3/XV9Xhn3C9DrgNOBO4q02SpAmZa1j8a+ALST7F4HrB24CtszWoqj9ixOijqr4FXHGMNltHHbeqpoHZrndIksZork9w70gyzeDlgQHeUlUPj7VnkqRFY64jC1o4GBCStAS9rFeUS5KWFsNCktRlWEiSugwLSVKXYSFJ6jIsJEldhoUkqcuwkCR1GRaSpC7DQpLUZVhIkroMC0lSl2EhSeoyLCRJXYaFJKnLsJAkdRkWkqQuw0KS1GVYSJK6DAtJUpdhIUnqMiwkSV1jC4sk5yf5/SSPJNmb5Jda/ewkdyd5rM3PGmpzY5J9SR5NcuVQfX2SB9u2m5NkXP2WJB1tnCOL54F/WlV/A7gcuD7JOuAGYHdVrQV2t3Xatk3AhcAG4JYky9qxbgW2AGvbtGGM/ZYkHWFsYVFVB6vq/rb8DPAIsArYCGxvu20HrmrLG4Hbq+q5qnoc2AdcluQ8YHl
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"sns.countplot(x='quality',data=df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Let's find out if there is a difference between red and white wine when it comes to fraud. Create a countplot that has the wine *type* on the x axis with the hue separating columns by Fraud vs Legit.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<AxesSubplot:xlabel='type', ylabel='count'>"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEGCAYAAACUzrmNAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAA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pk2b1u+jCjAsJGmHjBs3jjPPPJP29nZOO+00Jk6cCMAll1zCNddcw7HHHsvq1as39T///POZM2cOEyZM4He/+x3Dhg3b5vE/+9nPcvvtt2+a4AY466yzePnll5k2bVr9vthWRGb2+4f2h46OjvTHj7Sz8tLZt72bS2eXL1/OmDFjalLHlVde+Y4J6XqYP38+d955JzfddFNNjtfb94+Irszs2LyvcxaSNAB8/etfZ8GCBdx9990N+XzDQpJq4Morr6zr8WfPnl3X45dxzkKSVMqwkCSVMiwkSaUMC0lSKSe4JalQ60uSyy7rHT58OK+++uq7+owVK1Zw0UUXMX/+fJYsWcKKFSv49Kc//a6O2RtHFpI0gB144IGb7hRfsmRJ3S6tNSwkqYk8++yzTJ48mfHjxzNx4kR++9vfbmqfMGECH/rQh7j88ssZPnw48PaPJ7355ptcfvnl3HLLLbS3t3PLLbfUtC7DQpKayIwZM5g9ezZdXV3MmjWL888/H4CLL76Yiy++mEcffZQDDzxwi/3e85738N3vfpczzzyTJUuWcOaZZ9a0LucsJKlJvPrqqzz00EOcccYZm9reeOMNABYtWsQdd9wBwOc///m6LivSG8NCkprEW2+9xZ577smSJUsaXcoWPA0lSU1ixIgRjB49mp///OcAZCaPPfYYABMmTODWW28FYO7cub3u39uy5rXiyEKSCu9mBdsd8ac//YnW1tZNr7/5zW9y8803c9555/G9732PdevWMXXqVI466ih++MMfcvbZZ/ODH/yAKVOmsMcee2xxvI9//ONcddVVtLe3c9lll9V03sKwkKQG2fiLepu75557tmgbNWoUDz/8MBHB3Llz6eiorCLe1tbGE088AcDee+/No48+WpdaDQtJGgC6urq48MILyUz23HNPrrvuun79fMNCkgaAiRMnbpq/aAQnuCVJpQwLSVIpw0KSVKruYRERQyJicUT8sni9d0TcGxFPF897VfW9LCKeiYinIuJTVe3jI+Lx4r0fR0TUu25J0tv6Y4L7YmA5MKJ4fSmwMDOviohLi9ffjojDganAWOBA4B8i4t9l5gbgGmAG8DBwNzAZWNAPtUsaRJ7/7pE1Pd7Blz++zfe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38ZfyJA1ara2t9PT0MBiXB2ppaaG1tbW8Y8GwkDRoDR06lNGjRze6jAHB01CSpFKGhSSplGEhSSplWEiSShkWkqRShoUkqZRhIUkqZVhIkkoZFpKkUoaFJKmUYSFJKmVYSJJKGRaSpFKGhSSplGEhSSplWEiSShkWkqRShoUkqZRhIUkqZVhIkkoZFpKkUoaFJKmUYSFJKmVYSJJKGRaSpFKGhSSplGEhSSplWEiSStUtLCKiJSIeiYjHIuLJiPhO0b53RNwbEU8Xz3tV7XNZRDwTEU9FxKeq2sdHxOPFez+OiKhX3ZKkLdVzZPEG8GeZeRTQDkyOiAnApcDCzDwUWFi8JiIOB6YCY4HJwE8jYkhxrGuAGcChxWNyHeuWJG2mbmGRFa8WL4cWjwROBuYU7XOAU4rtk4G5mflGZj4HPAMcExEHACMyc1FmJnBj1T6SpH5Q1zmLiBgSEUuAl4B7M/NfgP0zcyVA8bxf0X0U8ELV7j1F26hie/P23j5vRkR0RkTnqlWravpdJGkwq2tYZOaGzGwHWqmMEo7YRvfe5iFyG+29fd61mdmRmR0jR47c7no
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"sns.countplot(x='type',hue='quality',data=df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: What percentage of red wines are Fraud? What percentage of white wines are fraud?**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"reds = df[df[\"type\"]=='red']"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"whites = df[df[\"type\"]=='white']"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Percentage of fraud in Red Wines:\n",
"3.9399624765478425\n"
]
}
],
"source": [
"print(\"Percentage of fraud in Red Wines:\")\n",
"print(100* (len(reds[reds['quality']=='Fraud'])/len(reds)))"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Percentage of fraud in White Wines:\n",
"3.7362188648427925\n"
]
}
],
"source": [
"print(\"Percentage of fraud in White Wines:\")\n",
"print(100* (len(whites[whites['quality']=='Fraud'])/len(whites)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Calculate the correlation between the various features and the \"quality\" column. To do this you may need to map the column to 0 and 1 instead of a string.**"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [],
"source": [
"df['Fraud']= df['quality'].map({'Legit':0,'Fraud':1})"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"fixed acidity 0.021794\n",
"volatile acidity 0.151228\n",
"citric acid -0.061789\n",
"residual sugar -0.048756\n",
"chlorides 0.034499\n",
"free sulfur dioxide -0.085204\n",
"total sulfur dioxide -0.035252\n",
"density 0.016351\n",
"pH 0.020107\n",
"sulphates -0.034046\n",
"alcohol -0.051141\n",
"Fraud 1.000000\n",
"Name: Fraud, dtype: float64"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.corr()['Fraud']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Create a bar plot of the correlation values to Fraudlent wine.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# CODE HERE"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<AxesSubplot:>"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAFMCAYAAADGGLtNAAAAOXRFWHRTb2Z0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Zc6fiJlvnXcEkr4s6buTfE31U70m2efm++nALsBewM3Au9ZSjhMlLZK0aMWKFVMMHRER67LOOwLbj1/Ta5J+IemBzd3AA4Hlk7xtGbDD0Pb2wE3N7/7F0O/6MPCFtZTjTOBMgAULFnhN76tRPpVHxHRM9xnBBcBzm5+fC/zHJO9ZCMyXNE/SHODY5jia5DHwdOC70yxPRESsp+k+I3g7cJ6kvwF+ChwNIOlBwEdsH277LkknA5cAs4GzbH+vOf5USXtRmopuAF44zfJERMR6mlYisP1L4HGT7L8JOHxo+yJgta6htp8znfgRETF9GVkcEVG5JIKIiMolEUREVC6JICKictPtNRRD0p8/Imai3BFERFQuiSAionJJBBERlUsiiIioXBJBRETlkggiIiqXRBARUbkkgoiIyiURRERULokgIqJySQQREZVLIoiIqFwSQURE5ZIIIiIql0QQEVG5JIKIiMolEUREVC6JICKickkEERGVG8s1i7N2cETE1OWOICKickkEERGVSyKIiKhcEkFEROWSCCIiKpdEEBFRuSSCiIjKJRFERFQuiSAionKy3XcZ1pukFcBPNvDwbYBbRlicmRC7trh9xs451xF7pp7zg23PnbhzRiaC6ZC0yPaCmmLXFrfP2DnnOmKP2zmnaSgionJJBBERlasxEZxZYeza4vY
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"df.corr()['Fraud'][:-1].sort_values().plot(kind='bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Create a clustermap with seaborn to explore the relationships between variables.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<seaborn.matrix.ClusterGrid at 0x231b34be088>"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsgAAALJCAYAAACp99XTAAAAOXRFWHRTb2Z0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q7Lyd7sM6s+z6y+OfWnfEUbKYL9zjRWLhvXL7GRb/nrjviNM66Zt+k4w2j2+ekPfEUb6xcuH/Y9j0x9v0XeEkWpGZwbNvu3PuKnvCNO68j6b9R1htc5/zyFr3Vs0u2b2T3HlymF9rJvjgRe2j18IfGnyBkm2TLL1xGPgscCF6/rCkiRJGq4ZzkFesYFi9OII4LNJXgL8CngWQJIdgI9V1ROAuwBfSALN9+r/VdWJPeWVJEnSLJhhQR7227YzUVVXAI+eYvklwBPaxz8Hdp/laJIkSerRQr7MmyRJknQbC3mKhSRJknQbMyvIM7jihSRJkjQXLdg5yJIkSdJUZlSQyykWkiRJmuecgyxJkiR1OIIsSZIkdTgHWZIkSepwBFmSJEnqsCBLkiRJHTMqyF9b8ZlsqCCSJEnSEGzUdwBJkiRpSCzIkiRJA5XkpUle0D4+OMkOI7Z9W5L9N3SOScsXJ7lwQ7xmn2Z2FQtJkiTNmqr6SOfpwcCFwCWTt0uycVW9ZZZyzHuOIEuSJA1AkhckOT/JeUmOaZctTXJokmcCY8CnkpybZPMkFyd5S5JvA89KclS7HUn2TvKd9lg/SLL1pNfaKskpSc5OckGSp6xJjvbxXu267wKvmJ3vzuxyBFmSJKlnSe4LvAl4WFVdnuSO3fVVdVySVwKHVtV4uw/A8qrap33++PbzZsBngAOr6swktwdumPSSy4GnVdUfk2wHfC/J8cB9RuVoHQn8fVWdluRd6+c7MCxzsiCPjY0dBizqO8dc94MT+k4gSdLCkWQJsKSzaFlVLWs
"text/plain": [
"<Figure size 720x720 with 4 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"sns.clustermap(df.corr(),cmap='viridis')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"## Machine Learning Model\n",
"\n",
"**TASK: Convert the categorical column \"type\" from a string or \"red\" or \"white\" to dummy variables:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# CODE HERE"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
"df['type'] = pd.get_dummies(df['type'],drop_first=True)"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
"df = df.drop('Fraud',axis=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Separate out the data into X features and y target label (\"quality\" column)**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"X = df.drop('quality',axis=1)\n",
"y = df['quality']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Perform a Train|Test split on the data, with a 10% test size. Note: The solution uses a random state of 101**"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=101)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Scale the X train and X test data.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.preprocessing import StandardScaler"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"scaler = StandardScaler()"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [],
"source": [
"scaled_X_train = scaler.fit_transform(X_train)\n",
"scaled_X_test = scaler.transform(X_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Create an instance of a Support Vector Machine classifier. Previously we have left this model \"blank\", (e.g. with no parameters). However, we already know that the classes are unbalanced, in an attempt to help alleviate this issue, we can automatically adjust weights inversely proportional to class frequencies in the input data with a argument call in the SVC() call. Check out the [documentation for SVC] online and look up what the argument\\parameter is.**"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
"# CODE HERE"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.svm import SVC"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [],
"source": [
"svc = SVC(class_weight='balanced')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Use a GridSearchCV to run a grid search for the best C and gamma parameters.**"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [],
"source": [
"# CODE HERE"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import GridSearchCV"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"param_grid = {'C':[0.001,0.01,0.1,0.5,1],'gamma':['scale','auto']}\n",
"grid = GridSearchCV(svc,param_grid)"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"GridSearchCV(estimator=SVC(class_weight='balanced'),\n",
" param_grid={'C': [0.001, 0.01, 0.1, 0.5, 1],\n",
" 'gamma': ['scale', 'auto']})"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grid.fit(scaled_X_train,y_train)"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'C': 1, 'gamma': 'auto'}"
]
},
"execution_count": 143,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grid.best_params_"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Display the confusion matrix and classification report for your model.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import confusion_matrix,classification_report"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [],
"source": [
"grid_pred = grid.predict(scaled_X_test)"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 17, 10],\n",
" [ 92, 531]], dtype=int64)"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"confusion_matrix(y_test,grid_pred)"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
" Fraud 0.16 0.63 0.25 27\n",
" Legit 0.98 0.85 0.91 623\n",
"\n",
" accuracy 0.84 650\n",
" macro avg 0.57 0.74 0.58 650\n",
"weighted avg 0.95 0.84 0.88 650\n",
"\n"
]
}
],
"source": [
"print(classification_report(y_test,grid_pred))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**TASK: Finally, think about how well this model performed, would you suggest using it? Realistically will this work?**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# View video for full discussion on this."
]
}
],
"metadata": {
"anaconda-cloud": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}