{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "599306b1-777c-447a-915c-8b7753065da8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import mean_squared_error, r2_score\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "16c87bdd-6384-4e6b-b9cc-2be6dcde0c63",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"youtube.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "6a06c67b-44f7-4a3e-90f5-6ac5bfaed688",
   "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>index</th>\n",
       "      <th>video_id</th>\n",
       "      <th>trending_date</th>\n",
       "      <th>title</th>\n",
       "      <th>channel_title</th>\n",
       "      <th>category_id</th>\n",
       "      <th>publish_date</th>\n",
       "      <th>time_frame</th>\n",
       "      <th>published_day_of_week</th>\n",
       "      <th>publish_country</th>\n",
       "      <th>tags</th>\n",
       "      <th>views</th>\n",
       "      <th>likes</th>\n",
       "      <th>dislikes</th>\n",
       "      <th>comment_count</th>\n",
       "      <th>comments_disabled</th>\n",
       "      <th>ratings_disabled</th>\n",
       "      <th>video_error_or_removed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2kyS6SvSYSE</td>\n",
       "      <td>17.14.11</td>\n",
       "      <td>WE WANT TO TALK ABOUT OUR MARRIAGE</td>\n",
       "      <td>CaseyNeistat</td>\n",
       "      <td>22</td>\n",
       "      <td>13/11/2017</td>\n",
       "      <td>17:00 to 17:59</td>\n",
       "      <td>Monday</td>\n",
       "      <td>US</td>\n",
       "      <td>SHANtell martin</td>\n",
       "      <td>748374</td>\n",
       "      <td>57527</td>\n",
       "      <td>2966</td>\n",
       "      <td>15954</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1ZAPwfrtAFY</td>\n",
       "      <td>17.14.11</td>\n",
       "      <td>The Trump Presidency: Last Week Tonight with J...</td>\n",
       "      <td>LastWeekTonight</td>\n",
       "      <td>24</td>\n",
       "      <td>13/11/2017</td>\n",
       "      <td>7:00 to 7:59</td>\n",
       "      <td>Monday</td>\n",
       "      <td>US</td>\n",
       "      <td>last week tonight trump presidency|\"last week ...</td>\n",
       "      <td>2418783</td>\n",
       "      <td>97185</td>\n",
       "      <td>6146</td>\n",
       "      <td>12703</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>5qpjK5DgCt4</td>\n",
       "      <td>17.14.11</td>\n",
       "      <td>Racist Superman | Rudy Mancuso, King Bach &amp; Le...</td>\n",
       "      <td>Rudy Mancuso</td>\n",
       "      <td>23</td>\n",
       "      <td>12/11/2017</td>\n",
       "      <td>19:00 to 19:59</td>\n",
       "      <td>Sunday</td>\n",
       "      <td>US</td>\n",
       "      <td>racist superman|\"rudy\"\"|\"\"mancuso\"\"|\"\"king\"\"|\"...</td>\n",
       "      <td>3191434</td>\n",
       "      <td>146033</td>\n",
       "      <td>5339</td>\n",
       "      <td>8181</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>puqaWrEC7tY</td>\n",
       "      <td>17.14.11</td>\n",
       "      <td>Nickelback Lyrics: Real or Fake?</td>\n",
       "      <td>Good Mythical Morning</td>\n",
       "      <td>24</td>\n",
       "      <td>13/11/2017</td>\n",
       "      <td>11:00 to 11:59</td>\n",
       "      <td>Monday</td>\n",
       "      <td>US</td>\n",
       "      <td>rhett and link|\"gmm\"\"|\"\"good mythical morning\"...</td>\n",
       "      <td>343168</td>\n",
       "      <td>10172</td>\n",
       "      <td>666</td>\n",
       "      <td>2146</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>d380meD0W0M</td>\n",
       "      <td>17.14.11</td>\n",
       "      <td>I Dare You: GOING BALD!?</td>\n",
       "      <td>nigahiga</td>\n",
       "      <td>24</td>\n",
       "      <td>12/11/2017</td>\n",
       "      <td>18:00 to 18:59</td>\n",
       "      <td>Sunday</td>\n",
       "      <td>US</td>\n",
       "      <td>ryan|\"higa\"\"|\"\"higatv\"\"|\"\"nigahiga\"\"|\"\"i dare ...</td>\n",
       "      <td>2095731</td>\n",
       "      <td>132235</td>\n",
       "      <td>1989</td>\n",
       "      <td>17518</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index     video_id trending_date  \\\n",
       "0      0  2kyS6SvSYSE      17.14.11   \n",
       "1      1  1ZAPwfrtAFY      17.14.11   \n",
       "2      2  5qpjK5DgCt4      17.14.11   \n",
       "3      3  puqaWrEC7tY      17.14.11   \n",
       "4      4  d380meD0W0M      17.14.11   \n",
       "\n",
       "                                               title          channel_title  \\\n",
       "0                 WE WANT TO TALK ABOUT OUR MARRIAGE           CaseyNeistat   \n",
       "1  The Trump Presidency: Last Week Tonight with J...        LastWeekTonight   \n",
       "2  Racist Superman | Rudy Mancuso, King Bach & Le...           Rudy Mancuso   \n",
       "3                   Nickelback Lyrics: Real or Fake?  Good Mythical Morning   \n",
       "4                           I Dare You: GOING BALD!?               nigahiga   \n",
       "\n",
       "   category_id publish_date      time_frame published_day_of_week  \\\n",
       "0           22   13/11/2017  17:00 to 17:59                Monday   \n",
       "1           24   13/11/2017    7:00 to 7:59                Monday   \n",
       "2           23   12/11/2017  19:00 to 19:59                Sunday   \n",
       "3           24   13/11/2017  11:00 to 11:59                Monday   \n",
       "4           24   12/11/2017  18:00 to 18:59                Sunday   \n",
       "\n",
       "  publish_country                                               tags    views  \\\n",
       "0              US                                    SHANtell martin   748374   \n",
       "1              US  last week tonight trump presidency|\"last week ...  2418783   \n",
       "2              US  racist superman|\"rudy\"\"|\"\"mancuso\"\"|\"\"king\"\"|\"...  3191434   \n",
       "3              US  rhett and link|\"gmm\"\"|\"\"good mythical morning\"...   343168   \n",
       "4              US  ryan|\"higa\"\"|\"\"higatv\"\"|\"\"nigahiga\"\"|\"\"i dare ...  2095731   \n",
       "\n",
       "    likes  dislikes  comment_count  comments_disabled  ratings_disabled  \\\n",
       "0   57527      2966          15954              False             False   \n",
       "1   97185      6146          12703              False             False   \n",
       "2  146033      5339           8181              False             False   \n",
       "3   10172       666           2146              False             False   \n",
       "4  132235      1989          17518              False             False   \n",
       "\n",
       "   video_error_or_removed  \n",
       "0                   False  \n",
       "1                   False  \n",
       "2                   False  \n",
       "3                   False  \n",
       "4                   False  "
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "fd1e8adc-2ac0-4a3d-a57f-2c63ffa98245",
   "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>index</th>\n",
       "      <th>category_id</th>\n",
       "      <th>views</th>\n",
       "      <th>likes</th>\n",
       "      <th>dislikes</th>\n",
       "      <th>comment_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>161470.00000</td>\n",
       "      <td>161470.000000</td>\n",
       "      <td>1.614700e+05</td>\n",
       "      <td>1.614700e+05</td>\n",
       "      <td>1.614700e+05</td>\n",
       "      <td>1.614700e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>80734.50000</td>\n",
       "      <td>19.461151</td>\n",
       "      <td>2.419854e+06</td>\n",
       "      <td>6.566194e+04</td>\n",
       "      <td>3.490153e+03</td>\n",
       "      <td>7.035494e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>46612.51832</td>\n",
       "      <td>7.432001</td>\n",
       "      <td>1.043749e+07</td>\n",
       "      <td>2.260617e+05</td>\n",
       "      <td>3.114779e+04</td>\n",
       "      <td>3.404121e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.230000e+02</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>40367.25000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>1.015382e+05</td>\n",
       "      <td>1.975000e+03</td>\n",
       "      <td>8.500000e+01</td>\n",
       "      <td>2.790000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>80734.50000</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>3.847395e+05</td>\n",
       "      <td>9.840000e+03</td>\n",
       "      <td>3.480000e+02</td>\n",
       "      <td>1.144000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>121101.75000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>1.339528e+06</td>\n",
       "      <td>4.006275e+04</td>\n",
       "      <td>1.350000e+03</td>\n",
       "      <td>4.144750e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>161469.00000</td>\n",
       "      <td>44.000000</td>\n",
       "      <td>4.245389e+08</td>\n",
       "      <td>5.613827e+06</td>\n",
       "      <td>1.944971e+06</td>\n",
       "      <td>1.626501e+06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              index    category_id         views         likes      dislikes  \\\n",
       "count  161470.00000  161470.000000  1.614700e+05  1.614700e+05  1.614700e+05   \n",
       "mean    80734.50000      19.461151  2.419854e+06  6.566194e+04  3.490153e+03   \n",
       "std     46612.51832       7.432001  1.043749e+07  2.260617e+05  3.114779e+04   \n",
       "min         0.00000       1.000000  2.230000e+02  0.000000e+00  0.000000e+00   \n",
       "25%     40367.25000      15.000000  1.015382e+05  1.975000e+03  8.500000e+01   \n",
       "50%     80734.50000      23.000000  3.847395e+05  9.840000e+03  3.480000e+02   \n",
       "75%    121101.75000      24.000000  1.339528e+06  4.006275e+04  1.350000e+03   \n",
       "max    161469.00000      44.000000  4.245389e+08  5.613827e+06  1.944971e+06   \n",
       "\n",
       "       comment_count  \n",
       "count   1.614700e+05  \n",
       "mean    7.035494e+03  \n",
       "std     3.404121e+04  \n",
       "min     0.000000e+00  \n",
       "25%     2.790000e+02  \n",
       "50%     1.144000e+03  \n",
       "75%     4.144750e+03  \n",
       "max     1.626501e+06  "
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "75bc7548-b375-4c5d-85dd-54735230e98d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['publish_date'] = pd.to_datetime(df['publish_date'], dayfirst=True, errors='coerce')\n",
    "df['trending_date'] = pd.to_datetime(\n",
    "    df['trending_date'],\n",
    "    format='%y.%d.%m'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "a2d7d781-3422-4052-9338-c8e4153ccaba",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['tags_count'] = df['tags'].str.count('\\|') + 1\n",
    "df['tags_length'] = df['tags'].str.len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "fd28be6c-d3db-46ef-944d-2ee791c1399a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['publish_hour'] = df['time_frame'].str.split(':').str[0].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "39ca622b-3e9f-4e0b-bbc4-eff9f21ed63c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ratings_disabled\n",
       "False    0.991181\n",
       "True     0.008819\n",
       "Name: proportion, dtype: float64"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['ratings_disabled'].value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "efe7701b-4813-435f-9641-f5e89f4d8c66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "comments_disabled\n",
       "False    0.982734\n",
       "True     0.017266\n",
       "Name: proportion, dtype: float64"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['comments_disabled'].value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "8eb3b362-9e0a-4b6b-9c9d-e71c12599500",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df[\n",
    "    (df['comments_disabled'] == False) &\n",
    "    (df['ratings_disabled'] == False)\n",
    "].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "4a468ebf-0138-4599-86a6-99583f208135",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.drop(columns=['ratings_disabled', 'comments_disabled'])\n",
    "df = df.drop(columns=['time_frame'])\n",
    "df = df.drop(columns=['index', 'video_error_or_removed'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "6cb877aa-eb15-4f73-848c-fa24c89a704f",
   "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>video_id</th>\n",
       "      <th>trending_date</th>\n",
       "      <th>title</th>\n",
       "      <th>channel_title</th>\n",
       "      <th>category_id</th>\n",
       "      <th>publish_date</th>\n",
       "      <th>published_day_of_week</th>\n",
       "      <th>publish_country</th>\n",
       "      <th>tags</th>\n",
       "      <th>views</th>\n",
       "      <th>likes</th>\n",
       "      <th>dislikes</th>\n",
       "      <th>comment_count</th>\n",
       "      <th>tags_count</th>\n",
       "      <th>tags_length</th>\n",
       "      <th>publish_hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2kyS6SvSYSE</td>\n",
       "      <td>2017-11-14</td>\n",
       "      <td>WE WANT TO TALK ABOUT OUR MARRIAGE</td>\n",
       "      <td>CaseyNeistat</td>\n",
       "      <td>22</td>\n",
       "      <td>2017-11-13</td>\n",
       "      <td>Monday</td>\n",
       "      <td>US</td>\n",
       "      <td>SHANtell martin</td>\n",
       "      <td>748374</td>\n",
       "      <td>57527</td>\n",
       "      <td>2966</td>\n",
       "      <td>15954</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1ZAPwfrtAFY</td>\n",
       "      <td>2017-11-14</td>\n",
       "      <td>The Trump Presidency: Last Week Tonight with J...</td>\n",
       "      <td>LastWeekTonight</td>\n",
       "      <td>24</td>\n",
       "      <td>2017-11-13</td>\n",
       "      <td>Monday</td>\n",
       "      <td>US</td>\n",
       "      <td>last week tonight trump presidency|\"last week ...</td>\n",
       "      <td>2418783</td>\n",
       "      <td>97185</td>\n",
       "      <td>6146</td>\n",
       "      <td>12703</td>\n",
       "      <td>4</td>\n",
       "      <td>108</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5qpjK5DgCt4</td>\n",
       "      <td>2017-11-14</td>\n",
       "      <td>Racist Superman | Rudy Mancuso, King Bach &amp; Le...</td>\n",
       "      <td>Rudy Mancuso</td>\n",
       "      <td>23</td>\n",
       "      <td>2017-11-12</td>\n",
       "      <td>Sunday</td>\n",
       "      <td>US</td>\n",
       "      <td>racist superman|\"rudy\"\"|\"\"mancuso\"\"|\"\"king\"\"|\"...</td>\n",
       "      <td>3191434</td>\n",
       "      <td>146033</td>\n",
       "      <td>5339</td>\n",
       "      <td>8181</td>\n",
       "      <td>23</td>\n",
       "      <td>356</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>puqaWrEC7tY</td>\n",
       "      <td>2017-11-14</td>\n",
       "      <td>Nickelback Lyrics: Real or Fake?</td>\n",
       "      <td>Good Mythical Morning</td>\n",
       "      <td>24</td>\n",
       "      <td>2017-11-13</td>\n",
       "      <td>Monday</td>\n",
       "      <td>US</td>\n",
       "      <td>rhett and link|\"gmm\"\"|\"\"good mythical morning\"...</td>\n",
       "      <td>343168</td>\n",
       "      <td>10172</td>\n",
       "      <td>666</td>\n",
       "      <td>2146</td>\n",
       "      <td>27</td>\n",
       "      <td>529</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d380meD0W0M</td>\n",
       "      <td>2017-11-14</td>\n",
       "      <td>I Dare You: GOING BALD!?</td>\n",
       "      <td>nigahiga</td>\n",
       "      <td>24</td>\n",
       "      <td>2017-11-12</td>\n",
       "      <td>Sunday</td>\n",
       "      <td>US</td>\n",
       "      <td>ryan|\"higa\"\"|\"\"higatv\"\"|\"\"nigahiga\"\"|\"\"i dare ...</td>\n",
       "      <td>2095731</td>\n",
       "      <td>132235</td>\n",
       "      <td>1989</td>\n",
       "      <td>17518</td>\n",
       "      <td>14</td>\n",
       "      <td>146</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      video_id trending_date  \\\n",
       "0  2kyS6SvSYSE    2017-11-14   \n",
       "1  1ZAPwfrtAFY    2017-11-14   \n",
       "2  5qpjK5DgCt4    2017-11-14   \n",
       "3  puqaWrEC7tY    2017-11-14   \n",
       "4  d380meD0W0M    2017-11-14   \n",
       "\n",
       "                                               title          channel_title  \\\n",
       "0                 WE WANT TO TALK ABOUT OUR MARRIAGE           CaseyNeistat   \n",
       "1  The Trump Presidency: Last Week Tonight with J...        LastWeekTonight   \n",
       "2  Racist Superman | Rudy Mancuso, King Bach & Le...           Rudy Mancuso   \n",
       "3                   Nickelback Lyrics: Real or Fake?  Good Mythical Morning   \n",
       "4                           I Dare You: GOING BALD!?               nigahiga   \n",
       "\n",
       "   category_id publish_date published_day_of_week publish_country  \\\n",
       "0           22   2017-11-13                Monday              US   \n",
       "1           24   2017-11-13                Monday              US   \n",
       "2           23   2017-11-12                Sunday              US   \n",
       "3           24   2017-11-13                Monday              US   \n",
       "4           24   2017-11-12                Sunday              US   \n",
       "\n",
       "                                                tags    views   likes  \\\n",
       "0                                    SHANtell martin   748374   57527   \n",
       "1  last week tonight trump presidency|\"last week ...  2418783   97185   \n",
       "2  racist superman|\"rudy\"\"|\"\"mancuso\"\"|\"\"king\"\"|\"...  3191434  146033   \n",
       "3  rhett and link|\"gmm\"\"|\"\"good mythical morning\"...   343168   10172   \n",
       "4  ryan|\"higa\"\"|\"\"higatv\"\"|\"\"nigahiga\"\"|\"\"i dare ...  2095731  132235   \n",
       "\n",
       "   dislikes  comment_count  tags_count  tags_length  publish_hour  \n",
       "0      2966          15954           1           15            17  \n",
       "1      6146          12703           4          108             7  \n",
       "2      5339           8181          23          356            19  \n",
       "3       666           2146          27          529            11  \n",
       "4      1989          17518          14          146            18  "
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "a9ca71b1-bb60-40d5-8331-6a750bea3a6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['video_id', 'trending_date', 'title', 'channel_title', 'category_id',\n",
       "       'publish_date', 'published_day_of_week', 'publish_country', 'tags',\n",
       "       'views', 'likes', 'dislikes', 'comment_count', 'tags_count',\n",
       "       'tags_length', 'publish_hour'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "68e096a4-0f1e-4ec3-8430-fa8e2cd24060",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    1.580340e+05\n",
       "mean     7.185344e+03\n",
       "std      3.439382e+04\n",
       "min      0.000000e+00\n",
       "25%      3.080000e+02\n",
       "50%      1.198000e+03\n",
       "75%      4.275000e+03\n",
       "max      1.626501e+06\n",
       "Name: comment_count, dtype: float64"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['comment_count'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "263fb98b-38c6-48b5-8ca7-4c9ff30a8dec",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['comment_bin'] = pd.qcut(\n",
    "    df['comment_count'],\n",
    "    q=5,\n",
    "    labels=False,\n",
    "    duplicates='drop'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "a106d7fe-86b2-43d9-9ee9-14970ad1357b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "comment_bin\n",
       "0    31631\n",
       "3    31606\n",
       "4    31604\n",
       "2    31597\n",
       "1    31596\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['comment_bin'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "3904de59-93bb-49ad-80ad-2693ff26ddf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/t0/wrlt9dys2m59fs2jsn0_bb280000gn/T/ipykernel_2379/1056370390.py:3: FutureWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n",
      "  .apply(lambda x: x.sample(n=10000, random_state=42))\n"
     ]
    }
   ],
   "source": [
    "balanced_df = (\n",
    "    df.groupby('comment_bin', group_keys=False)\n",
    "      .apply(lambda x: x.sample(n=10000, random_state=42))\n",
    "      .copy()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "59da8775-3199-4f47-9a51-896b1cb78e70",
   "metadata": {},
   "outputs": [],
   "source": [
    "balanced_df.drop(columns=['comment_bin'], inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "d72c41b6-abbf-4c11-a02a-0a1a148da89e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000, 16)"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "9091f98c-0352-48ee-96be-5bc82aa94a81",
   "metadata": {},
   "outputs": [],
   "source": [
    "balanced_df['log_comments'] = np.log1p(balanced_df['comment_count'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "d3e73f6f-c665-4f58-adf7-30c9f312b540",
   "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>video_id</th>\n",
       "      <th>trending_date</th>\n",
       "      <th>title</th>\n",
       "      <th>channel_title</th>\n",
       "      <th>category_id</th>\n",
       "      <th>publish_date</th>\n",
       "      <th>published_day_of_week</th>\n",
       "      <th>publish_country</th>\n",
       "      <th>tags</th>\n",
       "      <th>views</th>\n",
       "      <th>...</th>\n",
       "      <th>dislikes</th>\n",
       "      <th>comment_count</th>\n",
       "      <th>tags_count</th>\n",
       "      <th>tags_length</th>\n",
       "      <th>publish_hour</th>\n",
       "      <th>log_comments</th>\n",
       "      <th>publish_month</th>\n",
       "      <th>publish_year</th>\n",
       "      <th>is_weekend</th>\n",
       "      <th>days_to_trend</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>105548</th>\n",
       "      <td>7z_dSRE6KAg</td>\n",
       "      <td>2018-03-24</td>\n",
       "      <td>Mohammed VI, gravement malade, prÃ©pare son fi...</td>\n",
       "      <td>365 Days</td>\n",
       "      <td>25</td>\n",
       "      <td>2018-03-15</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>FRANCE</td>\n",
       "      <td>Mohammed VI\"|\"Maroc\"|\"News Maroc\"|\"fils\"|\"la s...</td>\n",
       "      <td>46923</td>\n",
       "      <td>...</td>\n",
       "      <td>66</td>\n",
       "      <td>163</td>\n",
       "      <td>7</td>\n",
       "      <td>75</td>\n",
       "      <td>16</td>\n",
       "      <td>5.099866</td>\n",
       "      <td>3</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101828</th>\n",
       "      <td>20xQIUzWQsY</td>\n",
       "      <td>2018-03-06</td>\n",
       "      <td>Camille Combal parodie Agathe Auproux sur Inst...</td>\n",
       "      <td>Touche pas Ã  mon poste !</td>\n",
       "      <td>24</td>\n",
       "      <td>2018-03-05</td>\n",
       "      <td>Monday</td>\n",
       "      <td>FRANCE</td>\n",
       "      <td>TPMP\"|\"Cyril Hanouna\"|\"Touche pas Ã  mon poste...</td>\n",
       "      <td>74255</td>\n",
       "      <td>...</td>\n",
       "      <td>61</td>\n",
       "      <td>116</td>\n",
       "      <td>8</td>\n",
       "      <td>141</td>\n",
       "      <td>10</td>\n",
       "      <td>4.762174</td>\n",
       "      <td>3</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68985</th>\n",
       "      <td>w3Rd-KZeVp4</td>\n",
       "      <td>2018-04-06</td>\n",
       "      <td>Khabib Nurmagomedov Comments on Confrontation ...</td>\n",
       "      <td>MMAWeekly.com</td>\n",
       "      <td>17</td>\n",
       "      <td>2018-04-05</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>GB</td>\n",
       "      <td>MMA|\"mixed martial arts\"|\"MMAWeekly.com\"|\"MMAW...</td>\n",
       "      <td>64009</td>\n",
       "      <td>...</td>\n",
       "      <td>112</td>\n",
       "      <td>105</td>\n",
       "      <td>11</td>\n",
       "      <td>179</td>\n",
       "      <td>0</td>\n",
       "      <td>4.663439</td>\n",
       "      <td>4</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44121</th>\n",
       "      <td>KfD8nLAeMDQ</td>\n",
       "      <td>2017-11-29</td>\n",
       "      <td>Best Friend From Heaven - Trailer (2017)</td>\n",
       "      <td>Brain Power Studio</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-11-16</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>GB</td>\n",
       "      <td>family film|\"movie\"|\"dog\"|\"romance movie\"|\"kid...</td>\n",
       "      <td>83872</td>\n",
       "      <td>...</td>\n",
       "      <td>79</td>\n",
       "      <td>75</td>\n",
       "      <td>9</td>\n",
       "      <td>127</td>\n",
       "      <td>20</td>\n",
       "      <td>4.330733</td>\n",
       "      <td>11</td>\n",
       "      <td>2017</td>\n",
       "      <td>False</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31046</th>\n",
       "      <td>FhhlMSpvCQE</td>\n",
       "      <td>2018-04-26</td>\n",
       "      <td>Haim Gets Ready for Coachella | Vogue</td>\n",
       "      <td>Vogue</td>\n",
       "      <td>26</td>\n",
       "      <td>2018-04-20</td>\n",
       "      <td>Friday</td>\n",
       "      <td>US</td>\n",
       "      <td>coachella|\"haim\"\"|\"\"haim coachella\"\"|\"\"coachel...</td>\n",
       "      <td>169972</td>\n",
       "      <td>...</td>\n",
       "      <td>146</td>\n",
       "      <td>140</td>\n",
       "      <td>33</td>\n",
       "      <td>560</td>\n",
       "      <td>13</td>\n",
       "      <td>4.948760</td>\n",
       "      <td>4</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           video_id trending_date  \\\n",
       "105548  7z_dSRE6KAg    2018-03-24   \n",
       "101828  20xQIUzWQsY    2018-03-06   \n",
       "68985   w3Rd-KZeVp4    2018-04-06   \n",
       "44121   KfD8nLAeMDQ    2017-11-29   \n",
       "31046   FhhlMSpvCQE    2018-04-26   \n",
       "\n",
       "                                                    title  \\\n",
       "105548  Mohammed VI, gravement malade, prÃ©pare son fi...   \n",
       "101828  Camille Combal parodie Agathe Auproux sur Inst...   \n",
       "68985   Khabib Nurmagomedov Comments on Confrontation ...   \n",
       "44121            Best Friend From Heaven - Trailer (2017)   \n",
       "31046               Haim Gets Ready for Coachella | Vogue   \n",
       "\n",
       "                    channel_title  category_id publish_date  \\\n",
       "105548                   365 Days           25   2018-03-15   \n",
       "101828  Touche pas Ã  mon poste !           24   2018-03-05   \n",
       "68985               MMAWeekly.com           17   2018-04-05   \n",
       "44121          Brain Power Studio            1   2017-11-16   \n",
       "31046                       Vogue           26   2018-04-20   \n",
       "\n",
       "       published_day_of_week publish_country  \\\n",
       "105548              Thursday          FRANCE   \n",
       "101828                Monday          FRANCE   \n",
       "68985               Thursday              GB   \n",
       "44121               Thursday              GB   \n",
       "31046                 Friday              US   \n",
       "\n",
       "                                                     tags   views  ...  \\\n",
       "105548  Mohammed VI\"|\"Maroc\"|\"News Maroc\"|\"fils\"|\"la s...   46923  ...   \n",
       "101828  TPMP\"|\"Cyril Hanouna\"|\"Touche pas Ã  mon poste...   74255  ...   \n",
       "68985   MMA|\"mixed martial arts\"|\"MMAWeekly.com\"|\"MMAW...   64009  ...   \n",
       "44121   family film|\"movie\"|\"dog\"|\"romance movie\"|\"kid...   83872  ...   \n",
       "31046   coachella|\"haim\"\"|\"\"haim coachella\"\"|\"\"coachel...  169972  ...   \n",
       "\n",
       "        dislikes  comment_count  tags_count  tags_length  publish_hour  \\\n",
       "105548        66            163           7           75            16   \n",
       "101828        61            116           8          141            10   \n",
       "68985        112            105          11          179             0   \n",
       "44121         79             75           9          127            20   \n",
       "31046        146            140          33          560            13   \n",
       "\n",
       "        log_comments  publish_month  publish_year  is_weekend  days_to_trend  \n",
       "105548      5.099866              3          2018       False            9.0  \n",
       "101828      4.762174              3          2018       False            1.0  \n",
       "68985       4.663439              4          2018       False            1.0  \n",
       "44121       4.330733             11          2017       False           13.0  \n",
       "31046       4.948760              4          2018       False            6.0  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "838fcbf8-826f-41d6-8b1c-42b1b7915c2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "balanced_df['publish_month'] = balanced_df['publish_date'].dt.month\n",
    "balanced_df['publish_year'] = balanced_df['publish_date'].dt.year\n",
    "balanced_df['is_weekend'] = balanced_df['publish_date'].dt.weekday >= 5\n",
    "balanced_df['days_to_trend'] = (\n",
    "    balanced_df['trending_date'] - df['publish_date']\n",
    ").dt.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "2bf812be-4225-49af-8a91-e4725cf4b172",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['video_id', 'trending_date', 'title', 'channel_title', 'category_id',\n",
       "       'publish_date', 'published_day_of_week', 'publish_country', 'tags',\n",
       "       'views', 'likes', 'dislikes', 'comment_count', 'tags_count',\n",
       "       'tags_length', 'publish_hour', 'log_comments', 'publish_month',\n",
       "       'publish_year', 'is_weekend', 'days_to_trend'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "366399c8-fe00-4715-b587-91fe4e3a6c70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df['publish_country'].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "id": "b668239a-369d-4912-852f-cf5163c8d944",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "STOPWORDS = {\n",
    "    \"the\",\"a\",\"an\",\"and\",\"or\",\"to\",\"of\",\"in\",\"on\",\"for\",\"with\",\"at\",\"by\",\n",
    "    \"official\",\"video\",\"trailer\",\"full\",\"episode\",\"new\",\"music\",\"mv\",\"vs\",\n",
    "    \"best\",\"from\",\"ready\",\"live\",\"feat\",\"ft\",\"hd\",\"4k\",\"2024\",\"2023\",\n",
    "    \"season\",\"part\",\"game\",\"clip\",\"reaction\"\n",
    "}\n",
    "\n",
    "def make_keyword(title: str, max_words: int = 3) -> str:\n",
    "    if not isinstance(title, str):\n",
    "        return \"\"\n",
    "    t = title.lower()\n",
    "    t = re.sub(r\"http\\S+|www\\.\\S+\", \" \", t)\n",
    "    t = re.sub(r\"[^a-z0-9\\s]\", \" \", t)\n",
    "    words = [w for w in t.split() if w not in STOPWORDS and len(w) > 2 and not w.isdigit()]\n",
    "    return \" \".join(words[:max_words])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "id": "b1318d08-4199-4801-a620-5fad7f9da312",
   "metadata": {},
   "outputs": [],
   "source": [
    "balanced_df[\"keyword\"] = balanced_df[\"title\"].apply(make_keyword)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "id": "cb36fcc2-f730-4325-b5c2-d8be75077333",
   "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>video_id</th>\n",
       "      <th>trending_date</th>\n",
       "      <th>title</th>\n",
       "      <th>channel_title</th>\n",
       "      <th>category_id</th>\n",
       "      <th>publish_date</th>\n",
       "      <th>published_day_of_week</th>\n",
       "      <th>publish_country</th>\n",
       "      <th>tags</th>\n",
       "      <th>views</th>\n",
       "      <th>...</th>\n",
       "      <th>tags_count</th>\n",
       "      <th>tags_length</th>\n",
       "      <th>publish_hour</th>\n",
       "      <th>log_comments</th>\n",
       "      <th>publish_month</th>\n",
       "      <th>publish_year</th>\n",
       "      <th>is_weekend</th>\n",
       "      <th>days_to_trend</th>\n",
       "      <th>keyword</th>\n",
       "      <th>trend_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>105548</th>\n",
       "      <td>7z_dSRE6KAg</td>\n",
       "      <td>2018-03-24</td>\n",
       "      <td>Mohammed VI, gravement malade, prÃ©pare son fi...</td>\n",
       "      <td>365 Days</td>\n",
       "      <td>25</td>\n",
       "      <td>2018-03-15</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>FRANCE</td>\n",
       "      <td>Mohammed VI\"|\"Maroc\"|\"News Maroc\"|\"fils\"|\"la s...</td>\n",
       "      <td>46923</td>\n",
       "      <td>...</td>\n",
       "      <td>7</td>\n",
       "      <td>75</td>\n",
       "      <td>16</td>\n",
       "      <td>5.099866</td>\n",
       "      <td>3</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>9.0</td>\n",
       "      <td>mohammed gravement malade</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101828</th>\n",
       "      <td>20xQIUzWQsY</td>\n",
       "      <td>2018-03-06</td>\n",
       "      <td>Camille Combal parodie Agathe Auproux sur Inst...</td>\n",
       "      <td>Touche pas Ã  mon poste !</td>\n",
       "      <td>24</td>\n",
       "      <td>2018-03-05</td>\n",
       "      <td>Monday</td>\n",
       "      <td>FRANCE</td>\n",
       "      <td>TPMP\"|\"Cyril Hanouna\"|\"Touche pas Ã  mon poste...</td>\n",
       "      <td>74255</td>\n",
       "      <td>...</td>\n",
       "      <td>8</td>\n",
       "      <td>141</td>\n",
       "      <td>10</td>\n",
       "      <td>4.762174</td>\n",
       "      <td>3</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>1.0</td>\n",
       "      <td>camille combal parodie</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68985</th>\n",
       "      <td>w3Rd-KZeVp4</td>\n",
       "      <td>2018-04-06</td>\n",
       "      <td>Khabib Nurmagomedov Comments on Confrontation ...</td>\n",
       "      <td>MMAWeekly.com</td>\n",
       "      <td>17</td>\n",
       "      <td>2018-04-05</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>GB</td>\n",
       "      <td>MMA|\"mixed martial arts\"|\"MMAWeekly.com\"|\"MMAW...</td>\n",
       "      <td>64009</td>\n",
       "      <td>...</td>\n",
       "      <td>11</td>\n",
       "      <td>179</td>\n",
       "      <td>0</td>\n",
       "      <td>4.663439</td>\n",
       "      <td>4</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>1.0</td>\n",
       "      <td>khabib nurmagomedov comments</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44121</th>\n",
       "      <td>KfD8nLAeMDQ</td>\n",
       "      <td>2017-11-29</td>\n",
       "      <td>Best Friend From Heaven - Trailer (2017)</td>\n",
       "      <td>Brain Power Studio</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-11-16</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>GB</td>\n",
       "      <td>family film|\"movie\"|\"dog\"|\"romance movie\"|\"kid...</td>\n",
       "      <td>83872</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>127</td>\n",
       "      <td>20</td>\n",
       "      <td>4.330733</td>\n",
       "      <td>11</td>\n",
       "      <td>2017</td>\n",
       "      <td>False</td>\n",
       "      <td>13.0</td>\n",
       "      <td>friend heaven</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31046</th>\n",
       "      <td>FhhlMSpvCQE</td>\n",
       "      <td>2018-04-26</td>\n",
       "      <td>Haim Gets Ready for Coachella | Vogue</td>\n",
       "      <td>Vogue</td>\n",
       "      <td>26</td>\n",
       "      <td>2018-04-20</td>\n",
       "      <td>Friday</td>\n",
       "      <td>US</td>\n",
       "      <td>coachella|\"haim\"\"|\"\"haim coachella\"\"|\"\"coachel...</td>\n",
       "      <td>169972</td>\n",
       "      <td>...</td>\n",
       "      <td>33</td>\n",
       "      <td>560</td>\n",
       "      <td>13</td>\n",
       "      <td>4.948760</td>\n",
       "      <td>4</td>\n",
       "      <td>2018</td>\n",
       "      <td>False</td>\n",
       "      <td>6.0</td>\n",
       "      <td>haim gets coachella</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           video_id trending_date  \\\n",
       "105548  7z_dSRE6KAg    2018-03-24   \n",
       "101828  20xQIUzWQsY    2018-03-06   \n",
       "68985   w3Rd-KZeVp4    2018-04-06   \n",
       "44121   KfD8nLAeMDQ    2017-11-29   \n",
       "31046   FhhlMSpvCQE    2018-04-26   \n",
       "\n",
       "                                                    title  \\\n",
       "105548  Mohammed VI, gravement malade, prÃ©pare son fi...   \n",
       "101828  Camille Combal parodie Agathe Auproux sur Inst...   \n",
       "68985   Khabib Nurmagomedov Comments on Confrontation ...   \n",
       "44121            Best Friend From Heaven - Trailer (2017)   \n",
       "31046               Haim Gets Ready for Coachella | Vogue   \n",
       "\n",
       "                    channel_title  category_id publish_date  \\\n",
       "105548                   365 Days           25   2018-03-15   \n",
       "101828  Touche pas Ã  mon poste !           24   2018-03-05   \n",
       "68985               MMAWeekly.com           17   2018-04-05   \n",
       "44121          Brain Power Studio            1   2017-11-16   \n",
       "31046                       Vogue           26   2018-04-20   \n",
       "\n",
       "       published_day_of_week publish_country  \\\n",
       "105548              Thursday          FRANCE   \n",
       "101828                Monday          FRANCE   \n",
       "68985               Thursday              GB   \n",
       "44121               Thursday              GB   \n",
       "31046                 Friday              US   \n",
       "\n",
       "                                                     tags   views  ...  \\\n",
       "105548  Mohammed VI\"|\"Maroc\"|\"News Maroc\"|\"fils\"|\"la s...   46923  ...   \n",
       "101828  TPMP\"|\"Cyril Hanouna\"|\"Touche pas Ã  mon poste...   74255  ...   \n",
       "68985   MMA|\"mixed martial arts\"|\"MMAWeekly.com\"|\"MMAW...   64009  ...   \n",
       "44121   family film|\"movie\"|\"dog\"|\"romance movie\"|\"kid...   83872  ...   \n",
       "31046   coachella|\"haim\"\"|\"\"haim coachella\"\"|\"\"coachel...  169972  ...   \n",
       "\n",
       "        tags_count  tags_length  publish_hour  log_comments  publish_month  \\\n",
       "105548           7           75            16      5.099866              3   \n",
       "101828           8          141            10      4.762174              3   \n",
       "68985           11          179             0      4.663439              4   \n",
       "44121            9          127            20      4.330733             11   \n",
       "31046           33          560            13      4.948760              4   \n",
       "\n",
       "        publish_year  is_weekend  days_to_trend                       keyword  \\\n",
       "105548          2018       False            9.0     mohammed gravement malade   \n",
       "101828          2018       False            1.0        camille combal parodie   \n",
       "68985           2018       False            1.0  khabib nurmagomedov comments   \n",
       "44121           2017       False           13.0                 friend heaven   \n",
       "31046           2018       False            6.0           haim gets coachella   \n",
       "\n",
       "        trend_score  \n",
       "105548          NaN  \n",
       "101828          NaN  \n",
       "68985           NaN  \n",
       "44121           NaN  \n",
       "31046           NaN  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 315,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 431,
   "id": "edcf1068-df70-4c5a-8258-c2d8f43a93ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "399"
      ]
     },
     "execution_count": 431,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kw = (balanced_df[\"keyword\"]\n",
    "      .value_counts()\n",
    "      .head(400)\n",
    "      .index\n",
    "      .tolist())\n",
    "kw = [k for k in kw if k]\n",
    "len(kw)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61777518-257c-44eb-8755-b43713f0bd14",
   "metadata": {},
   "source": [
    "## Adding a Google Trends Feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 433,
   "id": "b69e7e1e-0bd5-40ff-87f2-965cfe67a9d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n",
      "/Users/sanzhar/ML_project/ner_env/lib/python3.11/site-packages/pytrends/request.py:260: FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df = df.fillna(False)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(399,\n",
       " [('star wars last', 0.0),\n",
       "  ('avengers infinity war', 0.0),\n",
       "  ('vaani rani', 0.0),\n",
       "  ('marvel studios avengers', 0.0),\n",
       "  ('crime patrol dial', 0.0)])"
      ]
     },
     "execution_count": 433,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "from pytrends.request import TrendReq\n",
    "\n",
    "def fetch_trends_scores(keywords, geo=\"\", timeframe=\"2017-01-01 2017-12-31\", sleep_s=2):\n",
    "    pytrends = TrendReq(hl=\"en-US\", tz=360)\n",
    "    out = {}\n",
    "\n",
    "    for i in range(0, len(keywords), 5):\n",
    "        batch = keywords[i:i+5]\n",
    "        try:\n",
    "            pytrends.build_payload(batch, timeframe=timeframe, geo=geo)\n",
    "            it = pytrends.interest_over_time()\n",
    "\n",
    "            if it is None or it.empty:\n",
    "                for k in batch:\n",
    "                    out[k] = 0.0\n",
    "            else:\n",
    "                for k in batch:\n",
    "                    out[k] = float(it[k].mean()) if k in it.columns else 0.0\n",
    "\n",
    "        except Exception:\n",
    "            for k in batch:\n",
    "                out[k] = 0.0\n",
    "\n",
    "        time.sleep(sleep_s)\n",
    "\n",
    "    return out\n",
    "\n",
    "trend_map = fetch_trends_scores(kw, geo=\"\", timeframe=\"today 1-m\", sleep_s=2)\n",
    "len(trend_map), list(trend_map.items())[:5]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 435,
   "id": "c154f25d-8ccc-4a89-b78f-854896d86309",
   "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>keyword</th>\n",
       "      <th>trend_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>105548</th>\n",
       "      <td>mohammed gravement malade</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101828</th>\n",
       "      <td>camille combal parodie</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68985</th>\n",
       "      <td>khabib nurmagomedov comments</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44121</th>\n",
       "      <td>friend heaven</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31046</th>\n",
       "      <td>haim gets coachella</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42054</th>\n",
       "      <td>sweetie destroys flav</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54853</th>\n",
       "      <td>answer not hitchhiker</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14666</th>\n",
       "      <td>vision xfl</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83432</th>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133498</th>\n",
       "      <td>louis domingue steve</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             keyword  trend_score\n",
       "105548     mohammed gravement malade          0.0\n",
       "101828        camille combal parodie          0.0\n",
       "68985   khabib nurmagomedov comments          0.0\n",
       "44121                  friend heaven          0.0\n",
       "31046            haim gets coachella          0.0\n",
       "42054          sweetie destroys flav          0.0\n",
       "54853          answer not hitchhiker          0.0\n",
       "14666                     vision xfl          0.0\n",
       "83432                                         0.0\n",
       "133498          louis domingue steve          0.0"
      ]
     },
     "execution_count": 435,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df[\"trend_score\"] = balanced_df[\"keyword\"].map(trend_map).fillna(0.0)\n",
    "balanced_df[[\"keyword\", \"trend_score\"]].head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 437,
   "id": "63aef714-eeef-46d6-9054-8839b3f3af8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "105548    False\n",
       "101828    False\n",
       "68985     False\n",
       "44121     False\n",
       "31046     False\n",
       "          ...  \n",
       "71508     False\n",
       "43386     False\n",
       "41541     False\n",
       "23069     False\n",
       "19404     False\n",
       "Name: trend_score, Length: 50000, dtype: bool"
      ]
     },
     "execution_count": 437,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df.trend_score.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 439,
   "id": "34afda02-2d59-4300-acd7-fb30184634ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21320"
      ]
     },
     "execution_count": 439,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "balanced_df[\"keyword\"].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 443,
   "id": "b853d488-5744-45e8-b219-f213ad17f012",
   "metadata": {},
   "outputs": [],
   "source": [
    "features = [\n",
    "    'tags_count',\n",
    "    'tags_length',\n",
    "    'publish_hour',\n",
    "    'publish_month',\n",
    "    'days_to_trend',\n",
    "    'is_weekend',\n",
    "    'category_id',\n",
    "    'publish_country',\n",
    "    'trend_score'\n",
    "]\n",
    "\n",
    "X = balanced_df[features]\n",
    "y = balanced_df['log_comments']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 445,
   "id": "9428cdcb-0068-4a8b-b06f-ca3d323f2f0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = pd.get_dummies(\n",
    "    X,\n",
    "    columns=['publish_country'],\n",
    "    drop_first=True\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 447,
   "id": "c0a821e2-3dc9-482e-9795-507d97a42d6b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['tags_count', 'tags_length', 'publish_hour', 'publish_month',\n",
       "       'days_to_trend', 'is_weekend', 'category_id', 'trend_score',\n",
       "       'publish_country_FRANCE', 'publish_country_GB', 'publish_country_US'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 447,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 449,
   "id": "da99e147-c73d-42e3-ba70-99cd7ff5129c",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y,\n",
    "    test_size=0.2,\n",
    "    random_state=42\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 451,
   "id": "b105cfa7-cda7-4631-8c78-ba6e7772b3e1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((40000, 11), (10000, 11))"
      ]
     },
     "execution_count": 451,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape, X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 453,
   "id": "ac5b064d-c5f5-4b3e-8f42-3dd0a8ef6b0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-6 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: #000;\n",
       "  --sklearn-color-text-muted: #666;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "}\n",
       "\n",
       "#sk-container-id-6.light {\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: black;\n",
       "  --sklearn-color-background: white;\n",
       "  --sklearn-color-border-box: black;\n",
       "  --sklearn-color-icon: #696969;\n",
       "}\n",
       "\n",
       "#sk-container-id-6.dark {\n",
       "  --sklearn-color-text-on-default-background: white;\n",
       "  --sklearn-color-background: #111;\n",
       "  --sklearn-color-border-box: white;\n",
       "  --sklearn-color-icon: #878787;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-6 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-6 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-6 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-6 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: flex;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "  align-items: center;\n",
       "  justify-content: center;\n",
       "  gap: 0.5em;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 label.sk-toggleable__label .caption {\n",
       "  font-size: 0.6rem;\n",
       "  font-weight: lighter;\n",
       "  color: var(--sklearn-color-text-muted);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-6 div.sk-toggleable__content {\n",
       "  display: none;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  display: block;\n",
       "  width: 100%;\n",
       "  overflow: visible;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-6 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-6 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-6 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-6 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-6 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-6 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-6 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-6 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-6 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 0.5em;\n",
       "  text-align: center;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-3) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-0);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-0);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-6 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-6 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-6 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".estimator-table {\n",
       "    font-family: monospace;\n",
       "}\n",
       "\n",
       ".estimator-table summary {\n",
       "    padding: .5rem;\n",
       "    cursor: pointer;\n",
       "}\n",
       "\n",
       ".estimator-table summary::marker {\n",
       "    font-size: 0.7rem;\n",
       "}\n",
       "\n",
       ".estimator-table details[open] {\n",
       "    padding-left: 0.1rem;\n",
       "    padding-right: 0.1rem;\n",
       "    padding-bottom: 0.3rem;\n",
       "}\n",
       "\n",
       ".estimator-table .parameters-table {\n",
       "    margin-left: auto !important;\n",
       "    margin-right: auto !important;\n",
       "    margin-top: 0;\n",
       "}\n",
       "\n",
       ".estimator-table .parameters-table tr:nth-child(odd) {\n",
       "    background-color: #fff;\n",
       "}\n",
       "\n",
       ".estimator-table .parameters-table tr:nth-child(even) {\n",
       "    background-color: #f6f6f6;\n",
       "}\n",
       "\n",
       ".estimator-table .parameters-table tr:hover {\n",
       "    background-color: #e0e0e0;\n",
       "}\n",
       "\n",
       ".estimator-table table td {\n",
       "    border: 1px solid rgba(106, 105, 104, 0.232);\n",
       "}\n",
       "\n",
       "/*\n",
       "    `table td`is set in notebook with right text-align.\n",
       "    We need to overwrite it.\n",
       "*/\n",
       ".estimator-table table td.param {\n",
       "    text-align: left;\n",
       "    position: relative;\n",
       "    padding: 0;\n",
       "}\n",
       "\n",
       ".user-set td {\n",
       "    color:rgb(255, 94, 0);\n",
       "    text-align: left !important;\n",
       "}\n",
       "\n",
       ".user-set td.value {\n",
       "    color:rgb(255, 94, 0);\n",
       "    background-color: transparent;\n",
       "}\n",
       "\n",
       ".default td {\n",
       "    color: black;\n",
       "    text-align: left !important;\n",
       "}\n",
       "\n",
       ".user-set td i,\n",
       ".default td i {\n",
       "    color: black;\n",
       "}\n",
       "\n",
       "/*\n",
       "    Styles for parameter documentation links\n",
       "    We need styling for visited so jupyter doesn't overwrite it\n",
       "*/\n",
       "a.param-doc-link,\n",
       "a.param-doc-link:link,\n",
       "a.param-doc-link:visited {\n",
       "    text-decoration: underline dashed;\n",
       "    text-underline-offset: .3em;\n",
       "    color: inherit;\n",
       "    display: block;\n",
       "    padding: .5em;\n",
       "}\n",
       "\n",
       "/* \"hack\" to make the entire area of the cell containing the link clickable */\n",
       "a.param-doc-link::before {\n",
       "    position: absolute;\n",
       "    content: \"\";\n",
       "    inset: 0;\n",
       "}\n",
       "\n",
       ".param-doc-description {\n",
       "    display: none;\n",
       "    position: absolute;\n",
       "    z-index: 9999;\n",
       "    left: 0;\n",
       "    padding: .5ex;\n",
       "    margin-left: 1.5em;\n",
       "    color: var(--sklearn-color-text);\n",
       "    box-shadow: .3em .3em .4em #999;\n",
       "    width: max-content;\n",
       "    text-align: left;\n",
       "    max-height: 10em;\n",
       "    overflow-y: auto;\n",
       "\n",
       "    /* unfitted */\n",
       "    background: var(--sklearn-color-unfitted-level-0);\n",
       "    border: thin solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       "/* Fitted state for parameter tooltips */\n",
       ".fitted .param-doc-description {\n",
       "    /* fitted */\n",
       "    background: var(--sklearn-color-fitted-level-0);\n",
       "    border: thin solid var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".param-doc-link:hover .param-doc-description {\n",
       "    display: block;\n",
       "}\n",
       "\n",
       ".copy-paste-icon {\n",
       "    background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NDggNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjUgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTIwOCAwTDMzMi4xIDBjMTIuNyAwIDI0LjkgNS4xIDMzLjkgMTQuMWw2Ny45IDY3LjljOSA5IDE0LjEgMjEuMiAxNC4xIDMzLjlMNDQ4IDMzNmMwIDI2LjUtMjEuNSA0OC00OCA0OGwtMTkyIDBjLTI2LjUgMC00OC0yMS41LTQ4LTQ4bDAtMjg4YzAtMjYuNSAyMS41LTQ4IDQ4LTQ4ek00OCAxMjhsODAgMCAwIDY0LTY0IDAgMCAyNTYgMTkyIDAgMC0zMiA2NCAwIDAgNDhjMCAyNi41LTIxLjUgNDgtNDggNDhMNDggNTEyYy0yNi41IDAtNDgtMjEuNS00OC00OEwwIDE3NmMwLTI2LjUgMjEuNS00OCA0OC00OHoiLz48L3N2Zz4=);\n",
       "    background-repeat: no-repeat;\n",
       "    background-size: 14px 14px;\n",
       "    background-position: 0;\n",
       "    display: inline-block;\n",
       "    width: 14px;\n",
       "    height: 14px;\n",
       "    cursor: pointer;\n",
       "}\n",
       "</style><body><div id=\"sk-container-id-6\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LinearRegression()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-6\" type=\"checkbox\" checked><label for=\"sk-estimator-id-6\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>LinearRegression</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.linear_model.LinearRegression.html\">?<span>Documentation for LinearRegression</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\" data-param-prefix=\"\">\n",
       "        <div class=\"estimator-table\">\n",
       "            <details>\n",
       "                <summary>Parameters</summary>\n",
       "                <table class=\"parameters-table\">\n",
       "                  <tbody>\n",
       "                    \n",
       "        <tr class=\"default\">\n",
       "            <td><i class=\"copy-paste-icon\"\n",
       "                 onclick=\"copyToClipboard('fit_intercept',\n",
       "                          this.parentElement.nextElementSibling)\"\n",
       "            ></i></td>\n",
       "            <td class=\"param\">\n",
       "        <a class=\"param-doc-link\"\n",
       "            rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.linear_model.LinearRegression.html#:~:text=fit_intercept,-bool%2C%20default%3DTrue\">\n",
       "            fit_intercept\n",
       "            <span class=\"param-doc-description\">fit_intercept: bool, default=True<br><br>Whether to calculate the intercept for this model. If set<br>to False, no intercept will be used in calculations<br>(i.e. data is expected to be centered).</span>\n",
       "        </a>\n",
       "    </td>\n",
       "            <td class=\"value\">True</td>\n",
       "        </tr>\n",
       "    \n",
       "\n",
       "        <tr class=\"default\">\n",
       "            <td><i class=\"copy-paste-icon\"\n",
       "                 onclick=\"copyToClipboard('copy_X',\n",
       "                          this.parentElement.nextElementSibling)\"\n",
       "            ></i></td>\n",
       "            <td class=\"param\">\n",
       "        <a class=\"param-doc-link\"\n",
       "            rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.linear_model.LinearRegression.html#:~:text=copy_X,-bool%2C%20default%3DTrue\">\n",
       "            copy_X\n",
       "            <span class=\"param-doc-description\">copy_X: bool, default=True<br><br>If True, X will be copied; else, it may be overwritten.</span>\n",
       "        </a>\n",
       "    </td>\n",
       "            <td class=\"value\">True</td>\n",
       "        </tr>\n",
       "    \n",
       "\n",
       "        <tr class=\"default\">\n",
       "            <td><i class=\"copy-paste-icon\"\n",
       "                 onclick=\"copyToClipboard('tol',\n",
       "                          this.parentElement.nextElementSibling)\"\n",
       "            ></i></td>\n",
       "            <td class=\"param\">\n",
       "        <a class=\"param-doc-link\"\n",
       "            rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.linear_model.LinearRegression.html#:~:text=tol,-float%2C%20default%3D1e-6\">\n",
       "            tol\n",
       "            <span class=\"param-doc-description\">tol: float, default=1e-6<br><br>The precision of the solution (`coef_`) is determined by `tol` which<br>specifies a different convergence criterion for the `lsqr` solver.<br>`tol` is set as `atol` and `btol` of :func:`scipy.sparse.linalg.lsqr` when<br>fitting on sparse training data. This parameter has no effect when fitting<br>on dense data.<br><br>.. versionadded:: 1.7</span>\n",
       "        </a>\n",
       "    </td>\n",
       "            <td class=\"value\">1e-06</td>\n",
       "        </tr>\n",
       "    \n",
       "\n",
       "        <tr class=\"default\">\n",
       "            <td><i class=\"copy-paste-icon\"\n",
       "                 onclick=\"copyToClipboard('n_jobs',\n",
       "                          this.parentElement.nextElementSibling)\"\n",
       "            ></i></td>\n",
       "            <td class=\"param\">\n",
       "        <a class=\"param-doc-link\"\n",
       "            rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.linear_model.LinearRegression.html#:~:text=n_jobs,-int%2C%20default%3DNone\">\n",
       "            n_jobs\n",
       "            <span class=\"param-doc-description\">n_jobs: int, default=None<br><br>The number of jobs to use for the computation. This will only provide<br>speedup in case of sufficiently large problems, that is if firstly<br>`n_targets > 1` and secondly `X` is sparse or if `positive` is set<br>to `True`. ``None`` means 1 unless in a<br>:obj:`joblib.parallel_backend` context. ``-1`` means using all<br>processors. See :term:`Glossary <n_jobs>` for more details.</span>\n",
       "        </a>\n",
       "    </td>\n",
       "            <td class=\"value\">None</td>\n",
       "        </tr>\n",
       "    \n",
       "\n",
       "        <tr class=\"default\">\n",
       "            <td><i class=\"copy-paste-icon\"\n",
       "                 onclick=\"copyToClipboard('positive',\n",
       "                          this.parentElement.nextElementSibling)\"\n",
       "            ></i></td>\n",
       "            <td class=\"param\">\n",
       "        <a class=\"param-doc-link\"\n",
       "            rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.linear_model.LinearRegression.html#:~:text=positive,-bool%2C%20default%3DFalse\">\n",
       "            positive\n",
       "            <span class=\"param-doc-description\">positive: bool, default=False<br><br>When set to ``True``, forces the coefficients to be positive. This<br>option is only supported for dense arrays.<br><br>For a comparison between a linear regression model with positive constraints<br>on the regression coefficients and a linear regression without such constraints,<br>see :ref:`sphx_glr_auto_examples_linear_model_plot_nnls.py`.<br><br>.. versionadded:: 0.24</span>\n",
       "        </a>\n",
       "    </td>\n",
       "            <td class=\"value\">False</td>\n",
       "        </tr>\n",
       "    \n",
       "                  </tbody>\n",
       "                </table>\n",
       "            </details>\n",
       "        </div>\n",
       "    </div></div></div></div></div><script>function copyToClipboard(text, element) {\n",
       "    // Get the parameter prefix from the closest toggleable content\n",
       "    const toggleableContent = element.closest('.sk-toggleable__content');\n",
       "    const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
       "    const fullParamName = paramPrefix ? `${paramPrefix}${text}` : text;\n",
       "\n",
       "    const originalStyle = element.style;\n",
       "    const computedStyle = window.getComputedStyle(element);\n",
       "    const originalWidth = computedStyle.width;\n",
       "    const originalHTML = element.innerHTML.replace('Copied!', '');\n",
       "\n",
       "    navigator.clipboard.writeText(fullParamName)\n",
       "        .then(() => {\n",
       "            element.style.width = originalWidth;\n",
       "            element.style.color = 'green';\n",
       "            element.innerHTML = \"Copied!\";\n",
       "\n",
       "            setTimeout(() => {\n",
       "                element.innerHTML = originalHTML;\n",
       "                element.style = originalStyle;\n",
       "            }, 2000);\n",
       "        })\n",
       "        .catch(err => {\n",
       "            console.error('Failed to copy:', err);\n",
       "            element.style.color = 'red';\n",
       "            element.innerHTML = \"Failed!\";\n",
       "            setTimeout(() => {\n",
       "                element.innerHTML = originalHTML;\n",
       "                element.style = originalStyle;\n",
       "            }, 2000);\n",
       "        });\n",
       "    return false;\n",
       "}\n",
       "\n",
       "document.querySelectorAll('.copy-paste-icon').forEach(function(element) {\n",
       "    const toggleableContent = element.closest('.sk-toggleable__content');\n",
       "    const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
       "    const paramName = element.parentElement.nextElementSibling\n",
       "        .textContent.trim().split(' ')[0];\n",
       "    const fullParamName = paramPrefix ? `${paramPrefix}${paramName}` : paramName;\n",
       "\n",
       "    element.setAttribute('title', fullParamName);\n",
       "});\n",
       "\n",
       "\n",
       "/**\n",
       " * Adapted from Skrub\n",
       " * https://github.com/skrub-data/skrub/blob/403466d1d5d4dc76a7ef569b3f8228db59a31dc3/skrub/_reporting/_data/templates/report.js#L789\n",
       " * @returns \"light\" or \"dark\"\n",
       " */\n",
       "function detectTheme(element) {\n",
       "    const body = document.querySelector('body');\n",
       "\n",
       "    // Check VSCode theme\n",
       "    const themeKindAttr = body.getAttribute('data-vscode-theme-kind');\n",
       "    const themeNameAttr = body.getAttribute('data-vscode-theme-name');\n",
       "\n",
       "    if (themeKindAttr && themeNameAttr) {\n",
       "        const themeKind = themeKindAttr.toLowerCase();\n",
       "        const themeName = themeNameAttr.toLowerCase();\n",
       "\n",
       "        if (themeKind.includes(\"dark\") || themeName.includes(\"dark\")) {\n",
       "            return \"dark\";\n",
       "        }\n",
       "        if (themeKind.includes(\"light\") || themeName.includes(\"light\")) {\n",
       "            return \"light\";\n",
       "        }\n",
       "    }\n",
       "\n",
       "    // Check Jupyter theme\n",
       "    if (body.getAttribute('data-jp-theme-light') === 'false') {\n",
       "        return 'dark';\n",
       "    } else if (body.getAttribute('data-jp-theme-light') === 'true') {\n",
       "        return 'light';\n",
       "    }\n",
       "\n",
       "    // Guess based on a parent element's color\n",
       "    const color = window.getComputedStyle(element.parentNode, null).getPropertyValue('color');\n",
       "    const match = color.match(/^rgb\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)\\s*$/i);\n",
       "    if (match) {\n",
       "        const [r, g, b] = [\n",
       "            parseFloat(match[1]),\n",
       "            parseFloat(match[2]),\n",
       "            parseFloat(match[3])\n",
       "        ];\n",
       "\n",
       "        // https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness\n",
       "        const luma = 0.299 * r + 0.587 * g + 0.114 * b;\n",
       "\n",
       "        if (luma > 180) {\n",
       "            // If the text is very bright we have a dark theme\n",
       "            return 'dark';\n",
       "        }\n",
       "        if (luma < 75) {\n",
       "            // If the text is very dark we have a light theme\n",
       "            return 'light';\n",
       "        }\n",
       "        // Otherwise fall back to the next heuristic.\n",
       "    }\n",
       "\n",
       "    // Fallback to system preference\n",
       "    return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';\n",
       "}\n",
       "\n",
       "\n",
       "function forceTheme(elementId) {\n",
       "    const estimatorElement = document.querySelector(`#${elementId}`);\n",
       "    if (estimatorElement === null) {\n",
       "        console.error(`Element with id ${elementId} not found.`);\n",
       "    } else {\n",
       "        const theme = detectTheme(estimatorElement);\n",
       "        estimatorElement.classList.add(theme);\n",
       "    }\n",
       "}\n",
       "\n",
       "forceTheme('sk-container-id-6');</script></body>"
      ],
      "text/plain": [
       "LinearRegression()"
      ]
     },
     "execution_count": 453,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = LinearRegression()\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 455,
   "id": "5bfb5975-f2e2-497d-946e-5c7ce211dc85",
   "metadata": {},
   "outputs": [],
   "source": [
    "preds = model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 457,
   "id": "510e10a9-31cb-44ab-bfb8-374ddaf5f3e5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RMSE: 1.8412141156327049\n",
      "R2: 0.23028675785045494\n"
     ]
    }
   ],
   "source": [
    "print(\"RMSE:\", np.sqrt(mean_squared_error(y_test, preds)))\n",
    "print(\"R2:\", r2_score(y_test, preds))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 459,
   "id": "f82ff8e3-6cde-4660-95a4-19b60406bc0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RF RMSE: 1.3016674515473314\n",
      "RF R2: 0.6153015314103947\n"
     ]
    }
   ],
   "source": [
    "rf = RandomForestRegressor(n_estimators=100, random_state=42)\n",
    "rf.fit(X_train, y_train)\n",
    "\n",
    "preds_rf = rf.predict(X_test)\n",
    "\n",
    "print(\"RF RMSE:\", np.sqrt(mean_squared_error(y_test, preds_rf)))\n",
    "print(\"RF R2:\", r2_score(y_test, preds_rf))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 460,
   "id": "e3a21067-eefc-4dad-a1c9-b57233aa2862",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tags_length               0.195252\n",
      "publish_country_FRANCE    0.179267\n",
      "publish_hour              0.134345\n",
      "tags_count                0.130771\n",
      "category_id               0.110313\n",
      "days_to_trend             0.109666\n",
      "publish_month             0.077705\n",
      "is_weekend                0.022079\n",
      "trend_score               0.019728\n",
      "publish_country_US        0.011847\n",
      "publish_country_GB        0.009027\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "importances = pd.Series(\n",
    "    rf.feature_importances_,\n",
    "    index=X.columns\n",
    ").sort_values(ascending=False)\n",
    "\n",
    "print(importances.head(11))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f814eaee-4256-428f-89b9-035cf0024ad6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11 (NER)",
   "language": "python",
   "name": "ner_env"
  },
  "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.11.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
