CoolFace
Apppublic

Sadmanul/Collaborative-Filtering-Recommender-System

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
code.ipynb5939 linesDownload Raw Back to root
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 1,6   "id": "4f242ff6",7   "metadata": {},8   "outputs": [],9   "source": [10    "import pandas as pd\n",11    "import numpy as np\n",12    "import matplotlib.pyplot as plt"13   ]14  },15  {16   "cell_type": "code",17   "execution_count": 2,18   "id": "b7d6be03",19   "metadata": {},20   "outputs": [21    {22     "name": "stderr",23     "output_type": "stream",24     "text": [25      "C:\\Users\\sadma\\AppData\\Local\\Temp\\ipykernel_16112\\2952862037.py:5: DtypeWarning: Columns (3) have mixed types. Specify dtype option on import or set low_memory=False.\n",26      "  books = pd.read_csv(url1, encoding='latin-1')\n"27     ]28    },29    {30     "data": {31      "text/html": [32       "<div>\n",33       "<style scoped>\n",34       "    .dataframe tbody tr th:only-of-type {\n",35       "        vertical-align: middle;\n",36       "    }\n",37       "\n",38       "    .dataframe tbody tr th {\n",39       "        vertical-align: top;\n",40       "    }\n",41       "\n",42       "    .dataframe thead th {\n",43       "        text-align: right;\n",44       "    }\n",45       "</style>\n",46       "<table border=\"1\" class=\"dataframe\">\n",47       "  <thead>\n",48       "    <tr style=\"text-align: right;\">\n",49       "      <th></th>\n",50       "      <th>ISBN</th>\n",51       "      <th>Book-Title</th>\n",52       "      <th>Book-Author</th>\n",53       "      <th>Year-Of-Publication</th>\n",54       "      <th>Publisher</th>\n",55       "      <th>Image-URL-S</th>\n",56       "      <th>Image-URL-M</th>\n",57       "      <th>Image-URL-L</th>\n",58       "    </tr>\n",59       "  </thead>\n",60       "  <tbody>\n",61       "    <tr>\n",62       "      <th>0</th>\n",63       "      <td>0195153448</td>\n",64       "      <td>Classical Mythology</td>\n",65       "      <td>Mark P. O. Morford</td>\n",66       "      <td>2002</td>\n",67       "      <td>Oxford University Press</td>\n",68       "      <td>http://images.amazon.com/images/P/0195153448.0...</td>\n",69       "      <td>http://images.amazon.com/images/P/0195153448.0...</td>\n",70       "      <td>http://images.amazon.com/images/P/0195153448.0...</td>\n",71       "    </tr>\n",72       "    <tr>\n",73       "      <th>1</th>\n",74       "      <td>0002005018</td>\n",75       "      <td>Clara Callan</td>\n",76       "      <td>Richard Bruce Wright</td>\n",77       "      <td>2001</td>\n",78       "      <td>HarperFlamingo Canada</td>\n",79       "      <td>http://images.amazon.com/images/P/0002005018.0...</td>\n",80       "      <td>http://images.amazon.com/images/P/0002005018.0...</td>\n",81       "      <td>http://images.amazon.com/images/P/0002005018.0...</td>\n",82       "    </tr>\n",83       "    <tr>\n",84       "      <th>2</th>\n",85       "      <td>0060973129</td>\n",86       "      <td>Decision in Normandy</td>\n",87       "      <td>Carlo D'Este</td>\n",88       "      <td>1991</td>\n",89       "      <td>HarperPerennial</td>\n",90       "      <td>http://images.amazon.com/images/P/0060973129.0...</td>\n",91       "      <td>http://images.amazon.com/images/P/0060973129.0...</td>\n",92       "      <td>http://images.amazon.com/images/P/0060973129.0...</td>\n",93       "    </tr>\n",94       "    <tr>\n",95       "      <th>3</th>\n",96       "      <td>0374157065</td>\n",97       "      <td>Flu: The Story of the Great Influenza Pandemic...</td>\n",98       "      <td>Gina Bari Kolata</td>\n",99       "      <td>1999</td>\n",100       "      <td>Farrar Straus Giroux</td>\n",101       "      <td>http://images.amazon.com/images/P/0374157065.0...</td>\n",102       "      <td>http://images.amazon.com/images/P/0374157065.0...</td>\n",103       "      <td>http://images.amazon.com/images/P/0374157065.0...</td>\n",104       "    </tr>\n",105       "    <tr>\n",106       "      <th>4</th>\n",107       "      <td>0393045218</td>\n",108       "      <td>The Mummies of Urumchi</td>\n",109       "      <td>E. J. W. Barber</td>\n",110       "      <td>1999</td>\n",111       "      <td>W. W. Norton &amp;amp; Company</td>\n",112       "      <td>http://images.amazon.com/images/P/0393045218.0...</td>\n",113       "      <td>http://images.amazon.com/images/P/0393045218.0...</td>\n",114       "      <td>http://images.amazon.com/images/P/0393045218.0...</td>\n",115       "    </tr>\n",116       "  </tbody>\n",117       "</table>\n",118       "</div>"119      ],120      "text/plain": [121       "         ISBN                                         Book-Title  \\\n",122       "0  0195153448                                Classical Mythology   \n",123       "1  0002005018                                       Clara Callan   \n",124       "2  0060973129                               Decision in Normandy   \n",125       "3  0374157065  Flu: The Story of the Great Influenza Pandemic...   \n",126       "4  0393045218                             The Mummies of Urumchi   \n",127       "\n",128       "            Book-Author Year-Of-Publication                   Publisher  \\\n",129       "0    Mark P. O. Morford                2002     Oxford University Press   \n",130       "1  Richard Bruce Wright                2001       HarperFlamingo Canada   \n",131       "2          Carlo D'Este                1991             HarperPerennial   \n",132       "3      Gina Bari Kolata                1999        Farrar Straus Giroux   \n",133       "4       E. J. W. Barber                1999  W. W. Norton &amp; Company   \n",134       "\n",135       "                                         Image-URL-S  \\\n",136       "0  http://images.amazon.com/images/P/0195153448.0...   \n",137       "1  http://images.amazon.com/images/P/0002005018.0...   \n",138       "2  http://images.amazon.com/images/P/0060973129.0...   \n",139       "3  http://images.amazon.com/images/P/0374157065.0...   \n",140       "4  http://images.amazon.com/images/P/0393045218.0...   \n",141       "\n",142       "                                         Image-URL-M  \\\n",143       "0  http://images.amazon.com/images/P/0195153448.0...   \n",144       "1  http://images.amazon.com/images/P/0002005018.0...   \n",145       "2  http://images.amazon.com/images/P/0060973129.0...   \n",146       "3  http://images.amazon.com/images/P/0374157065.0...   \n",147       "4  http://images.amazon.com/images/P/0393045218.0...   \n",148       "\n",149       "                                         Image-URL-L  \n",150       "0  http://images.amazon.com/images/P/0195153448.0...  \n",151       "1  http://images.amazon.com/images/P/0002005018.0...  \n",152       "2  http://images.amazon.com/images/P/0060973129.0...  \n",153       "3  http://images.amazon.com/images/P/0374157065.0...  \n",154       "4  http://images.amazon.com/images/P/0393045218.0...  "155      ]156     },157     "execution_count": 2,158     "metadata": {},159     "output_type": "execute_result"160    }161   ],162   "source": [163    "url1 = \"datasets/books.csv\"\n",164    "url2 = \"datasets/ratings.csv\"\n",165    "url3 = \"datasets/users.csv\"\n",166    "\n",167    "books = pd.read_csv(url1, encoding='latin-1')\n",168    "ratings = pd.read_csv(url2, encoding='latin-1')\n",169    "users = pd.read_csv(url3, encoding='latin-1')\n",170    "books.head()"171   ]172  },173  {174   "cell_type": "code",175   "execution_count": 3,176   "id": "7dcac8bd",177   "metadata": {},178   "outputs": [179    {180     "data": {181      "text/html": [182       "<div>\n",183       "<style scoped>\n",184       "    .dataframe tbody tr th:only-of-type {\n",185       "        vertical-align: middle;\n",186       "    }\n",187       "\n",188       "    .dataframe tbody tr th {\n",189       "        vertical-align: top;\n",190       "    }\n",191       "\n",192       "    .dataframe thead th {\n",193       "        text-align: right;\n",194       "    }\n",195       "</style>\n",196       "<table border=\"1\" class=\"dataframe\">\n",197       "  <thead>\n",198       "    <tr style=\"text-align: right;\">\n",199       "      <th></th>\n",200       "      <th>User-ID</th>\n",201       "      <th>ISBN</th>\n",202       "      <th>Book-Rating</th>\n",203       "    </tr>\n",204       "  </thead>\n",205       "  <tbody>\n",206       "    <tr>\n",207       "      <th>0</th>\n",208       "      <td>276725</td>\n",209       "      <td>034545104X</td>\n",210       "      <td>0</td>\n",211       "    </tr>\n",212       "    <tr>\n",213       "      <th>1</th>\n",214       "      <td>276726</td>\n",215       "      <td>0155061224</td>\n",216       "      <td>5</td>\n",217       "    </tr>\n",218       "    <tr>\n",219       "      <th>2</th>\n",220       "      <td>276727</td>\n",221       "      <td>0446520802</td>\n",222       "      <td>0</td>\n",223       "    </tr>\n",224       "    <tr>\n",225       "      <th>3</th>\n",226       "      <td>276729</td>\n",227       "      <td>052165615X</td>\n",228       "      <td>3</td>\n",229       "    </tr>\n",230       "    <tr>\n",231       "      <th>4</th>\n",232       "      <td>276729</td>\n",233       "      <td>0521795028</td>\n",234       "      <td>6</td>\n",235       "    </tr>\n",236       "  </tbody>\n",237       "</table>\n",238       "</div>"239      ],240      "text/plain": [241       "   User-ID        ISBN  Book-Rating\n",242       "0   276725  034545104X            0\n",243       "1   276726  0155061224            5\n",244       "2   276727  0446520802            0\n",245       "3   276729  052165615X            3\n",246       "4   276729  0521795028            6"247      ]248     },249     "execution_count": 3,250     "metadata": {},251     "output_type": "execute_result"252    }253   ],254   "source": [255    "ratings.head()"256   ]257  },258  {259   "cell_type": "code",260   "execution_count": 4,261   "id": "87afa6a4",262   "metadata": {},263   "outputs": [264    {265     "data": {266      "text/html": [267       "<div>\n",268       "<style scoped>\n",269       "    .dataframe tbody tr th:only-of-type {\n",270       "        vertical-align: middle;\n",271       "    }\n",272       "\n",273       "    .dataframe tbody tr th {\n",274       "        vertical-align: top;\n",275       "    }\n",276       "\n",277       "    .dataframe thead th {\n",278       "        text-align: right;\n",279       "    }\n",280       "</style>\n",281       "<table border=\"1\" class=\"dataframe\">\n",282       "  <thead>\n",283       "    <tr style=\"text-align: right;\">\n",284       "      <th></th>\n",285       "      <th>User-ID</th>\n",286       "      <th>Location</th>\n",287       "      <th>Age</th>\n",288       "    </tr>\n",289       "  </thead>\n",290       "  <tbody>\n",291       "    <tr>\n",292       "      <th>0</th>\n",293       "      <td>1</td>\n",294       "      <td>nyc, new york, usa</td>\n",295       "      <td>NaN</td>\n",296       "    </tr>\n",297       "    <tr>\n",298       "      <th>1</th>\n",299       "      <td>2</td>\n",300       "      <td>stockton, california, usa</td>\n",301       "      <td>18.0</td>\n",302       "    </tr>\n",303       "    <tr>\n",304       "      <th>2</th>\n",305       "      <td>3</td>\n",306       "      <td>moscow, yukon territory, russia</td>\n",307       "      <td>NaN</td>\n",308       "    </tr>\n",309       "    <tr>\n",310       "      <th>3</th>\n",311       "      <td>4</td>\n",312       "      <td>porto, v.n.gaia, portugal</td>\n",313       "      <td>17.0</td>\n",314       "    </tr>\n",315       "    <tr>\n",316       "      <th>4</th>\n",317       "      <td>5</td>\n",318       "      <td>farnborough, hants, united kingdom</td>\n",319       "      <td>NaN</td>\n",320       "    </tr>\n",321       "  </tbody>\n",322       "</table>\n",323       "</div>"324      ],325      "text/plain": [326       "   User-ID                            Location   Age\n",327       "0        1                  nyc, new york, usa   NaN\n",328       "1        2           stockton, california, usa  18.0\n",329       "2        3     moscow, yukon territory, russia   NaN\n",330       "3        4           porto, v.n.gaia, portugal  17.0\n",331       "4        5  farnborough, hants, united kingdom   NaN"332      ]333     },334     "execution_count": 4,335     "metadata": {},336     "output_type": "execute_result"337    }338   ],339   "source": [340    "users.head()"341   ]342  },343  {344   "cell_type": "code",345   "execution_count": 5,346   "id": "a4776127",347   "metadata": {},348   "outputs": [],349   "source": [350    "books.columns = books.columns.str.lower().str.replace(\" \", \"_\").str.replace(\"-\", \"_\")\n",351    "ratings.columns = ratings.columns.str.lower().str.replace(\" \", \"_\").str.replace(\"-\", \"_\")\n",352    "users.columns = users.columns.str.lower().str.replace(\" \", \"_\").str.replace(\"-\", \"_\")"353   ]354  },355  {356   "cell_type": "code",357   "execution_count": 6,358   "id": "6ccc16e7",359   "metadata": {},360   "outputs": [361    {362     "data": {363      "text/plain": [364       "Index(['isbn', 'book_name', 'author', 'year_of_publication', 'publisher',\n",365       "       'image_url_s', 'image_url_m', 'image_url'],\n",366       "      dtype='object')"367      ]368     },369     "execution_count": 6,370     "metadata": {},371     "output_type": "execute_result"372    }373   ],374   "source": [375    "books.rename(columns={\n",376    "    \"book_title\": \"book_name\",\n",377    "    \"book_author\": \"author\",\n",378    "    \"image_url_l\": \"image_url\"\n",379    "    }, inplace=True)\n",380    "books.columns"381   ]382  },383  {384   "cell_type": "code",385   "execution_count": 7,386   "id": "509db8c9",387   "metadata": {},388   "outputs": [389    {390     "data": {391      "text/plain": [392       "Index(['isbn', 'book_name', 'author', 'publisher', 'image_url'], dtype='object')"393      ]394     },395     "execution_count": 7,396     "metadata": {},397     "output_type": "execute_result"398    }399   ],400   "source": [401    "books.drop([\n",402    "    \"image_url_s\",\n",403    "    \"image_url_m\",\n",404    "    \"year_of_publication\"\n",405    "    ], axis=1, inplace=True)\n",406    "books.columns"407   ]408  },409  {410   "cell_type": "code",411   "execution_count": 8,412   "id": "74a80e62",413   "metadata": {},414   "outputs": [415    {416     "data": {417      "text/plain": [418       "Index(['user_id', 'isbn', 'ratings'], dtype='object')"419      ]420     },421     "execution_count": 8,422     "metadata": {},423     "output_type": "execute_result"424    }425   ],426   "source": [427    "ratings.rename(columns={\n",428    "    \"book_rating\": \"ratings\"\n",429    "    }, inplace=True)\n",430    "ratings.columns\n"431   ]432  },433  {434   "cell_type": "code",435   "execution_count": 9,436   "id": "45376706",437   "metadata": {},438   "outputs": [439    {440     "data": {441      "text/plain": [442       "Index(['user_id', 'location'], dtype='object')"443      ]444     },445     "execution_count": 9,446     "metadata": {},447     "output_type": "execute_result"448    }449   ],450   "source": [451    "users.drop(columns={\n",452    "    \"age\"\n",453    "    }, inplace=True)\n",454    "users.columns"455   ]456  },457  {458   "cell_type": "code",459   "execution_count": 10,460   "id": "cfa6a8e0",461   "metadata": {},462   "outputs": [463    {464     "name": "stdout",465     "output_type": "stream",466     "text": [467      "(271360, 5)\n",468      "(1149780, 3)\n",469      "(278858, 2)\n"470     ]471    }472   ],473   "source": [474    "print(books.shape)\n",475    "print(ratings.shape)\n",476    "print(users.shape)"477   ]478  },479  {480   "cell_type": "markdown",481   "id": "2bc1b7f7",482   "metadata": {},483   "source": [484    "#### There are 271360 books \n",485    "#### There are 278858 users\n",486    "#### 278858 users rated 1149780"487   ]488  },489  {490   "cell_type": "code",491   "execution_count": 11,492   "id": "2b21229a",493   "metadata": {},494   "outputs": [495    {496     "data": {497      "text/plain": [498       "isbn         0\n",499       "book_name    0\n",500       "author       2\n",501       "publisher    2\n",502       "image_url    3\n",503       "dtype: int64"504      ]505     },506     "execution_count": 11,507     "metadata": {},508     "output_type": "execute_result"509    }510   ],511   "source": [512    "books.isnull().sum()"513   ]514  },515  {516   "cell_type": "code",517   "execution_count": 12,518   "id": "94d0373b",519   "metadata": {},520   "outputs": [],521   "source": [522    "books.dropna(inplace=True)"523   ]524  },525  {526   "cell_type": "code",527   "execution_count": 13,528   "id": "81b90706",529   "metadata": {},530   "outputs": [531    {532     "data": {533      "text/plain": [534       "isbn         0\n",535       "book_name    0\n",536       "author       0\n",537       "publisher    0\n",538       "image_url    0\n",539       "dtype: int64"540      ]541     },542     "execution_count": 13,543     "metadata": {},544     "output_type": "execute_result"545    }546   ],547   "source": [548    "books.isnull().sum()"549   ]550  },551  {552   "cell_type": "code",553   "execution_count": 14,554   "id": "e8e46e91",555   "metadata": {},556   "outputs": [557    {558     "data": {559      "text/plain": [560       "user_id    0\n",561       "isbn       0\n",562       "ratings    0\n",563       "dtype: int64"564      ]565     },566     "execution_count": 14,567     "metadata": {},568     "output_type": "execute_result"569    }570   ],571   "source": [572    "ratings.isnull().sum()"573   ]574  },575  {576   "cell_type": "code",577   "execution_count": 15,578   "id": "1fca21a7",579   "metadata": {},580   "outputs": [581    {582     "data": {583      "text/plain": [584       "user_id     0\n",585       "location    0\n",586       "dtype: int64"587      ]588     },589     "execution_count": 15,590     "metadata": {},591     "output_type": "execute_result"592    }593   ],594   "source": [595    "users.isnull().sum()"596   ]597  },598  {599   "cell_type": "code",600   "execution_count": 16,601   "id": "29e83d6e",602   "metadata": {},603   "outputs": [604    {605     "data": {606      "text/plain": [607       "np.int64(0)"608      ]609     },610     "execution_count": 16,611     "metadata": {},612     "output_type": "execute_result"613    }614   ],615   "source": [616    "books.duplicated().sum()"617   ]618  },619  {620   "cell_type": "code",621   "execution_count": 17,622   "id": "5fe84163",623   "metadata": {},624   "outputs": [625    {626     "data": {627      "text/plain": [628       "np.int64(0)"629      ]630     },631     "execution_count": 17,632     "metadata": {},633     "output_type": "execute_result"634    }635   ],636   "source": [637    "ratings.duplicated().sum()"638   ]639  },640  {641   "cell_type": "code",642   "execution_count": 18,643   "id": "10df484b",644   "metadata": {},645   "outputs": [646    {647     "data": {648      "text/plain": [649       "np.int64(0)"650      ]651     },652     "execution_count": 18,653     "metadata": {},654     "output_type": "execute_result"655    }656   ],657   "source": [658    "users.duplicated().sum()"659   ]660  },661  {662   "cell_type": "markdown",663   "id": "a2987762",664   "metadata": {},665   "source": [666    "## Step 1: Merge Rarings and Books Together"667   ]668  },669  {670   "cell_type": "code",671   "execution_count": 19,672   "id": "971f806a",673   "metadata": {},674   "outputs": [675    {676     "data": {677      "text/html": [678       "<div>\n",679       "<style scoped>\n",680       "    .dataframe tbody tr th:only-of-type {\n",681       "        vertical-align: middle;\n",682       "    }\n",683       "\n",684       "    .dataframe tbody tr th {\n",685       "        vertical-align: top;\n",686       "    }\n",687       "\n",688       "    .dataframe thead th {\n",689       "        text-align: right;\n",690       "    }\n",691       "</style>\n",692       "<table border=\"1\" class=\"dataframe\">\n",693       "  <thead>\n",694       "    <tr style=\"text-align: right;\">\n",695       "      <th></th>\n",696       "      <th>user_id</th>\n",697       "      <th>isbn</th>\n",698       "      <th>ratings</th>\n",699       "      <th>book_name</th>\n",700       "      <th>author</th>\n",701       "      <th>publisher</th>\n",702       "      <th>image_url</th>\n",703       "    </tr>\n",704       "  </thead>\n",705       "  <tbody>\n",706       "    <tr>\n",707       "      <th>0</th>\n",708       "      <td>276725</td>\n",709       "      <td>034545104X</td>\n",710       "      <td>0</td>\n",711       "      <td>Flesh Tones: A Novel</td>\n",712       "      <td>M. J. Rose</td>\n",713       "      <td>Ballantine Books</td>\n",714       "      <td>http://images.amazon.com/images/P/034545104X.0...</td>\n",715       "    </tr>\n",716       "    <tr>\n",717       "      <th>1</th>\n",718       "      <td>276726</td>\n",719       "      <td>0155061224</td>\n",720       "      <td>5</td>\n",721       "      <td>Rites of Passage</td>\n",722       "      <td>Judith Rae</td>\n",723       "      <td>Heinle</td>\n",724       "      <td>http://images.amazon.com/images/P/0155061224.0...</td>\n",725       "    </tr>\n",726       "    <tr>\n",727       "      <th>2</th>\n",728       "      <td>276727</td>\n",729       "      <td>0446520802</td>\n",730       "      <td>0</td>\n",731       "      <td>The Notebook</td>\n",732       "      <td>Nicholas Sparks</td>\n",733       "      <td>Warner Books</td>\n",734       "      <td>http://images.amazon.com/images/P/0446520802.0...</td>\n",735       "    </tr>\n",736       "    <tr>\n",737       "      <th>3</th>\n",738       "      <td>276729</td>\n",739       "      <td>052165615X</td>\n",740       "      <td>3</td>\n",741       "      <td>Help!: Level 1</td>\n",742       "      <td>Philip Prowse</td>\n",743       "      <td>Cambridge University Press</td>\n",744       "      <td>http://images.amazon.com/images/P/052165615X.0...</td>\n",745       "    </tr>\n",746       "    <tr>\n",747       "      <th>4</th>\n",748       "      <td>276729</td>\n",749       "      <td>0521795028</td>\n",750       "      <td>6</td>\n",751       "      <td>The Amsterdam Connection : Level 4 (Cambridge ...</td>\n",752       "      <td>Sue Leather</td>\n",753       "      <td>Cambridge University Press</td>\n",754       "      <td>http://images.amazon.com/images/P/0521795028.0...</td>\n",755       "    </tr>\n",756       "  </tbody>\n",757       "</table>\n",758       "</div>"759      ],760      "text/plain": [761       "   user_id        isbn  ratings  \\\n",762       "0   276725  034545104X        0   \n",763       "1   276726  0155061224        5   \n",764       "2   276727  0446520802        0   \n",765       "3   276729  052165615X        3   \n",766       "4   276729  0521795028        6   \n",767       "\n",768       "                                           book_name           author  \\\n",769       "0                               Flesh Tones: A Novel       M. J. Rose   \n",770       "1                                   Rites of Passage       Judith Rae   \n",771       "2                                       The Notebook  Nicholas Sparks   \n",772       "3                                     Help!: Level 1    Philip Prowse   \n",773       "4  The Amsterdam Connection : Level 4 (Cambridge ...      Sue Leather   \n",774       "\n",775       "                    publisher  \\\n",776       "0            Ballantine Books   \n",777       "1                      Heinle   \n",778       "2                Warner Books   \n",779       "3  Cambridge University Press   \n",780       "4  Cambridge University Press   \n",781       "\n",782       "                                           image_url  \n",783       "0  http://images.amazon.com/images/P/034545104X.0...  \n",784       "1  http://images.amazon.com/images/P/0155061224.0...  \n",785       "2  http://images.amazon.com/images/P/0446520802.0...  \n",786       "3  http://images.amazon.com/images/P/052165615X.0...  \n",787       "4  http://images.amazon.com/images/P/0521795028.0...  "788      ]789     },790     "execution_count": 19,791     "metadata": {},792     "output_type": "execute_result"793    }794   ],795   "source": [796    "ratings_with_books = ratings.merge(books, on=\"isbn\", how=\"inner\")\n",797    "ratings_with_books.head()"798   ]799  },800  {801   "cell_type": "code",802   "execution_count": 20,803   "id": "850feff9",804   "metadata": {},805   "outputs": [806    {807     "data": {808      "text/plain": [809       "(1031128, 7)"810      ]811     },812     "execution_count": 20,813     "metadata": {},814     "output_type": "execute_result"815    }816   ],817   "source": [818    "ratings_with_books.shape"819   ]820  },821  {822   "cell_type": "markdown",823   "id": "917999f1",824   "metadata": {},825   "source": [826    "## Step 2: find users who rated > 200 books\n"827   ]828  },829  {830   "cell_type": "code",831   "execution_count": 21,832   "id": "ed99caee",833   "metadata": {},834   "outputs": [835    {836     "data": {837      "text/html": [838       "<div>\n",839       "<style scoped>\n",840       "    .dataframe tbody tr th:only-of-type {\n",841       "        vertical-align: middle;\n",842       "    }\n",843       "\n",844       "    .dataframe tbody tr th {\n",845       "        vertical-align: top;\n",846       "    }\n",847       "\n",848       "    .dataframe thead th {\n",849       "        text-align: right;\n",850       "    }\n",851       "</style>\n",852       "<table border=\"1\" class=\"dataframe\">\n",853       "  <thead>\n",854       "    <tr style=\"text-align: right;\">\n",855       "      <th></th>\n",856       "      <th>isbn</th>\n",857       "      <th>ratings</th>\n",858       "      <th>book_name</th>\n",859       "      <th>author</th>\n",860       "      <th>publisher</th>\n",861       "      <th>image_url</th>\n",862       "    </tr>\n",863       "    <tr>\n",864       "      <th>user_id</th>\n",865       "      <th></th>\n",866       "      <th></th>\n",867       "      <th></th>\n",868       "      <th></th>\n",869       "      <th></th>\n",870       "      <th></th>\n",871       "    </tr>\n",872       "  </thead>\n",873       "  <tbody>\n",874       "    <tr>\n",875       "      <th>2</th>\n",876       "      <td>1</td>\n",877       "      <td>1</td>\n",878       "      <td>1</td>\n",879       "      <td>1</td>\n",880       "      <td>1</td>\n",881       "      <td>1</td>\n",882       "    </tr>\n",883       "    <tr>\n",884       "      <th>8</th>\n",885       "      <td>17</td>\n",886       "      <td>17</td>\n",887       "      <td>17</td>\n",888       "      <td>17</td>\n",889       "      <td>17</td>\n",890       "      <td>17</td>\n",891       "    </tr>\n",892       "    <tr>\n",893       "      <th>9</th>\n",894       "      <td>3</td>\n",895       "      <td>3</td>\n",896       "      <td>3</td>\n",897       "      <td>3</td>\n",898       "      <td>3</td>\n",899       "      <td>3</td>\n",900       "    </tr>\n",901       "    <tr>\n",902       "      <th>10</th>\n",903       "      <td>1</td>\n",904       "      <td>1</td>\n",905       "      <td>1</td>\n",906       "      <td>1</td>\n",907       "      <td>1</td>\n",908       "      <td>1</td>\n",909       "    </tr>\n",910       "    <tr>\n",911       "      <th>12</th>\n",912       "      <td>1</td>\n",913       "      <td>1</td>\n",914       "      <td>1</td>\n",915       "      <td>1</td>\n",916       "      <td>1</td>\n",917       "      <td>1</td>\n",918       "    </tr>\n",919       "    <tr>\n",920       "      <th>...</th>\n",921       "      <td>...</td>\n",922       "      <td>...</td>\n",923       "      <td>...</td>\n",924       "      <td>...</td>\n",925       "      <td>...</td>\n",926       "      <td>...</td>\n",927       "    </tr>\n",928       "    <tr>\n",929       "      <th>278846</th>\n",930       "      <td>1</td>\n",931       "      <td>1</td>\n",932       "      <td>1</td>\n",933       "      <td>1</td>\n",934       "      <td>1</td>\n",935       "      <td>1</td>\n",936       "    </tr>\n",937       "    <tr>\n",938       "      <th>278849</th>\n",939       "      <td>4</td>\n",940       "      <td>4</td>\n",941       "      <td>4</td>\n",942       "      <td>4</td>\n",943       "      <td>4</td>\n",944       "      <td>4</td>\n",945       "    </tr>\n",946       "    <tr>\n",947       "      <th>278851</th>\n",948       "      <td>23</td>\n",949       "      <td>23</td>\n",950       "      <td>23</td>\n",951       "      <td>23</td>\n",952       "      <td>23</td>\n",953       "      <td>23</td>\n",954       "    </tr>\n",955       "    <tr>\n",956       "      <th>278852</th>\n",957       "      <td>1</td>\n",958       "      <td>1</td>\n",959       "      <td>1</td>\n",960       "      <td>1</td>\n",961       "      <td>1</td>\n",962       "      <td>1</td>\n",963       "    </tr>\n",964       "    <tr>\n",965       "      <th>278854</th>\n",966       "      <td>8</td>\n",967       "      <td>8</td>\n",968       "      <td>8</td>\n",969       "      <td>8</td>\n",970       "      <td>8</td>\n",971       "      <td>8</td>\n",972       "    </tr>\n",973       "  </tbody>\n",974       "</table>\n",975       "<p>92106 rows × 6 columns</p>\n",976       "</div>"977      ],978      "text/plain": [979       "         isbn  ratings  book_name  author  publisher  image_url\n",980       "user_id                                                        \n",981       "2           1        1          1       1          1          1\n",982       "8          17       17         17      17         17         17\n",983       "9           3        3          3       3          3          3\n",984       "10          1        1          1       1          1          1\n",985       "12          1        1          1       1          1          1\n",986       "...       ...      ...        ...     ...        ...        ...\n",987       "278846      1        1          1       1          1          1\n",988       "278849      4        4          4       4          4          4\n",989       "278851     23       23         23      23         23         23\n",990       "278852      1        1          1       1          1          1\n",991       "278854      8        8          8       8          8          8\n",992       "\n",993       "[92106 rows x 6 columns]"994      ]995     },996     "execution_count": 21,997     "metadata": {},998     "output_type": "execute_result"999    }1000   ],1001   "source": [1002    "ratings_with_books.groupby(\"user_id\").count()"1003   ]1004  },1005  {1006   "cell_type": "code",1007   "execution_count": 22,1008   "id": "c6a046fb",1009   "metadata": {},1010   "outputs": [1011    {1012     "data": {1013      "text/plain": [1014       "user_id\n",1015       "2          1\n",1016       "8         17\n",1017       "9          3\n",1018       "10         1\n",1019       "12         1\n",1020       "          ..\n",1021       "278846     1\n",1022       "278849     4\n",1023       "278851    23\n",1024       "278852     1\n",1025       "278854     8\n",1026       "Name: ratings, Length: 92106, dtype: int64"1027      ]1028     },1029     "execution_count": 22,1030     "metadata": {},1031     "output_type": "execute_result"1032    }1033   ],1034   "source": [1035    "ratings_with_books.groupby(\"user_id\").count()[\"ratings\"]"1036   ]1037  },1038  {1039   "cell_type": "code",1040   "execution_count": 23,1041   "id": "01191e50",1042   "metadata": {},1043   "outputs": [1044    {1045     "data": {1046      "text/plain": [1047       "user_id\n",1048       "2         False\n",1049       "8         False\n",1050       "9         False\n",1051       "10        False\n",1052       "12        False\n",1053       "          ...  \n",1054       "278846    False\n",1055       "278849    False\n",1056       "278851    False\n",1057       "278852    False\n",1058       "278854    False\n",1059       "Name: ratings, Length: 92106, dtype: bool"1060      ]1061     },1062     "execution_count": 23,1063     "metadata": {},1064     "output_type": "execute_result"1065    }1066   ],1067   "source": [1068    "ratings_with_books.groupby(\"user_id\").count()[\"ratings\"] > 200"1069   ]1070  },1071  {1072   "cell_type": "code",1073   "execution_count": 24,1074   "id": "644e03b5",1075   "metadata": {},1076   "outputs": [1077    {1078     "data": {1079      "text/plain": [1080       "user_id\n",1081       "254       True\n",1082       "2276      True\n",1083       "2766      True\n",1084       "2977      True\n",1085       "3363      True\n",1086       "          ... \n",1087       "274308    True\n",1088       "275970    True\n",1089       "277427    True\n",1090       "277639    True\n",1091       "278418    True\n",1092       "Name: ratings, Length: 811, dtype: bool"1093      ]1094     },1095     "execution_count": 24,1096     "metadata": {},1097     "output_type": "execute_result"1098    }1099   ],1100   "source": [1101    "users_rated_books_more_then_200 = ratings_with_books.groupby(\"user_id\").count()[\"ratings\"] > 200\n",1102    "users_rated_books_more_then_200[users_rated_books_more_then_200]"1103   ]1104  },1105  {1106   "cell_type": "code",1107   "execution_count": 25,1108   "id": "3ccdf70f",1109   "metadata": {},1110   "outputs": [1111    {1112     "data": {1113      "text/plain": [1114       "Index([   254,   2276,   2766,   2977,   3363,   4017,   4385,   6251,   6323,\n",1115       "         6543,\n",1116       "       ...\n",1117       "       271705, 273979, 274004, 274061, 274301, 274308, 275970, 277427, 277639,\n",1118       "       278418],\n",1119       "      dtype='int64', name='user_id', length=811)"1120      ]1121     },1122     "execution_count": 25,1123     "metadata": {},1124     "output_type": "execute_result"1125    }1126   ],1127   "source": [1128    "users_rated_books_more_then_200[users_rated_books_more_then_200].index"1129   ]1130  },1131  {1132   "cell_type": "code",1133   "execution_count": 26,1134   "id": "89cad2e9",1135   "metadata": {},1136   "outputs": [1137    {1138     "data": {1139      "text/plain": [1140       "Index([   254,   2276,   2766,   2977,   3363,   4017,   4385,   6251,   6323,\n",1141       "         6543,\n",1142       "       ...\n",1143       "       271705, 273979, 274004, 274061, 274301, 274308, 275970, 277427, 277639,\n",1144       "       278418],\n",1145       "      dtype='int64', name='user_id', length=811)"1146      ]1147     },1148     "execution_count": 26,1149     "metadata": {},1150     "output_type": "execute_result"1151    }1152   ],1153   "source": [1154    "users_rated_books_more_then_200 = users_rated_books_more_then_200[users_rated_books_more_then_200].index\n",1155    "users_rated_books_more_then_200"1156   ]1157  },1158  {1159   "cell_type": "code",1160   "execution_count": 27,1161   "id": "bd361f98",1162   "metadata": {},1163   "outputs": [1164    {1165     "data": {1166      "text/plain": [1167       "0          False\n",1168       "1          False\n",1169       "2          False\n",1170       "3          False\n",1171       "4          False\n",1172       "           ...  \n",1173       "1031123    False\n",1174       "1031124    False\n",1175       "1031125    False\n",1176       "1031126    False\n",1177       "1031127    False\n",1178       "Name: user_id, Length: 1031128, dtype: bool"1179      ]1180     },1181     "execution_count": 27,1182     "metadata": {},1183     "output_type": "execute_result"1184    }1185   ],1186   "source": [1187    "ratings_with_books[\"user_id\"].isin(users_rated_books_more_then_200)"1188   ]1189  },1190  {1191   "cell_type": "code",1192   "execution_count": 28,1193   "id": "782b5ab0",1194   "metadata": {},1195   "outputs": [1196    {1197     "data": {1198      "text/html": [1199       "<div>\n",1200       "<style scoped>\n",

Showing the first 1,200 of 5939 lines. Download the file for the rest.