From 7f9ce849eccce6166612b318b9b3b329d30d9d09 Mon Sep 17 00:00:00 2001 From: Filip Johnsson <filjo653@student.liu.se> Date: Wed, 6 Nov 2024 15:29:45 +0100 Subject: [PATCH] l1 --- l1/TM-Lab1.ipynb | 744 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 720 insertions(+), 24 deletions(-) diff --git a/l1/TM-Lab1.ipynb b/l1/TM-Lab1.ipynb index 887a2f9..4b023f6 100644 --- a/l1/TM-Lab1.ipynb +++ b/l1/TM-Lab1.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "id": "2c92aa93-cf15-4e1c-975e-fea9bbe0b0c4", "metadata": { "deletable": false, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 95, "id": "fd4982e3-3df4-4837-97b5-4c300b0d4a20", "metadata": { "deletable": false, @@ -116,10 +116,93 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "id": "141b8fbb-7a52-4cec-8d9f-921f4694e9c8", "metadata": {}, - "outputs": [], + "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>name</th>\n", + " <th>description</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>200</th>\n", + " <td>Brick Breaker Star: Space King</td>\n", + " <td>Introducing the best Brick Breaker game that everyone can enjoy.\\nEnjoy various missions and addictively simple play control.\\n\\n[Features]\\n- Hundreds of stages and various missions\\n- No limit to play such as Heart, play as much as you can!\\n- 5 kinds of various items and items reinforcement system\\n- No network required\\n- game file is as low as 20M, light-weight download!\\n- supports tablet screen\\n- supports Google Play Leaderboards, Achievement, Multiplay\\n- supports 14 languages\\n\\nHo...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>201</th>\n", + " <td>Brick Classic - Brick Game</td>\n", + " <td>Classic Brick Game!\\n\\nBrick Classic is a popular and addictive puzzle game!\\n\\nHow to play?\\n- Simply drag the bricks to move them.\\n- Create full lines on the grid vertically or horizontally to break bricks.\\n\\nTips:\\n- Classic brick game without time limits.\\n- Place the bricks in a reasonable position.\\n- The more brick break, the more scores you have.\\n- Bricks can't be rotated.\\n\\nWho's the best brick breaker? Challenge it now!!!</td>\n", + " </tr>\n", + " <tr>\n", + " <th>202</th>\n", + " <td>Bricks Breaker - Glow Balls</td>\n", + " <td>Bricks Breaker - Glow Balls is a addictive and challenging brick game.\\nJust play it to relax your brain. Be focus on breaking bricks and you will find it more funny and exciting.\\n\\nHow to play\\n- Hold the screen with your finger and move to aim.\\n- Find best positions and angles to hit all bricks.\\n- When the durability of brick reaches 0, destroyed.\\n- Never let bricks reach the bottom or game is over.\\n\\nFeatures\\n- Colorful glow skins.\\n- Free to play.\\n- Easy game controls with one fin...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>203</th>\n", + " <td>Bricks Breaker Quest</td>\n", + " <td>How to play\\n- The ball flies to wherever you touched.\\n- Clear the stages by removing bricks on the board.\\n- Break the bricks and never let them hit the bottom.\\n- Find best positions and angles to hit every brick.\\n\\nFeature\\n- Free to play\\n- Tons of stages\\n- Various types of balls\\n- Easy to play, Simplest game system, Designed for one handheld gameplay.\\n- Off-line (without internet connection) gameplay supported \\n- Multi-play supported\\n- Tablet device supported\\n- Achievement & lea...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>204</th>\n", + " <td>Brothers in Arms® 3</td>\n", + " <td>Fight brave soldiers from around the globe on the frenzied multiplayer battlegrounds of World War 2 or become Sergeant Wright and experience a dramatic, life-changing single-player journey, in the aftermath of the D-Day invasion.\\n\\nCLIMB THE ARMY RANKS IN MULTIPLAYER \\n> 4 maps to master and enjoy. \\n> 2 gameplay modes to begin with: Free For All and Team Deathmatch.\\n> Unlock game-changing perks by playing with each weapon class!\\n> A soldier’s only as deadly as his weapon. Be sure to upgr...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>205</th>\n", + " <td>Brown Dust - Tactical RPG</td>\n", + " <td>The Empire has fallen, and the Age of Great Mercenaries Now Begins!\\nCreate Your Ultimate Team And Strike Down Your Enemies!\\n\\nCAPTIVATING AND STUNNING ARTWORK\\n- Experience the high-quality anime illustrations you have never seen before.\\n- Meet Brown Dust's charming Mercenaries now.\\n\\nASSEMBLE LEGENDARY MERCENARIES\\n- Over 300 Mercenaries and a Variety of Skills.\\n- Discover the Unique Mercenaries, 6 Devils and Dominus Octo.\\n- All Mercenaries can reach max level and the highest rank.\\n\\...</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " name \\\n", + "200 Brick Breaker Star: Space King \n", + "201 Brick Classic - Brick Game \n", + "202 Bricks Breaker - Glow Balls \n", + "203 Bricks Breaker Quest \n", + "204 Brothers in Arms® 3 \n", + "205 Brown Dust - Tactical RPG \n", + "\n", + " description \n", + "200 Introducing the best Brick Breaker game that everyone can enjoy.\\nEnjoy various missions and addictively simple play control.\\n\\n[Features]\\n- Hundreds of stages and various missions\\n- No limit to play such as Heart, play as much as you can!\\n- 5 kinds of various items and items reinforcement system\\n- No network required\\n- game file is as low as 20M, light-weight download!\\n- supports tablet screen\\n- supports Google Play Leaderboards, Achievement, Multiplay\\n- supports 14 languages\\n\\nHo... \n", + "201 Classic Brick Game!\\n\\nBrick Classic is a popular and addictive puzzle game!\\n\\nHow to play?\\n- Simply drag the bricks to move them.\\n- Create full lines on the grid vertically or horizontally to break bricks.\\n\\nTips:\\n- Classic brick game without time limits.\\n- Place the bricks in a reasonable position.\\n- The more brick break, the more scores you have.\\n- Bricks can't be rotated.\\n\\nWho's the best brick breaker? Challenge it now!!! \n", + "202 Bricks Breaker - Glow Balls is a addictive and challenging brick game.\\nJust play it to relax your brain. Be focus on breaking bricks and you will find it more funny and exciting.\\n\\nHow to play\\n- Hold the screen with your finger and move to aim.\\n- Find best positions and angles to hit all bricks.\\n- When the durability of brick reaches 0, destroyed.\\n- Never let bricks reach the bottom or game is over.\\n\\nFeatures\\n- Colorful glow skins.\\n- Free to play.\\n- Easy game controls with one fin... \n", + "203 How to play\\n- The ball flies to wherever you touched.\\n- Clear the stages by removing bricks on the board.\\n- Break the bricks and never let them hit the bottom.\\n- Find best positions and angles to hit every brick.\\n\\nFeature\\n- Free to play\\n- Tons of stages\\n- Various types of balls\\n- Easy to play, Simplest game system, Designed for one handheld gameplay.\\n- Off-line (without internet connection) gameplay supported \\n- Multi-play supported\\n- Tablet device supported\\n- Achievement & lea... \n", + "204 Fight brave soldiers from around the globe on the frenzied multiplayer battlegrounds of World War 2 or become Sergeant Wright and experience a dramatic, life-changing single-player journey, in the aftermath of the D-Day invasion.\\n\\nCLIMB THE ARMY RANKS IN MULTIPLAYER \\n> 4 maps to master and enjoy. \\n> 2 gameplay modes to begin with: Free For All and Team Deathmatch.\\n> Unlock game-changing perks by playing with each weapon class!\\n> A soldier’s only as deadly as his weapon. Be sure to upgr... \n", + "205 The Empire has fallen, and the Age of Great Mercenaries Now Begins!\\nCreate Your Ultimate Team And Strike Down Your Enemies!\\n\\nCAPTIVATING AND STUNNING ARTWORK\\n- Experience the high-quality anime illustrations you have never seen before.\\n- Meet Brown Dust's charming Mercenaries now.\\n\\nASSEMBLE LEGENDARY MERCENARIES\\n- Over 300 Mercenaries and a Variety of Skills.\\n- Discover the Unique Mercenaries, 6 Devils and Dominus Octo.\\n- All Mercenaries can reach max level and the highest rank.\\n\\... " + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "df.loc[200:205]" ] @@ -134,10 +217,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "id": "595bb7af-5ee4-4b8e-8f5e-df5aaae688d9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Introducing the best Brick Breaker game that everyone can enjoy.\\nEnjoy various missions and addictively simple play control.\\n\\n[Features]\\n- Hundreds of stages and various missions\\n- No limit to play such as Heart, play as much as you can!\\n- 5 kinds of various items and items reinforcement system\\n- No network required\\n- game file is as low as 20M, light-weight download!\\n- supports tablet screen\\n- supports Google Play Leaderboards, Achievement, Multiplay\\n- supports 14 languages\\n\\nHomepage:\\nhttps://play.google.com/store/apps/dev?id=4931745640662708567\\n\\nFacebook: \\nhttps://www.facebook.com/spcomesgames/'" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "df.loc[200, 'description']" ] @@ -160,7 +254,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 98, "id": "0ca674d8-c2df-4c8f-bb3d-6d59bdc401fb", "metadata": { "deletable": false, @@ -176,7 +270,19 @@ "task": false } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "<Compressed Sparse Row sparse matrix of dtype 'float64'\n", + "\twith 267110 stored elements and shape (1614, 27877)>" + ] + }, + "execution_count": 98, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from sklearn.feature_extraction.text import TfidfVectorizer\n", "\n", @@ -195,16 +301,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "id": "10d112e7-e564-4e47-a633-38910709947f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Let's see how many pancakes you can pile up!!\n", + "Vector: [0. 0. 0. ... 0. 0. 0.]\n" + ] + } + ], "source": [ "# We can use 'toarray' to convert the sparse matrix object into a \"normal\" array\n", "vec = X[1032].toarray()[0]\n", "\n", "# The app description & its corresponding vector\n", - "df.loc[1032, 'description'], vec" + "print(df.loc[1032, 'description'])\n", + "\n", + "print(\"Vector: \",vec)" ] }, { @@ -227,7 +344,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 100, "id": "2f43ad18-aa4e-4d31-85de-0a7419767c37", "metadata": { "deletable": false, @@ -243,12 +360,37 @@ "task": false } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "can: 0.13047602895910532\n", + "how: 0.21153246225085887\n", + "let: 0.2615287714771797\n", + "many: 0.23491959669849022\n", + "pancakes: 0.6539332651185913\n", + "pile: 0.5304701435508047\n", + "see: 0.2557630827415271\n", + "up: 0.17216837691451817\n", + "you: 0.10276923239718011\n" + ] + } + ], "source": [ "\"\"\"Print the tokens and their tf–idf values, in descending order.\"\"\"\n", "\n", - "# YOUR CODE HERE\n", - "raise NotImplementedError()" + "# Get a list of all tokens\n", + "tokens = vectorizer.get_feature_names_out()\n", + "\n", + "for i in range(len(tokens)):\n", + " token = tokens[i]\n", + " value = vec[i]\n", + "\n", + " if value > 0:\n", + " print(f\"{token}: {value}\")\n", + "\n", + " " ] }, { @@ -273,7 +415,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "id": "ccf46d50-ea74-42d7-86e2-6520dde6eac6", "metadata": { "deletable": false, @@ -288,12 +430,435 @@ "task": false } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "<style>#sk-container-id-2 {\n", + " /* Definition of color scheme common for light and dark mode */\n", + " --sklearn-color-text: black;\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", + " /* Specific color for light theme */\n", + " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n", + " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n", + " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n", + " --sklearn-color-icon: #696969;\n", + "\n", + " @media (prefers-color-scheme: dark) {\n", + " /* Redefinition of color scheme for dark theme */\n", + " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n", + " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n", + " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n", + " --sklearn-color-icon: #878787;\n", + " }\n", + "}\n", + "\n", + "#sk-container-id-2 {\n", + " color: var(--sklearn-color-text);\n", + "}\n", + "\n", + "#sk-container-id-2 pre {\n", + " padding: 0;\n", + "}\n", + "\n", + "#sk-container-id-2 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-2 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-2 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-2 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-2 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-2 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-2 div.sk-parallel-item {\n", + " display: flex;\n", + " flex-direction: column;\n", + "}\n", + "\n", + "#sk-container-id-2 div.sk-parallel-item:first-child::after {\n", + " align-self: flex-end;\n", + " width: 50%;\n", + "}\n", + "\n", + "#sk-container-id-2 div.sk-parallel-item:last-child::after {\n", + " align-self: flex-start;\n", + " width: 50%;\n", + "}\n", + "\n", + "#sk-container-id-2 div.sk-parallel-item:only-child::after {\n", + " width: 0;\n", + "}\n", + "\n", + "/* Serial-specific style estimator block */\n", + "\n", + "#sk-container-id-2 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-2 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-2 label.sk-toggleable__label {\n", + " cursor: pointer;\n", + " display: block;\n", + " width: 100%;\n", + " margin-bottom: 0;\n", + " padding: 0.5em;\n", + " box-sizing: border-box;\n", + " text-align: center;\n", + "}\n", + "\n", + "#sk-container-id-2 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-2 label.sk-toggleable__label-arrow:hover:before {\n", + " color: var(--sklearn-color-text);\n", + "}\n", + "\n", + "/* Toggleable content - dropdown */\n", + "\n", + "#sk-container-id-2 div.sk-toggleable__content {\n", + " max-height: 0;\n", + " max-width: 0;\n", + " overflow: hidden;\n", + " text-align: left;\n", + " /* unfitted */\n", + " background-color: var(--sklearn-color-unfitted-level-0);\n", + "}\n", + "\n", + "#sk-container-id-2 div.sk-toggleable__content.fitted {\n", + " /* fitted */\n", + " background-color: var(--sklearn-color-fitted-level-0);\n", + "}\n", + "\n", + "#sk-container-id-2 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-2 div.sk-toggleable__content.fitted pre {\n", + " /* unfitted */\n", + " background-color: var(--sklearn-color-fitted-level-0);\n", + "}\n", + "\n", + "#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n", + " /* Expand drop-down */\n", + " max-height: 200px;\n", + " max-width: 100%;\n", + " overflow: auto;\n", + "}\n", + "\n", + "#sk-container-id-2 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-2 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-2 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-2 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-2 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-2 div.sk-label label.sk-toggleable__label,\n", + "#sk-container-id-2 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-2 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-2 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-2 div.sk-label label {\n", + " font-family: monospace;\n", + " font-weight: bold;\n", + " display: inline-block;\n", + " line-height: 1.2em;\n", + "}\n", + "\n", + "#sk-container-id-2 div.sk-label-container {\n", + " text-align: center;\n", + "}\n", + "\n", + "/* Estimator-specific */\n", + "#sk-container-id-2 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-2 div.sk-estimator.fitted {\n", + " /* fitted */\n", + " background-color: var(--sklearn-color-fitted-level-0);\n", + "}\n", + "\n", + "/* on hover */\n", + "#sk-container-id-2 div.sk-estimator:hover {\n", + " /* unfitted */\n", + " background-color: var(--sklearn-color-unfitted-level-2);\n", + "}\n", + "\n", + "#sk-container-id-2 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-background);\n", + " border-radius: 1em;\n", + " height: 1em;\n", + " width: 1em;\n", + " text-decoration: none !important;\n", + " margin-left: 1ex;\n", + " /* unfitted */\n", + " border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n", + " color: var(--sklearn-color-unfitted-level-1);\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", + " border: var(--sklearn-color-fitted-level-1) 1pt solid;\n", + " color: var(--sklearn-color-fitted-level-1);\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", + " color: var(--sklearn-color-background);\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", + " color: var(--sklearn-color-background);\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-2 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-background);\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-2 a.estimator_doc_link.fitted {\n", + " /* fitted */\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-2 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-2 a.estimator_doc_link.fitted:hover {\n", + " /* fitted */\n", + " background-color: var(--sklearn-color-fitted-level-3);\n", + "}\n", + "</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>NearestNeighbors(metric='cosine', n_neighbors=10)</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-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\"> NearestNeighbors<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.5/modules/generated/sklearn.neighbors.NearestNeighbors.html\">?<span>Documentation for NearestNeighbors</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>NearestNeighbors(metric='cosine', n_neighbors=10)</pre></div> </div></div></div></div>" + ], + "text/plain": [ + "NearestNeighbors(metric='cosine', n_neighbors=10)" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "\"\"\"Instantiate and fit a class that returns the 10 nearest neighboring vectors.\"\"\"\n", "\n", - "# YOUR CODE HERE\n", - "raise NotImplementedError()" + "from sklearn.neighbors import NearestNeighbors\n", + "\n", + "# Instantiate the class\n", + "nearest_neighbors = NearestNeighbors(n_neighbors=10, metric='cosine')\n", + "\n", + "# Fit the model\n", + "nearest_neighbors.fit(X)" ] }, { @@ -306,7 +871,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 129, "id": "d70bfb5a-3648-44ad-aa3a-dfa39838ffc3", "metadata": { "deletable": false, @@ -333,8 +898,17 @@ " The 10 apps (with name and description) most similar (in terms of\n", " cosine similarity) to the given query as a Pandas DataFrame.\n", " \"\"\"\n", - " # YOUR CODE HERE\n", - " raise NotImplementedError()" + " query_tokens = vectorizer.transform([query])\n", + "\n", + " neighbors = nearest_neighbors.kneighbors(query_tokens, 10, return_distance=False)[0]\n", + "\n", + " data = {\n", + " \"name\": [df.loc[i, 'name'] for i in neighbors],\n", + " \"description\": [df.loc[i, 'description'] for i in neighbors]\n", + " }\n", + "\n", + " return pd.DataFrame(data)\n", + "\n" ] }, { @@ -349,7 +923,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 130, "id": "3d30a52e-bdac-412b-b9c4-8e9cc17436a6", "metadata": { "deletable": false, @@ -366,7 +940,129 @@ "task": false } }, - "outputs": [], + "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>name</th>\n", + " <th>description</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>0</th>\n", + " <td>Pancake Tower</td>\n", + " <td>Let's see how many pancakes you can pile up!!</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>Cooking School: Games for Girls</td>\n", + " <td>Children like to help their parents. They especially like to help with cooking . When there is a cooking in the kitchen, it is no way to play. But cooking is a complicated process and often it ends up with a huge mess in the kitchen. But what if you are so eager to cook pancakes, cake or cupcakes? How to cook all that without doing a cleaning after? We have a solution! Home Cooking School with our curious Hippo has opened especially for parents and children! We do not only cook food here. We...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>Hell’s Cooking — crazy chef burger, kitchen fever</td>\n", + " <td>⭐ ⭐ ⭐ ⭐ ⭐ New world of crazy cooking is here. Feel what it means to be a master chef who prepares fantastic fast food in a prominent king kitchen! If you haven't ever tried yourself as a hamburger chef cook, it's possibly the best time for making diner. Download and launch Hell's Cooking — crazy chef burger, kitchen fever HD game and get prepared to jump into a fever and adventurous perfect world of burgers.\\n\\nNew girls game Hell's Cooking gives you lots of opportunities for your crazy cafe...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>Solitaire</td>\n", + " <td>Solitaire Free by Solitaire Card Games is the #1 klondike solitaire games on android. The solitaire Free is popular and classic card games you know and love.\\n\\nWe carefully designed a fresh solitaire free modern look, woven into the wonderful solitaire classic feel that everyone loves. \\n\\nExperience the crisp, clear, and easy to read cards, simple and quick animations, and subtle sounds, in either landscape or portrait views. \\n\\nYou can move cards with a single tap or drag them to their d...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>Rummy - Free</td>\n", + " <td>Play the famous Rummy card game on your Android Smartphone or Tablet !! \\n\\nPlay rummy with 2, 3, or 4 players against simulated opponents playing with high-level artificial intelligence. \\nThere are a number of rules that can be modified, making this game very faithful to the original. \\n\\n*** MANY VARIATIONS INCLUDED *** \\n\\nMany rummy variations are included in the application: \\n\\n- From 2 to 4 players. \\n- Choose the AI level of opponents. \\n- Number of cards dealt to each player (from ...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>5</th>\n", + " <td>Sago Mini Trucks and Diggers</td>\n", + " <td>Drive a dump truck with Rosie the hamster! Pile dirt high and dig deep in the ground with diggers, cranes and bulldozers. Build a home for a new friend! Choose a barn, a castle or even a cupcake-house. Don’t forget to add the finishing touches for the proud owner.\\n\\nOn this construction site, kids love being the boss. With six mighty machines and piles of dirt, you can build all day! Part of the award-winning suite of Sago Mini apps, this app puts kids in charge.\\n\\nSago Mini apps have no i...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>6</th>\n", + " <td>Dr. Panda's Ice Cream Truck</td>\n", + " <td>Chocolate? Vanilla? Strawberry? All three!? You decide! In Dr. Panda’s Ice Cream Truck you can mix up all sorts of different flavors with cookies, chocolate, nuts and more to make the perfect ice cream—hundreds of combinations in all.\\n\\nScoop it!\\nThese animals love ice cream, and will eat as much (or little) as you want to serve them. You can make scoops big or small and pile them as high as you want—using any of the ice cream you’ve created!\\n\\nToppings galore!\\nUse chocolate syrup, cooki...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>7</th>\n", + " <td>Turbo Dismount™</td>\n", + " <td>The legendary crash simulator is now on Google Play!\\n\\nPerform death-defying motor stunts, crash into walls, create traffic pile-ups of epic scale - and share the fun!\\n\\nTurbo Dismount™ is a kinetic tragedy about Mr. Dismount and the cars who love him. It is the official sequel to the wildly popular and immensely successful personal impact simulator - Stair Dismount™. \\n\\nFEATURES:\\n* Flinch-inducing crash physics\\n* Crunchy sound effects\\n* Delicious slow-mo replay system\\n* Multiple vehi...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>8</th>\n", + " <td>UNO!™</td>\n", + " <td>Play the world’s number one card game like never before. UNO!™ has all-new rules, tournaments, adventures and so much more! At home or on the move, jump into games instantly. Whether an UNO!™ veteran or completely new, take on challenges and reap the rewards. UNO!™ is the ultimate competitive family-friendly card game.\\n- Play classic UNO!™ or use tons of popular house rules!\\n- Connect anytime, anywhere with friends from around the world! \\n- Two heads are better than one in 2v2 mode. Use t...</td>\n", + " </tr>\n", + " <tr>\n", + " <th>9</th>\n", + " <td>TO-FU Oh!SUSHI</td>\n", + " <td>You are the veritable sushi master! Prepare your own fun sushi with “Daizu” the skunk!\\n\\nThis app is designed to allow children to be creative by decorating their original sushi.\\n\\nServe your delicious, mysterious or impossible sushi to the people of “Tofu Island”! \\n\\nHow about creating sushi that is totally original and serve it to your beloved guests? Spice it up with tons of wasabi or even sprinkle chocolate and gummy bears for those sweet lovers.\\nFeel free to make any kind of sushi y...</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " name \\\n", + "0 Pancake Tower \n", + "1 Cooking School: Games for Girls \n", + "2 Hell’s Cooking — crazy chef burger, kitchen fever \n", + "3 Solitaire \n", + "4 Rummy - Free \n", + "5 Sago Mini Trucks and Diggers \n", + "6 Dr. Panda's Ice Cream Truck \n", + "7 Turbo Dismount™ \n", + "8 UNO!™ \n", + "9 TO-FU Oh!SUSHI \n", + "\n", + " description \n", + "0 Let's see how many pancakes you can pile up!! \n", + "1 Children like to help their parents. They especially like to help with cooking . When there is a cooking in the kitchen, it is no way to play. But cooking is a complicated process and often it ends up with a huge mess in the kitchen. But what if you are so eager to cook pancakes, cake or cupcakes? How to cook all that without doing a cleaning after? We have a solution! Home Cooking School with our curious Hippo has opened especially for parents and children! We do not only cook food here. We... \n", + "2 ⭐ ⭐ ⭐ ⭐ ⭐ New world of crazy cooking is here. Feel what it means to be a master chef who prepares fantastic fast food in a prominent king kitchen! If you haven't ever tried yourself as a hamburger chef cook, it's possibly the best time for making diner. Download and launch Hell's Cooking — crazy chef burger, kitchen fever HD game and get prepared to jump into a fever and adventurous perfect world of burgers.\\n\\nNew girls game Hell's Cooking gives you lots of opportunities for your crazy cafe... \n", + "3 Solitaire Free by Solitaire Card Games is the #1 klondike solitaire games on android. The solitaire Free is popular and classic card games you know and love.\\n\\nWe carefully designed a fresh solitaire free modern look, woven into the wonderful solitaire classic feel that everyone loves. \\n\\nExperience the crisp, clear, and easy to read cards, simple and quick animations, and subtle sounds, in either landscape or portrait views. \\n\\nYou can move cards with a single tap or drag them to their d... \n", + "4 Play the famous Rummy card game on your Android Smartphone or Tablet !! \\n\\nPlay rummy with 2, 3, or 4 players against simulated opponents playing with high-level artificial intelligence. \\nThere are a number of rules that can be modified, making this game very faithful to the original. \\n\\n*** MANY VARIATIONS INCLUDED *** \\n\\nMany rummy variations are included in the application: \\n\\n- From 2 to 4 players. \\n- Choose the AI level of opponents. \\n- Number of cards dealt to each player (from ... \n", + "5 Drive a dump truck with Rosie the hamster! Pile dirt high and dig deep in the ground with diggers, cranes and bulldozers. Build a home for a new friend! Choose a barn, a castle or even a cupcake-house. Don’t forget to add the finishing touches for the proud owner.\\n\\nOn this construction site, kids love being the boss. With six mighty machines and piles of dirt, you can build all day! Part of the award-winning suite of Sago Mini apps, this app puts kids in charge.\\n\\nSago Mini apps have no i... \n", + "6 Chocolate? Vanilla? Strawberry? All three!? You decide! In Dr. Panda’s Ice Cream Truck you can mix up all sorts of different flavors with cookies, chocolate, nuts and more to make the perfect ice cream—hundreds of combinations in all.\\n\\nScoop it!\\nThese animals love ice cream, and will eat as much (or little) as you want to serve them. You can make scoops big or small and pile them as high as you want—using any of the ice cream you’ve created!\\n\\nToppings galore!\\nUse chocolate syrup, cooki... \n", + "7 The legendary crash simulator is now on Google Play!\\n\\nPerform death-defying motor stunts, crash into walls, create traffic pile-ups of epic scale - and share the fun!\\n\\nTurbo Dismount™ is a kinetic tragedy about Mr. Dismount and the cars who love him. It is the official sequel to the wildly popular and immensely successful personal impact simulator - Stair Dismount™. \\n\\nFEATURES:\\n* Flinch-inducing crash physics\\n* Crunchy sound effects\\n* Delicious slow-mo replay system\\n* Multiple vehi... \n", + "8 Play the world’s number one card game like never before. UNO!™ has all-new rules, tournaments, adventures and so much more! At home or on the move, jump into games instantly. Whether an UNO!™ veteran or completely new, take on challenges and reap the rewards. UNO!™ is the ultimate competitive family-friendly card game.\\n- Play classic UNO!™ or use tons of popular house rules!\\n- Connect anytime, anywhere with friends from around the world! \\n- Two heads are better than one in 2v2 mode. Use t... \n", + "9 You are the veritable sushi master! Prepare your own fun sushi with “Daizu” the skunk!\\n\\nThis app is designed to allow children to be creative by decorating their original sushi.\\n\\nServe your delicious, mysterious or impossible sushi to the people of “Tofu Island”! \\n\\nHow about creating sushi that is totally original and serve it to your beloved guests? Spice it up with tons of wasabi or even sprinkle chocolate and gummy bears for those sweet lovers.\\nFeel free to make any kind of sushi y... " + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "<div class=\"alert alert-success\"><strong>Checks have passed!</strong></div>" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "\"\"\"Check that searching for \"pile up pancakes\" returns a DataFrame with ten results,\n", " and that the top result is \"Pancake Tower\".\"\"\"\n", -- GitLab