|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Requirement already satisfied: creme in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (0.6.1)\n", |
| 13 | + "Requirement already satisfied: numpy>=1.18.1 in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (from creme) (1.18.5)\n", |
| 14 | + "Requirement already satisfied: scipy>=1.4.1 in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (from creme) (1.4.1)\n", |
| 15 | + "Requirement already satisfied: pandas>=1.0.1 in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (from creme) (1.0.5)\n", |
| 16 | + "Requirement already satisfied: mmh3==2.5.1 in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (from creme) (2.5.1)\n", |
| 17 | + "Requirement already satisfied: python-dateutil>=2.6.1 in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (from pandas>=1.0.1->creme) (2.8.1)\n", |
| 18 | + "Requirement already satisfied: pytz>=2017.2 in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (from pandas>=1.0.1->creme) (2020.1)\n", |
| 19 | + "Requirement already satisfied: six>=1.5 in /Users/apoorvgarg/opt/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.6.1->pandas>=1.0.1->creme) (1.15.0)\n" |
| 20 | + ] |
| 21 | + } |
| 22 | + ], |
| 23 | + "source": [ |
| 24 | + "!pip install creme" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 5, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "import creme, math\n", |
| 34 | + "from creme import compose\n", |
| 35 | + "from creme import feature_extraction\n", |
| 36 | + "from creme import naive_bayes" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 3, |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "# List of tuple \n", |
| 46 | + "# creme accepts input as list of tuple\n", |
| 47 | + "docs = [ ('Chinese beijing Chinese','yes'),\n", |
| 48 | + " ('Chinese Chinese Shanghai','yes'),\n", |
| 49 | + " ('Chinese Macao','yes'),\n", |
| 50 | + " ('Tokyo Japan Chinese','no')\n", |
| 51 | + " ]" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 4, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [ |
| 59 | + { |
| 60 | + "data": { |
| 61 | + "text/plain": [ |
| 62 | + "[('Chinese beijing Chinese', 'yes'),\n", |
| 63 | + " ('Chinese Chinese Shanghai', 'yes'),\n", |
| 64 | + " ('Chinese Macao', 'yes'),\n", |
| 65 | + " ('Tokyo Japan Chinese', 'no')]" |
| 66 | + ] |
| 67 | + }, |
| 68 | + "execution_count": 4, |
| 69 | + "metadata": {}, |
| 70 | + "output_type": "execute_result" |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "docs " |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 6, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [], |
| 82 | + "source": [ |
| 83 | + "model = compose.Pipeline(\n", |
| 84 | + " ('tokenize', feature_extraction.BagOfWords(lowercase=False)),\n", |
| 85 | + " ('nb',naive_bayes.MultinomialNB(alpha=1))\n", |
| 86 | + ")" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 7, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "for sentence, label in docs:\n", |
| 96 | + " model = model.fit_one(sentence,label)" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 8, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "new_unseen_text = 'Tokyo india'" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 9, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [ |
| 113 | + { |
| 114 | + "data": { |
| 115 | + "text/plain": [ |
| 116 | + "'no'" |
| 117 | + ] |
| 118 | + }, |
| 119 | + "execution_count": 9, |
| 120 | + "metadata": {}, |
| 121 | + "output_type": "execute_result" |
| 122 | + } |
| 123 | + ], |
| 124 | + "source": [ |
| 125 | + "model.predict_one(new_unseen_text)" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "### Training on a new Data and new category" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 17, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [ |
| 147 | + { |
| 148 | + "data": { |
| 149 | + "text/plain": [ |
| 150 | + "Pipeline (\n", |
| 151 | + " BagOfWords (\n", |
| 152 | + " on=None\n", |
| 153 | + " strip_accents=True\n", |
| 154 | + " lowercase=False\n", |
| 155 | + " preprocessor=None\n", |
| 156 | + " tokenizer=<built-in method findall of re.Pattern object at 0x7fc7f4fef6b0>\n", |
| 157 | + " ngram_range=(1, 1)\n", |
| 158 | + " ),\n", |
| 159 | + " MultinomialNB (\n", |
| 160 | + " alpha=1\n", |
| 161 | + " )\n", |
| 162 | + ")" |
| 163 | + ] |
| 164 | + }, |
| 165 | + "execution_count": 17, |
| 166 | + "metadata": {}, |
| 167 | + "output_type": "execute_result" |
| 168 | + } |
| 169 | + ], |
| 170 | + "source": [ |
| 171 | + "model.fit_one('France Africa','may be')" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": 19, |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [ |
| 179 | + { |
| 180 | + "data": { |
| 181 | + "text/plain": [ |
| 182 | + "'may be'" |
| 183 | + ] |
| 184 | + }, |
| 185 | + "execution_count": 19, |
| 186 | + "metadata": {}, |
| 187 | + "output_type": "execute_result" |
| 188 | + } |
| 189 | + ], |
| 190 | + "source": [ |
| 191 | + "model.predict_one(\"Africa Delhi\")" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": null, |
| 197 | + "metadata": {}, |
| 198 | + "outputs": [], |
| 199 | + "source": [] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "kernelspec": { |
| 204 | + "display_name": "Python 3", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.8.3" |
| 219 | + } |
| 220 | + }, |
| 221 | + "nbformat": 4, |
| 222 | + "nbformat_minor": 4 |
| 223 | +} |
0 commit comments