{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import all libraries and dependencies\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import matplotlib.ticker as ticker\n",
    "import matplotlib.ticker as plticker\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.datasets import make_classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [],
   "source": [
    "#load data \n",
    "world_cup = pd.read_csv('datasets/World Cup 2019 Dataset.csv')\n",
    "results = pd.read_csv('datasets/results.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "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>Team</th>\n",
       "      <th>Group</th>\n",
       "      <th>Previous \r\n",
       "appearances</th>\n",
       "      <th>Previous \r\n",
       "titles</th>\n",
       "      <th>Previous\r\n",
       " finals</th>\n",
       "      <th>Previous\r\n",
       " semifinals</th>\n",
       "      <th>Current \r",
       " rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>England</td>\n",
       "      <td>A</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>South Africa</td>\n",
       "      <td>A</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>West Indies</td>\n",
       "      <td>A</td>\n",
       "      <td>11</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Pakistan</td>\n",
       "      <td>A</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>New Zealand</td>\n",
       "      <td>A</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Team Group  Previous \\r\\nappearances  Previous \\r\\ntitles  \\\n",
       "0       England     A                        11                    0   \n",
       "1  South Africa     A                         6                    0   \n",
       "2   West Indies     A                        11                    2   \n",
       "3      Pakistan     A                        11                    1   \n",
       "4   New Zealand     A                        11                    0   \n",
       "\n",
       "   Previous\\r\\n finals  Previous\\r\\n semifinals  Current \\r rank  \n",
       "0                    3                        5                1  \n",
       "1                    0                        4                3  \n",
       "2                    3                        4                8  \n",
       "3                    2                        6                6  \n",
       "4                    1                        7                4  "
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "world_cup.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "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>date</th>\n",
       "      <th>Team_1</th>\n",
       "      <th>Team_2</th>\n",
       "      <th>Winner</th>\n",
       "      <th>Margin</th>\n",
       "      <th>Ground</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>7 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5-Jan-10</td>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>5 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>India</td>\n",
       "      <td>India</td>\n",
       "      <td>6 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>9 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10-Jan-10</td>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>India</td>\n",
       "      <td>8 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        date      Team_1     Team_2     Winner     Margin Ground\n",
       "0   4-Jan-10  Bangladesh  Sri Lanka  Sri Lanka  7 wickets  Dhaka\n",
       "1   5-Jan-10       India  Sri Lanka  Sri Lanka  5 wickets  Dhaka\n",
       "2   7-Jan-10  Bangladesh      India      India  6 wickets  Dhaka\n",
       "3   8-Jan-10  Bangladesh  Sri Lanka  Sri Lanka  9 wickets  Dhaka\n",
       "4  10-Jan-10       India  Sri Lanka      India  8 wickets  Dhaka"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "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>date</th>\n",
       "      <th>Team_1</th>\n",
       "      <th>Team_2</th>\n",
       "      <th>Winner</th>\n",
       "      <th>Margin</th>\n",
       "      <th>Ground</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5-Jan-10</td>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>5 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>India</td>\n",
       "      <td>India</td>\n",
       "      <td>6 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10-Jan-10</td>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>India</td>\n",
       "      <td>8 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>11-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>India</td>\n",
       "      <td>India</td>\n",
       "      <td>6 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13-Jan-10</td>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>4 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        date      Team_1     Team_2     Winner     Margin Ground\n",
       "1   5-Jan-10       India  Sri Lanka  Sri Lanka  5 wickets  Dhaka\n",
       "2   7-Jan-10  Bangladesh      India      India  6 wickets  Dhaka\n",
       "4  10-Jan-10       India  Sri Lanka      India  8 wickets  Dhaka\n",
       "5  11-Jan-10  Bangladesh      India      India  6 wickets  Dhaka\n",
       "6  13-Jan-10       India  Sri Lanka  Sri Lanka  4 wickets  Dhaka"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = results[(results['Team_1'] == 'India') | (results['Team_2'] == 'India')]\n",
    "india = df.iloc[:]\n",
    "india.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date          151\n",
       "Team_1        151\n",
       "Team_2        151\n",
       "Winner        151\n",
       "Margin        142\n",
       "Ground        151\n",
       "match_year    151\n",
       "dtype: int64"
      ]
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#creating a column for matches played in 2010\n",
    "year = []\n",
    "for row in india['date']:\n",
    "    year.append(int(row[7:]))\n",
    "india ['match_year']= year\n",
    "india_2010 = india[india.match_year >= 10]\n",
    "india_2010.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "date      1445\n",
       "Team_1    1445\n",
       "Team_2    1445\n",
       "Winner    1445\n",
       "Margin    1368\n",
       "Ground    1445\n",
       "dtype: int64"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#narrowing to team patcipating in the world cup\n",
    "worldcup_teams = ['England', ' South Africa', '', 'West Indies', \n",
    "            'Pakistan', 'New Zealand', 'Sri Lanka', 'Afghanistan', \n",
    "            'Australia', 'Bangladesh', 'India']\n",
    "df_teams_1 = results[results['Team_1'].isin(worldcup_teams)]\n",
    "df_teams_2 = results[results['Team_2'].isin(worldcup_teams)]\n",
    "df_teams = pd.concat((df_teams_1, df_teams_2))\n",
    "df_teams.drop_duplicates()\n",
    "df_teams.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "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>date</th>\n",
       "      <th>Team_1</th>\n",
       "      <th>Team_2</th>\n",
       "      <th>Winner</th>\n",
       "      <th>Margin</th>\n",
       "      <th>Ground</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>7 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5-Jan-10</td>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>5 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>India</td>\n",
       "      <td>India</td>\n",
       "      <td>6 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8-Jan-10</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>9 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10-Jan-10</td>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>India</td>\n",
       "      <td>8 wickets</td>\n",
       "      <td>Dhaka</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        date      Team_1     Team_2     Winner     Margin Ground\n",
       "0   4-Jan-10  Bangladesh  Sri Lanka  Sri Lanka  7 wickets  Dhaka\n",
       "1   5-Jan-10       India  Sri Lanka  Sri Lanka  5 wickets  Dhaka\n",
       "2   7-Jan-10  Bangladesh      India      India  6 wickets  Dhaka\n",
       "3   8-Jan-10  Bangladesh  Sri Lanka  Sri Lanka  9 wickets  Dhaka\n",
       "4  10-Jan-10       India  Sri Lanka      India  8 wickets  Dhaka"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_teams.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "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>Team_1</th>\n",
       "      <th>Team_2</th>\n",
       "      <th>Winner</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>India</td>\n",
       "      <td>India</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>India</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Team_1     Team_2     Winner\n",
       "0  Bangladesh  Sri Lanka  Sri Lanka\n",
       "1       India  Sri Lanka  Sri Lanka\n",
       "2  Bangladesh      India      India\n",
       "3  Bangladesh  Sri Lanka  Sri Lanka\n",
       "4       India  Sri Lanka      India"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#dropping columns that wll not affect match outcomes\n",
    "df_teams_2010 = df_teams.drop(['date','Margin', 'Ground'], axis=1)\n",
    "df_teams_2010.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "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>Team_1</th>\n",
       "      <th>Team_2</th>\n",
       "      <th>Winner</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>India</td>\n",
       "      <td>India</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>Sri Lanka</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>India</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>India</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Team_1     Team_2     Winner\n",
       "0  Bangladesh  Sri Lanka  Sri Lanka\n",
       "1       India  Sri Lanka  Sri Lanka\n",
       "2  Bangladesh      India      India\n",
       "3  Bangladesh  Sri Lanka  Sri Lanka\n",
       "4       India  Sri Lanka      India"
      ]
     },
     "execution_count": 194,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Building the model\n",
    "#the prediction label: The winning_team column will show \"1\" Team 1 has won and \"2\" if the away team has won.\n",
    "\n",
    "df_teams_2010 = df_teams_2010.reset_index(drop=True)\n",
    "df_teams_2010.loc[df_teams_2010.Winner == df_teams_2010.Team_1,'winning_team']=1\n",
    "df_teams_2010.loc[df_teams_2010.Winner == df_teams_2010.Team_2, 'winning_team']=2\n",
    "df_teams_2010 = df_teams_2010.drop(['winning_team'], axis=1)\n",
    "\n",
    "df_teams_2010.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {},
   "outputs": [],
   "source": [
    "#convert team-1 and team-2 from categorical variables to continous inputs \n",
    "# Get dummy variables\n",
    "final = pd.get_dummies(df_teams_2010, prefix=['Team_1', 'Team_2'], columns=['Team_1', 'Team_2'])\n",
    "\n",
    "# Separate X and y sets\n",
    "X = final.drop(['Winner'], axis=1)\n",
    "y = final[\"Winner\"]\n",
    "\n",
    "\n",
    "# Separate train and test sets\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "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>Winner</th>\n",
       "      <th>Team_1_Afghanistan</th>\n",
       "      <th>Team_1_Australia</th>\n",
       "      <th>Team_1_Bangladesh</th>\n",
       "      <th>Team_1_Canada</th>\n",
       "      <th>Team_1_England</th>\n",
       "      <th>Team_1_India</th>\n",
       "      <th>Team_1_Ireland</th>\n",
       "      <th>Team_1_Kenya</th>\n",
       "      <th>Team_1_Netherlands</th>\n",
       "      <th>...</th>\n",
       "      <th>Team_2_Kenya</th>\n",
       "      <th>Team_2_Netherlands</th>\n",
       "      <th>Team_2_New Zealand</th>\n",
       "      <th>Team_2_Pakistan</th>\n",
       "      <th>Team_2_Scotland</th>\n",
       "      <th>Team_2_South Africa</th>\n",
       "      <th>Team_2_Sri Lanka</th>\n",
       "      <th>Team_2_U.A.E.</th>\n",
       "      <th>Team_2_West Indies</th>\n",
       "      <th>Team_2_Zimbabwe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>India</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>India</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Winner  Team_1_Afghanistan  Team_1_Australia  Team_1_Bangladesh  \\\n",
       "0  Sri Lanka                   0                 0                  1   \n",
       "1  Sri Lanka                   0                 0                  0   \n",
       "2      India                   0                 0                  1   \n",
       "3  Sri Lanka                   0                 0                  1   \n",
       "4      India                   0                 0                  0   \n",
       "\n",
       "   Team_1_Canada  Team_1_England  Team_1_India  Team_1_Ireland  Team_1_Kenya  \\\n",
       "0              0               0             0               0             0   \n",
       "1              0               0             1               0             0   \n",
       "2              0               0             0               0             0   \n",
       "3              0               0             0               0             0   \n",
       "4              0               0             1               0             0   \n",
       "\n",
       "   Team_1_Netherlands       ...         Team_2_Kenya  Team_2_Netherlands  \\\n",
       "0                   0       ...                    0                   0   \n",
       "1                   0       ...                    0                   0   \n",
       "2                   0       ...                    0                   0   \n",
       "3                   0       ...                    0                   0   \n",
       "4                   0       ...                    0                   0   \n",
       "\n",
       "   Team_2_New Zealand  Team_2_Pakistan  Team_2_Scotland  Team_2_South Africa  \\\n",
       "0                   0                0                0                    0   \n",
       "1                   0                0                0                    0   \n",
       "2                   0                0                0                    0   \n",
       "3                   0                0                0                    0   \n",
       "4                   0                0                0                    0   \n",
       "\n",
       "   Team_2_Sri Lanka  Team_2_U.A.E.  Team_2_West Indies  Team_2_Zimbabwe  \n",
       "0                 1              0                   0                0  \n",
       "1                 1              0                   0                0  \n",
       "2                 0              0                   0                0  \n",
       "3                 1              0                   0                0  \n",
       "4                 1              0                   0                0  \n",
       "\n",
       "[5 rows x 36 columns]"
      ]
     },
     "execution_count": 196,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training set accuracy:  0.700\n",
      "Test set accuracy:  0.675\n"
     ]
    }
   ],
   "source": [
    "\n",
    "rf = RandomForestClassifier(n_estimators=100, max_depth=20,\n",
    "                              random_state=0)\n",
    "rf.fit(X_train, y_train) \n",
    "\n",
    "\n",
    "score = rf.score(X_train, y_train)\n",
    "score2 = rf.score(X_test, y_test)\n",
    "\n",
    "\n",
    "print(\"Training set accuracy: \", '%.3f'%(score))\n",
    "print(\"Test set accuracy: \", '%.3f'%(score2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [],
   "source": [
    "#adding ICC rankings\n",
    "#the team which is positioned higher on the ICC Ranking will be considered \"favourite\" for the match\n",
    "#and therefore, will be positioned under the \"Team_1\" column\n",
    "\n",
    "# Loading new datasets\n",
    "ranking = pd.read_csv('datasets/icc_rankings.csv') \n",
    "fixtures = pd.read_csv('datasets/fixtures.csv')\n",
    "\n",
    "# List for storing the group stage games\n",
    "pred_set = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "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>Round Number</th>\n",
       "      <th>first_position</th>\n",
       "      <th>second_position</th>\n",
       "      <th>Date</th>\n",
       "      <th>Location</th>\n",
       "      <th>Team_1</th>\n",
       "      <th>Team_2</th>\n",
       "      <th>Group</th>\n",
       "      <th>Result</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3/7/2019</td>\n",
       "      <td>Riverside Ground, Chester-le-Street</td>\n",
       "      <td>England</td>\n",
       "      <td>New Zealand</td>\n",
       "      <td>Group A</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>4/7/2019</td>\n",
       "      <td>Headingley, Leeds</td>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>West Indies</td>\n",
       "      <td>Group A</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5/7/2019</td>\n",
       "      <td>Lord's, London</td>\n",
       "      <td>Pakistan</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>Group A</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>1</td>\n",
       "      <td>9.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6/7/2019</td>\n",
       "      <td>Headingley, Leeds</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>India</td>\n",
       "      <td>Group A</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6/7/2019</td>\n",
       "      <td>Emirates Old Trafford, Manchester</td>\n",
       "      <td>Australia</td>\n",
       "      <td>South Africa</td>\n",
       "      <td>Group A</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Round Number  first_position  second_position      Date  \\\n",
       "40            1             1.0              4.0  3/7/2019   \n",
       "41            1            10.0              8.0  4/7/2019   \n",
       "42            1             6.0              7.0  5/7/2019   \n",
       "43            1             9.0              2.0  6/7/2019   \n",
       "44            1             5.0              3.0  6/7/2019   \n",
       "\n",
       "                               Location       Team_1        Team_2   Group   \\\n",
       "40  Riverside Ground, Chester-le-Street      England   New Zealand  Group A   \n",
       "41                    Headingley, Leeds  Afghanistan   West Indies  Group A   \n",
       "42                       Lord's, London     Pakistan    Bangladesh  Group A   \n",
       "43                    Headingley, Leeds    Sri Lanka         India  Group A   \n",
       "44    Emirates Old Trafford, Manchester    Australia  South Africa  Group A   \n",
       "\n",
       "    Result  \n",
       "40     NaN  \n",
       "41     NaN  \n",
       "42     NaN  \n",
       "43     NaN  \n",
       "44     NaN  "
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create new columns with ranking position of each team\n",
    "fixtures.insert(1, 'first_position', fixtures['Team_1'].map(ranking.set_index('Team')['Position']))\n",
    "fixtures.insert(2, 'second_position', fixtures['Team_2'].map(ranking.set_index('Team')['Position']))\n",
    "\n",
    "# We only need the group stage games, so we have to slice the dataset\n",
    "fixtures = fixtures.iloc[:45, :]\n",
    "fixtures.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "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>Team_1</th>\n",
       "      <th>Team_2</th>\n",
       "      <th>winning_team</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>England</td>\n",
       "      <td>South Africa</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pakistan</td>\n",
       "      <td>West Indies</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>New Zealand</td>\n",
       "      <td>Sri Lanka</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Australia</td>\n",
       "      <td>Afghanistan</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>South Africa</td>\n",
       "      <td>Bangladesh</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Team_1        Team_2 winning_team\n",
       "0       England  South Africa         None\n",
       "1      Pakistan   West Indies         None\n",
       "2   New Zealand     Sri Lanka         None\n",
       "3     Australia   Afghanistan         None\n",
       "4  South Africa    Bangladesh         None"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Loop to add teams to new prediction dataset based on the ranking position of each team\n",
    "for index, row in fixtures.iterrows():\n",
    "    if row['first_position'] < row['second_position']:\n",
    "        pred_set.append({'Team_1': row['Team_1'], 'Team_2': row['Team_2'], 'winning_team': None})\n",
    "    else:\n",
    "        pred_set.append({'Team_1': row['Team_2'], 'Team_2': row['Team_1'], 'winning_team': None})\n",
    "        \n",
    "pred_set = pd.DataFrame(pred_set)\n",
    "backup_pred_set = pred_set\n",
    "pred_set.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "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>Team_1_Afghanistan</th>\n",
       "      <th>Team_1_Australia</th>\n",
       "      <th>Team_1_Bangladesh</th>\n",
       "      <th>Team_1_Canada</th>\n",
       "      <th>Team_1_England</th>\n",
       "      <th>Team_1_India</th>\n",
       "      <th>Team_1_Ireland</th>\n",
       "      <th>Team_1_Kenya</th>\n",
       "      <th>Team_1_Netherlands</th>\n",
       "      <th>Team_1_New Zealand</th>\n",
       "      <th>...</th>\n",
       "      <th>Team_2_Kenya</th>\n",
       "      <th>Team_2_Netherlands</th>\n",
       "      <th>Team_2_New Zealand</th>\n",
       "      <th>Team_2_Pakistan</th>\n",
       "      <th>Team_2_Scotland</th>\n",
       "      <th>Team_2_South Africa</th>\n",
       "      <th>Team_2_Sri Lanka</th>\n",
       "      <th>Team_2_U.A.E.</th>\n",
       "      <th>Team_2_West Indies</th>\n",
       "      <th>Team_2_Zimbabwe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Team_1_Afghanistan  Team_1_Australia  Team_1_Bangladesh  Team_1_Canada  \\\n",
       "0                   0                 0                  0              0   \n",
       "1                   0                 0                  0              0   \n",
       "2                   0                 0                  0              0   \n",
       "3                   0                 1                  0              0   \n",
       "4                   0                 0                  0              0   \n",
       "\n",
       "   Team_1_England  Team_1_India  Team_1_Ireland  Team_1_Kenya  \\\n",
       "0               1             0               0             0   \n",
       "1               0             0               0             0   \n",
       "2               0             0               0             0   \n",
       "3               0             0               0             0   \n",
       "4               0             0               0             0   \n",
       "\n",
       "   Team_1_Netherlands  Team_1_New Zealand       ...         Team_2_Kenya  \\\n",
       "0                   0                   0       ...                    0   \n",
       "1                   0                   0       ...                    0   \n",
       "2                   0                   1       ...                    0   \n",
       "3                   0                   0       ...                    0   \n",
       "4                   0                   0       ...                    0   \n",
       "\n",
       "   Team_2_Netherlands  Team_2_New Zealand  Team_2_Pakistan  Team_2_Scotland  \\\n",
       "0                   0                   0                0                0   \n",
       "1                   0                   0                0                0   \n",
       "2                   0                   0                0                0   \n",
       "3                   0                   0                0                0   \n",
       "4                   0                   0                0                0   \n",
       "\n",
       "   Team_2_South Africa  Team_2_Sri Lanka  Team_2_U.A.E.  Team_2_West Indies  \\\n",
       "0                    1                 0              0                   0   \n",
       "1                    0                 0              0                   1   \n",
       "2                    0                 1              0                   0   \n",
       "3                    0                 0              0                   0   \n",
       "4                    0                 0              0                   0   \n",
       "\n",
       "   Team_2_Zimbabwe  \n",
       "0                0  \n",
       "1                0  \n",
       "2                0  \n",
       "3                0  \n",
       "4                0  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get dummy variables and drop winning_team column\n",
    "pred_set = pd.get_dummies(pred_set, prefix=['Team_1', 'Team_2'], columns=['Team_1', 'Team_2'])\n",
    "\n",
    "# Add missing columns compared to the model's training dataset\n",
    "missing_cols = set(final.columns) - set(pred_set.columns)\n",
    "for c in missing_cols:\n",
    "    pred_set[c] = 0\n",
    "pred_set = pred_set[final.columns]\n",
    "\n",
    "\n",
    "pred_set = pred_set.drop(['Winner'], axis=1)\n",
    "pred_set.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "South Africa and England\n",
      "Winner: England\n",
      "\n",
      "West Indies and Pakistan\n",
      "Winner: Pakistan\n",
      "\n",
      "Sri Lanka and New Zealand\n",
      "Winner: New Zealand\n",
      "\n",
      "Afghanistan and Australia\n",
      "Winner: Australia\n",
      "\n",
      "Bangladesh and South Africa\n",
      "Winner: South Africa\n",
      "\n",
      "Pakistan and England\n",
      "Winner: England\n",
      "\n",
      "Afghanistan and Sri Lanka\n",
      "Winner: Sri Lanka\n",
      "\n",
      "South Africa and India\n",
      "Winner: India\n",
      "\n",
      "Bangladesh and New Zealand\n",
      "Winner: New Zealand\n",
      "\n",
      "West Indies and Australia\n",
      "Winner: Australia\n",
      "\n",
      "Sri Lanka and Pakistan\n",
      "Winner: Pakistan\n",
      "\n",
      "Bangladesh and England\n",
      "Winner: England\n",
      "\n",
      "Afghanistan and New Zealand\n",
      "Winner: New Zealand\n",
      "\n",
      "Australia and India\n",
      "Winner: India\n",
      "\n",
      "West Indies and South Africa\n",
      "Winner: South Africa\n",
      "\n",
      "Sri Lanka and Bangladesh\n",
      "Winner: Bangladesh\n",
      "\n",
      "Pakistan and Australia\n",
      "Winner: Australia\n",
      "\n",
      "New Zealand and India\n",
      "Winner: India\n",
      "\n",
      "West Indies and England\n",
      "Winner: England\n",
      "\n",
      "Sri Lanka and Australia\n",
      "Winner: Australia\n",
      "\n",
      "Afghanistan and South Africa\n",
      "Winner: South Africa\n",
      "\n",
      "Pakistan and India\n",
      "Winner: India\n",
      "\n",
      "West Indies and Bangladesh\n",
      "Winner: Bangladesh\n",
      "\n",
      "Afghanistan and England\n",
      "Winner: England\n",
      "\n",
      "New Zealand and South Africa\n",
      "Winner: South Africa\n",
      "\n",
      "Bangladesh and Australia\n",
      "Winner: Australia\n",
      "\n",
      "Sri Lanka and England\n",
      "Winner: England\n",
      "\n",
      "Afghanistan and India\n",
      "Winner: India\n",
      "\n",
      "West Indies and New Zealand\n",
      "Winner: New Zealand\n",
      "\n",
      "Pakistan and South Africa\n",
      "Winner: South Africa\n",
      "\n",
      "Afghanistan and Bangladesh\n",
      "Winner: Bangladesh\n",
      "\n",
      "Australia and England\n",
      "Winner: England\n",
      "\n",
      "Pakistan and New Zealand\n",
      "Winner: New Zealand\n",
      "\n",
      "West Indies and India\n",
      "Winner: India\n",
      "\n",
      "Sri Lanka and South Africa\n",
      "Winner: South Africa\n",
      "\n",
      "Afghanistan and Pakistan\n",
      "Winner: Pakistan\n",
      "\n",
      "Australia and New Zealand\n",
      "Winner: New Zealand\n",
      "\n",
      "India and England\n",
      "Winner: England\n",
      "\n",
      "Sri Lanka and West Indies\n",
      "Winner: West Indies\n",
      "\n",
      "Bangladesh and India\n",
      "Winner: India\n",
      "\n",
      "New Zealand and England\n",
      "Winner: England\n",
      "\n",
      "Afghanistan and West Indies\n",
      "Winner: West Indies\n",
      "\n",
      "Bangladesh and Pakistan\n",
      "Winner: Pakistan\n",
      "\n",
      "Sri Lanka and India\n",
      "Winner: India\n",
      "\n",
      "Australia and South Africa\n",
      "Winner: South Africa\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#group matches \n",
    "predictions = rf.predict(pred_set)\n",
    "for i in range(fixtures.shape[0]):\n",
    "    print(backup_pred_set.iloc[i, 1] + \" and \" + backup_pred_set.iloc[i, 0])\n",
    "    if predictions[i] == 1:\n",
    "        print(\"Winner: \" + backup_pred_set.iloc[i, 1])\n",
    "    \n",
    "    else:\n",
    "        print(\"Winner: \" + backup_pred_set.iloc[i, 0])\n",
    "    print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [],
   "source": [
    "# List of tuples before \n",
    "semi = [('New Zealand', 'India'),\n",
    "            ('England', 'South Africa')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "metadata": {},
   "outputs": [],
   "source": [
    "def clean_and_predict(matches, ranking, final, logreg):\n",
    "\n",
    "    # Initialization of auxiliary list for data cleaning\n",
    "    positions = []\n",
    "\n",
    "    # Loop to retrieve each team's position according to ICC ranking\n",
    "    for match in matches:\n",
    "        positions.append(ranking.loc[ranking['Team'] == match[0],'Position'].iloc[0])\n",
    "        positions.append(ranking.loc[ranking['Team'] == match[1],'Position'].iloc[0])\n",
    "    \n",
    "    # Creating the DataFrame for prediction\n",
    "    pred_set = []\n",
    "\n",
    "    # Initializing iterators for while loop\n",
    "    i = 0\n",
    "    j = 0\n",
    "\n",
    "    # 'i' will be the iterator for the 'positions' list, and 'j' for the list of matches (list of tuples)\n",
    "    while i < len(positions):\n",
    "        dict1 = {}\n",
    "\n",
    "        # If position of first team is better then this team will be the 'Team_1' team, and vice-versa\n",
    "        if positions[i] < positions[i + 1]:\n",
    "            dict1.update({'Team_1': matches[j][0], 'Team_2': matches[j][1]})\n",
    "        else:\n",
    "            dict1.update({'Team_1': matches[j][1], 'Team_2': matches[j][0]})\n",
    "\n",
    "        # Append updated dictionary to the list, that will later be converted into a DataFrame\n",
    "        pred_set.append(dict1)\n",
    "        i += 2\n",
    "        j += 1\n",
    "        \n",
    "        # Convert list into DataFrame\n",
    "    pred_set = pd.DataFrame(pred_set)\n",
    "    backup_pred_set = pred_set\n",
    "\n",
    "    # Get dummy variables and drop winning_team column\n",
    "    pred_set = pd.get_dummies(pred_set, prefix=['Team_1', 'Team_2'], columns=['Team_1', 'Team_2'])\n",
    "\n",
    "    # Add missing columns compared to the model's training dataset\n",
    "    missing_cols2 = set(final.columns) - set(pred_set.columns)\n",
    "    for c in missing_cols2:\n",
    "        pred_set[c] = 0\n",
    "    pred_set = pred_set[final.columns]\n",
    "\n",
    "    pred_set = pred_set.drop(['Winner'], axis=1)\n",
    "\n",
    "    # Predict!\n",
    "    predictions = logreg.predict(pred_set)\n",
    "    for i in range(len(pred_set)):\n",
    "        print(backup_pred_set.iloc[i, 1] + \" and \" + backup_pred_set.iloc[i, 0])\n",
    "        if predictions[i] == 1:\n",
    "            print(\"Winner: \" + backup_pred_set.iloc[i, 1])\n",
    "        else:\n",
    "            print(\"Winner: \" + backup_pred_set.iloc[i, 0])\n",
    "        print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "New Zealand and India\n",
      "Winner: India\n",
      "\n",
      "South Africa and England\n",
      "Winner: England\n",
      "\n"
     ]
    }
   ],
   "source": [
    "clean_and_predict(semi, ranking, final, rf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Finals\n",
    "finals = [('India', 'England')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "India and England\n",
      "Winner: England\n",
      "\n"
     ]
    }
   ],
   "source": [
    "clean_and_predict(finals, ranking, final, rf)"
   ]
  }
 ],
 "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
