great-ai/examples/simple/train.ipynb

1810 lines
61 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Train a domain classifier on the [semantic scholar dataset](https://api.semanticscholar.org/corpus)\n",
"\n",
"> Part 2: train a model\n",
"\n",
"![position of this step in the lifecycle](diagrams/scope-train.svg)\n",
"> The blue boxes show the steps implemented in this notebook.\n",
"\n",
"In [Part 1](data.ipynb), we have cleaned and transformed our training data. We can now access this data using `great_ai.LargeFile`. Locally, it will gives us the cached version, otherwise, the latest version is downloaded from S3. \n",
"\n",
"In this part, we hyperparameter-optimise and train a simple, Naive Bayes classifier which we then export for deployment using `great_ai.save_model`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load data that has been extracted in [part 1](data.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[38;5;226m2022-06-25 11:25:08,430 | WARNING | Environment variable ENVIRONMENT is not set, defaulting to development mode ‼️\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,431 | INFO | Found credentials file (/data/projects/great-ai/examples/simple/mongo.ini), initialising MongodbDriver\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,432 | INFO | Found credentials file (/data/projects/great-ai/examples/simple/mongo.ini), initialising LargeFileMongo\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,432 | INFO | Settings: configured ✅\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,433 | INFO | 🔩 tracing_database: MongodbDriver\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,433 | INFO | 🔩 large_file_implementation: LargeFileMongo\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,434 | INFO | 🔩 is_production: False\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,434 | INFO | 🔩 should_log_exception_stack: True\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:08,434 | INFO | 🔩 prediction_cache_size: 512\u001b[0m\n",
"\u001b[38;5;226m2022-06-25 11:25:08,435 | WARNING | You still need to check whether you follow all best practices before trusting your deployment.\u001b[0m\n",
"\u001b[38;5;226m2022-06-25 11:25:08,435 | WARNING | > Find out more at https://se-ml.github.io/practices/\u001b[0m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'filter': {'$and': [{'tags': 'train'}, {'feedback': {'$ne': None}}]}, 'sort': []}\n"
]
}
],
"source": [
"from great_ai import query_ground_truth\n",
"\n",
"data = query_ground_truth(\"train\")\n",
"X = [d.input for d in data for domain in d.feedback]\n",
"y = [domain for d in data for domain in d.feedback]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"hovertemplate": "domain=%{x}<br>count=%{y}<extra></extra>",
"legendgroup": "",
"marker": {
"color": "#636efa",
"pattern": {
"shape": ""
}
},
"name": "",
"offsetgroup": "",
"orientation": "v",
"showlegend": false,
"textposition": "auto",
"type": "bar",
"x": [
"Medicine",
"Computer Science",
"Biology",
"Chemistry",
"Materials Science",
"Engineering",
"Physics",
"Psychology",
"Mathematics",
"Political Science",
"Business",
"Sociology",
"Economics",
"Geography",
"Geology",
"Environmental Science",
"History",
"Art",
"Philosophy"
],
"xaxis": "x",
"y": [
4409,
1422,
1369,
1352,
1026,
887,
638,
619,
582,
340,
333,
303,
256,
254,
229,
217,
113,
112,
65
],
"yaxis": "y"
}
],
"layout": {
"barmode": "relative",
"height": 400,
"legend": {
"tracegroupgap": 0
},
"margin": {
"t": 60
},
"template": {
"data": {
"bar": [
{
"error_x": {
"color": "#2a3f5f"
},
"error_y": {
"color": "#2a3f5f"
},
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "bar"
}
],
"barpolar": [
{
"marker": {
"line": {
"color": "#E5ECF6",
"width": 0.5
},
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "barpolar"
}
],
"carpet": [
{
"aaxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"baxis": {
"endlinecolor": "#2a3f5f",
"gridcolor": "white",
"linecolor": "white",
"minorgridcolor": "white",
"startlinecolor": "#2a3f5f"
},
"type": "carpet"
}
],
"choropleth": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "choropleth"
}
],
"contour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "contour"
}
],
"contourcarpet": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "contourcarpet"
}
],
"heatmap": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmap"
}
],
"heatmapgl": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "heatmapgl"
}
],
"histogram": [
{
"marker": {
"pattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
}
},
"type": "histogram"
}
],
"histogram2d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2d"
}
],
"histogram2dcontour": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "histogram2dcontour"
}
],
"mesh3d": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"type": "mesh3d"
}
],
"parcoords": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "parcoords"
}
],
"pie": [
{
"automargin": true,
"type": "pie"
}
],
"scatter": [
{
"fillpattern": {
"fillmode": "overlay",
"size": 10,
"solidity": 0.2
},
"type": "scatter"
}
],
"scatter3d": [
{
"line": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatter3d"
}
],
"scattercarpet": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattercarpet"
}
],
"scattergeo": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergeo"
}
],
"scattergl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattergl"
}
],
"scattermapbox": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scattermapbox"
}
],
"scatterpolar": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolar"
}
],
"scatterpolargl": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterpolargl"
}
],
"scatterternary": [
{
"marker": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"type": "scatterternary"
}
],
"surface": [
{
"colorbar": {
"outlinewidth": 0,
"ticks": ""
},
"colorscale": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"type": "surface"
}
],
"table": [
{
"cells": {
"fill": {
"color": "#EBF0F8"
},
"line": {
"color": "white"
}
},
"header": {
"fill": {
"color": "#C8D4E3"
},
"line": {
"color": "white"
}
},
"type": "table"
}
]
},
"layout": {
"annotationdefaults": {
"arrowcolor": "#2a3f5f",
"arrowhead": 0,
"arrowwidth": 1
},
"autotypenumbers": "strict",
"coloraxis": {
"colorbar": {
"outlinewidth": 0,
"ticks": ""
}
},
"colorscale": {
"diverging": [
[
0,
"#8e0152"
],
[
0.1,
"#c51b7d"
],
[
0.2,
"#de77ae"
],
[
0.3,
"#f1b6da"
],
[
0.4,
"#fde0ef"
],
[
0.5,
"#f7f7f7"
],
[
0.6,
"#e6f5d0"
],
[
0.7,
"#b8e186"
],
[
0.8,
"#7fbc41"
],
[
0.9,
"#4d9221"
],
[
1,
"#276419"
]
],
"sequential": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
],
"sequentialminus": [
[
0,
"#0d0887"
],
[
0.1111111111111111,
"#46039f"
],
[
0.2222222222222222,
"#7201a8"
],
[
0.3333333333333333,
"#9c179e"
],
[
0.4444444444444444,
"#bd3786"
],
[
0.5555555555555556,
"#d8576b"
],
[
0.6666666666666666,
"#ed7953"
],
[
0.7777777777777778,
"#fb9f3a"
],
[
0.8888888888888888,
"#fdca26"
],
[
1,
"#f0f921"
]
]
},
"colorway": [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52"
],
"font": {
"color": "#2a3f5f"
},
"geo": {
"bgcolor": "white",
"lakecolor": "white",
"landcolor": "#E5ECF6",
"showlakes": true,
"showland": true,
"subunitcolor": "white"
},
"hoverlabel": {
"align": "left"
},
"hovermode": "closest",
"mapbox": {
"style": "light"
},
"paper_bgcolor": "white",
"plot_bgcolor": "#E5ECF6",
"polar": {
"angularaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"radialaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"scene": {
"xaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"yaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
},
"zaxis": {
"backgroundcolor": "#E5ECF6",
"gridcolor": "white",
"gridwidth": 2,
"linecolor": "white",
"showbackground": true,
"ticks": "",
"zerolinecolor": "white"
}
},
"shapedefaults": {
"line": {
"color": "#2a3f5f"
}
},
"ternary": {
"aaxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"baxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
},
"bgcolor": "#E5ECF6",
"caxis": {
"gridcolor": "white",
"linecolor": "white",
"ticks": ""
}
},
"title": {
"x": 0.05
},
"xaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
},
"yaxis": {
"automargin": true,
"gridcolor": "white",
"linecolor": "white",
"ticks": "",
"title": {
"standoff": 15
},
"zerolinecolor": "white",
"zerolinewidth": 2
}
}
},
"width": 1200,
"xaxis": {
"anchor": "y",
"domain": [
0,
1
],
"title": {
"text": "domain"
}
},
"yaxis": {
"anchor": "x",
"domain": [
0,
1
],
"title": {
"text": "count"
}
}
}
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pandas as pd\n",
"from collections import Counter\n",
"import plotly.express as px\n",
"\n",
"df = pd.DataFrame(Counter(y).most_common(), columns=[\"domain\", \"count\"])\n",
"px.bar(df, \"domain\", \"count\", width=1200, height=400).show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Optimise and train Multinomial Naive Bayes classifier"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.naive_bayes import MultinomialNB\n",
"from sklearn.pipeline import Pipeline\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"\n",
"\n",
"def create_pipeline() -> Pipeline:\n",
" return Pipeline(\n",
" steps=[\n",
" (\"vectorizer\", TfidfVectorizer(sublinear_tf=True)),\n",
" (\"classifier\", MultinomialNB()),\n",
" ]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fitting 3 folds for each of 24 candidates, totalling 72 fits\n"
]
},
{
"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>mean_fit_time</th>\n",
" <th>std_fit_time</th>\n",
" <th>mean_score_time</th>\n",
" <th>std_score_time</th>\n",
" <th>param_classifier__alpha</th>\n",
" <th>param_classifier__fit_prior</th>\n",
" <th>param_vectorizer__max_df</th>\n",
" <th>param_vectorizer__min_df</th>\n",
" <th>params</th>\n",
" <th>split0_test_score</th>\n",
" <th>split1_test_score</th>\n",
" <th>split2_test_score</th>\n",
" <th>mean_test_score</th>\n",
" <th>std_test_score</th>\n",
" <th>rank_test_score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1.986588</td>\n",
" <td>0.050896</td>\n",
" <td>1.090251</td>\n",
" <td>0.135508</td>\n",
" <td>0.5</td>\n",
" <td>False</td>\n",
" <td>0.05</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.459165</td>\n",
" <td>0.473024</td>\n",
" <td>0.475462</td>\n",
" <td>0.469217</td>\n",
" <td>0.007177</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>2.070333</td>\n",
" <td>0.038396</td>\n",
" <td>0.976315</td>\n",
" <td>0.033742</td>\n",
" <td>0.5</td>\n",
" <td>False</td>\n",
" <td>0.1</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.457524</td>\n",
" <td>0.463575</td>\n",
" <td>0.458007</td>\n",
" <td>0.459702</td>\n",
" <td>0.002745</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>2.049166</td>\n",
" <td>0.193113</td>\n",
" <td>1.064576</td>\n",
" <td>0.087034</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>0.05</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.441657</td>\n",
" <td>0.452933</td>\n",
" <td>0.451286</td>\n",
" <td>0.448625</td>\n",
" <td>0.004973</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>2.288145</td>\n",
" <td>0.131338</td>\n",
" <td>1.133445</td>\n",
" <td>0.099583</td>\n",
" <td>0.5</td>\n",
" <td>False</td>\n",
" <td>0.05</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.436976</td>\n",
" <td>0.449693</td>\n",
" <td>0.437911</td>\n",
" <td>0.441527</td>\n",
" <td>0.005787</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>1.872234</td>\n",
" <td>0.072997</td>\n",
" <td>0.748070</td>\n",
" <td>0.085882</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>0.1</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.432322</td>\n",
" <td>0.438805</td>\n",
" <td>0.428209</td>\n",
" <td>0.433112</td>\n",
" <td>0.004362</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>2.067691</td>\n",
" <td>0.126923</td>\n",
" <td>0.910947</td>\n",
" <td>0.078748</td>\n",
" <td>0.5</td>\n",
" <td>False</td>\n",
" <td>0.1</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.426436</td>\n",
" <td>0.429182</td>\n",
" <td>0.437410</td>\n",
" <td>0.431009</td>\n",
" <td>0.004662</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>1.847330</td>\n",
" <td>0.147504</td>\n",
" <td>0.495354</td>\n",
" <td>0.018589</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>0.1</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.422130</td>\n",
" <td>0.430875</td>\n",
" <td>0.430829</td>\n",
" <td>0.427945</td>\n",
" <td>0.004112</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>2.071489</td>\n",
" <td>0.256086</td>\n",
" <td>1.055936</td>\n",
" <td>0.037198</td>\n",
" <td>0.5</td>\n",
" <td>False</td>\n",
" <td>0.1</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.416746</td>\n",
" <td>0.425938</td>\n",
" <td>0.417381</td>\n",
" <td>0.420022</td>\n",
" <td>0.004192</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>1.776546</td>\n",
" <td>0.064677</td>\n",
" <td>0.888485</td>\n",
" <td>0.093302</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>0.05</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.413441</td>\n",
" <td>0.417122</td>\n",
" <td>0.427196</td>\n",
" <td>0.419253</td>\n",
" <td>0.005814</td>\n",
" <td>9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>2.015992</td>\n",
" <td>0.062583</td>\n",
" <td>0.974434</td>\n",
" <td>0.082582</td>\n",
" <td>0.5</td>\n",
" <td>False</td>\n",
" <td>0.05</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.412522</td>\n",
" <td>0.410409</td>\n",
" <td>0.425047</td>\n",
" <td>0.415993</td>\n",
" <td>0.006460</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>2.032050</td>\n",
" <td>0.104313</td>\n",
" <td>0.969210</td>\n",
" <td>0.070997</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>0.05</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.392393</td>\n",
" <td>0.396143</td>\n",
" <td>0.386439</td>\n",
" <td>0.391659</td>\n",
" <td>0.003996</td>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2.307611</td>\n",
" <td>0.142902</td>\n",
" <td>1.102178</td>\n",
" <td>0.078755</td>\n",
" <td>0.5</td>\n",
" <td>True</td>\n",
" <td>0.05</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.385834</td>\n",
" <td>0.392225</td>\n",
" <td>0.366549</td>\n",
" <td>0.381536</td>\n",
" <td>0.010914</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1.876958</td>\n",
" <td>0.050648</td>\n",
" <td>1.031975</td>\n",
" <td>0.029341</td>\n",
" <td>0.5</td>\n",
" <td>True</td>\n",
" <td>0.1</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.377309</td>\n",
" <td>0.384224</td>\n",
" <td>0.366632</td>\n",
" <td>0.376055</td>\n",
" <td>0.007236</td>\n",
" <td>13</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>1.897479</td>\n",
" <td>0.094903</td>\n",
" <td>0.740516</td>\n",
" <td>0.143821</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" <td>0.1</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.360551</td>\n",
" <td>0.366311</td>\n",
" <td>0.350439</td>\n",
" <td>0.359101</td>\n",
" <td>0.006561</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1.824982</td>\n",
" <td>0.038113</td>\n",
" <td>0.960814</td>\n",
" <td>0.041330</td>\n",
" <td>0.5</td>\n",
" <td>True</td>\n",
" <td>0.1</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.347831</td>\n",
" <td>0.350708</td>\n",
" <td>0.349963</td>\n",
" <td>0.349501</td>\n",
" <td>0.001219</td>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2.151446</td>\n",
" <td>0.120440</td>\n",
" <td>0.887620</td>\n",
" <td>0.037900</td>\n",
" <td>0.5</td>\n",
" <td>True</td>\n",
" <td>0.05</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.335093</td>\n",
" <td>0.340020</td>\n",
" <td>0.341502</td>\n",
" <td>0.338872</td>\n",
" <td>0.002739</td>\n",
" <td>16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>2.004961</td>\n",
" <td>0.196640</td>\n",
" <td>0.953482</td>\n",
" <td>0.113914</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" <td>0.1</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.300093</td>\n",
" <td>0.306020</td>\n",
" <td>0.305047</td>\n",
" <td>0.303720</td>\n",
" <td>0.002595</td>\n",
" <td>17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>1.890709</td>\n",
" <td>0.056234</td>\n",
" <td>1.006219</td>\n",
" <td>0.042628</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" <td>0.05</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.297119</td>\n",
" <td>0.302061</td>\n",
" <td>0.298975</td>\n",
" <td>0.299385</td>\n",
" <td>0.002038</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>2.022432</td>\n",
" <td>0.201745</td>\n",
" <td>0.931837</td>\n",
" <td>0.047467</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" <td>0.05</td>\n",
" <td>100</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.300746</td>\n",
" <td>0.291492</td>\n",
" <td>0.297456</td>\n",
" <td>0.296565</td>\n",
" <td>0.003830</td>\n",
" <td>19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2.081572</td>\n",
" <td>0.123875</td>\n",
" <td>1.058692</td>\n",
" <td>0.034084</td>\n",
" <td>0.5</td>\n",
" <td>True</td>\n",
" <td>0.05</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.296667</td>\n",
" <td>0.294715</td>\n",
" <td>0.292144</td>\n",
" <td>0.294509</td>\n",
" <td>0.001852</td>\n",
" <td>20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>1.961700</td>\n",
" <td>0.108977</td>\n",
" <td>0.964816</td>\n",
" <td>0.089735</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" <td>0.1</td>\n",
" <td>20</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.278389</td>\n",
" <td>0.287134</td>\n",
" <td>0.286320</td>\n",
" <td>0.283948</td>\n",
" <td>0.003945</td>\n",
" <td>21</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2.272779</td>\n",
" <td>0.155897</td>\n",
" <td>0.982528</td>\n",
" <td>0.045744</td>\n",
" <td>0.5</td>\n",
" <td>True</td>\n",
" <td>0.1</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 0.5, 'classifier__fit_pr...</td>\n",
" <td>0.275833</td>\n",
" <td>0.272693</td>\n",
" <td>0.278427</td>\n",
" <td>0.275651</td>\n",
" <td>0.002344</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>1.979826</td>\n",
" <td>0.106524</td>\n",
" <td>0.938829</td>\n",
" <td>0.050975</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" <td>0.05</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.181309</td>\n",
" <td>0.186707</td>\n",
" <td>0.193406</td>\n",
" <td>0.187141</td>\n",
" <td>0.004948</td>\n",
" <td>23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>1.952817</td>\n",
" <td>0.107900</td>\n",
" <td>0.938851</td>\n",
" <td>0.048295</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" <td>0.1</td>\n",
" <td>5</td>\n",
" <td>{'classifier__alpha': 1, 'classifier__fit_prio...</td>\n",
" <td>0.160402</td>\n",
" <td>0.172025</td>\n",
" <td>0.171455</td>\n",
" <td>0.167960</td>\n",
" <td>0.005350</td>\n",
" <td>24</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" mean_fit_time std_fit_time mean_score_time std_score_time \\\n",
"7 1.986588 0.050896 1.090251 0.135508 \n",
"10 2.070333 0.038396 0.976315 0.033742 \n",
"19 2.049166 0.193113 1.064576 0.087034 \n",
"6 2.288145 0.131338 1.133445 0.099583 \n",
"22 1.872234 0.072997 0.748070 0.085882 \n",
"11 2.067691 0.126923 0.910947 0.078748 \n",
"23 1.847330 0.147504 0.495354 0.018589 \n",
"9 2.071489 0.256086 1.055936 0.037198 \n",
"20 1.776546 0.064677 0.888485 0.093302 \n",
"8 2.015992 0.062583 0.974434 0.082582 \n",
"18 2.032050 0.104313 0.969210 0.070997 \n",
"1 2.307611 0.142902 1.102178 0.078755 \n",
"4 1.876958 0.050648 1.031975 0.029341 \n",
"21 1.897479 0.094903 0.740516 0.143821 \n",
"5 1.824982 0.038113 0.960814 0.041330 \n",
"2 2.151446 0.120440 0.887620 0.037900 \n",
"17 2.004961 0.196640 0.953482 0.113914 \n",
"13 1.890709 0.056234 1.006219 0.042628 \n",
"14 2.022432 0.201745 0.931837 0.047467 \n",
"0 2.081572 0.123875 1.058692 0.034084 \n",
"16 1.961700 0.108977 0.964816 0.089735 \n",
"3 2.272779 0.155897 0.982528 0.045744 \n",
"12 1.979826 0.106524 0.938829 0.050975 \n",
"15 1.952817 0.107900 0.938851 0.048295 \n",
"\n",
" param_classifier__alpha param_classifier__fit_prior \\\n",
"7 0.5 False \n",
"10 0.5 False \n",
"19 1 False \n",
"6 0.5 False \n",
"22 1 False \n",
"11 0.5 False \n",
"23 1 False \n",
"9 0.5 False \n",
"20 1 False \n",
"8 0.5 False \n",
"18 1 False \n",
"1 0.5 True \n",
"4 0.5 True \n",
"21 1 False \n",
"5 0.5 True \n",
"2 0.5 True \n",
"17 1 True \n",
"13 1 True \n",
"14 1 True \n",
"0 0.5 True \n",
"16 1 True \n",
"3 0.5 True \n",
"12 1 True \n",
"15 1 True \n",
"\n",
" param_vectorizer__max_df param_vectorizer__min_df \\\n",
"7 0.05 20 \n",
"10 0.1 20 \n",
"19 0.05 20 \n",
"6 0.05 5 \n",
"22 0.1 20 \n",
"11 0.1 100 \n",
"23 0.1 100 \n",
"9 0.1 5 \n",
"20 0.05 100 \n",
"8 0.05 100 \n",
"18 0.05 5 \n",
"1 0.05 20 \n",
"4 0.1 20 \n",
"21 0.1 5 \n",
"5 0.1 100 \n",
"2 0.05 100 \n",
"17 0.1 100 \n",
"13 0.05 20 \n",
"14 0.05 100 \n",
"0 0.05 5 \n",
"16 0.1 20 \n",
"3 0.1 5 \n",
"12 0.05 5 \n",
"15 0.1 5 \n",
"\n",
" params split0_test_score \\\n",
"7 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.459165 \n",
"10 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.457524 \n",
"19 {'classifier__alpha': 1, 'classifier__fit_prio... 0.441657 \n",
"6 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.436976 \n",
"22 {'classifier__alpha': 1, 'classifier__fit_prio... 0.432322 \n",
"11 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.426436 \n",
"23 {'classifier__alpha': 1, 'classifier__fit_prio... 0.422130 \n",
"9 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.416746 \n",
"20 {'classifier__alpha': 1, 'classifier__fit_prio... 0.413441 \n",
"8 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.412522 \n",
"18 {'classifier__alpha': 1, 'classifier__fit_prio... 0.392393 \n",
"1 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.385834 \n",
"4 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.377309 \n",
"21 {'classifier__alpha': 1, 'classifier__fit_prio... 0.360551 \n",
"5 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.347831 \n",
"2 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.335093 \n",
"17 {'classifier__alpha': 1, 'classifier__fit_prio... 0.300093 \n",
"13 {'classifier__alpha': 1, 'classifier__fit_prio... 0.297119 \n",
"14 {'classifier__alpha': 1, 'classifier__fit_prio... 0.300746 \n",
"0 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.296667 \n",
"16 {'classifier__alpha': 1, 'classifier__fit_prio... 0.278389 \n",
"3 {'classifier__alpha': 0.5, 'classifier__fit_pr... 0.275833 \n",
"12 {'classifier__alpha': 1, 'classifier__fit_prio... 0.181309 \n",
"15 {'classifier__alpha': 1, 'classifier__fit_prio... 0.160402 \n",
"\n",
" split1_test_score split2_test_score mean_test_score std_test_score \\\n",
"7 0.473024 0.475462 0.469217 0.007177 \n",
"10 0.463575 0.458007 0.459702 0.002745 \n",
"19 0.452933 0.451286 0.448625 0.004973 \n",
"6 0.449693 0.437911 0.441527 0.005787 \n",
"22 0.438805 0.428209 0.433112 0.004362 \n",
"11 0.429182 0.437410 0.431009 0.004662 \n",
"23 0.430875 0.430829 0.427945 0.004112 \n",
"9 0.425938 0.417381 0.420022 0.004192 \n",
"20 0.417122 0.427196 0.419253 0.005814 \n",
"8 0.410409 0.425047 0.415993 0.006460 \n",
"18 0.396143 0.386439 0.391659 0.003996 \n",
"1 0.392225 0.366549 0.381536 0.010914 \n",
"4 0.384224 0.366632 0.376055 0.007236 \n",
"21 0.366311 0.350439 0.359101 0.006561 \n",
"5 0.350708 0.349963 0.349501 0.001219 \n",
"2 0.340020 0.341502 0.338872 0.002739 \n",
"17 0.306020 0.305047 0.303720 0.002595 \n",
"13 0.302061 0.298975 0.299385 0.002038 \n",
"14 0.291492 0.297456 0.296565 0.003830 \n",
"0 0.294715 0.292144 0.294509 0.001852 \n",
"16 0.287134 0.286320 0.283948 0.003945 \n",
"3 0.272693 0.278427 0.275651 0.002344 \n",
"12 0.186707 0.193406 0.187141 0.004948 \n",
"15 0.172025 0.171455 0.167960 0.005350 \n",
"\n",
" rank_test_score \n",
"7 1 \n",
"10 2 \n",
"19 3 \n",
"6 4 \n",
"22 5 \n",
"11 6 \n",
"23 7 \n",
"9 8 \n",
"20 9 \n",
"8 10 \n",
"18 11 \n",
"1 12 \n",
"4 13 \n",
"21 14 \n",
"5 15 \n",
"2 16 \n",
"17 17 \n",
"13 18 \n",
"14 19 \n",
"0 20 \n",
"16 21 \n",
"3 22 \n",
"12 23 \n",
"15 24 "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.model_selection import GridSearchCV\n",
"\n",
"optimisation_pipeline = GridSearchCV(\n",
" create_pipeline(),\n",
" {\n",
" \"vectorizer__min_df\": [5, 20, 100],\n",
" \"vectorizer__max_df\": [0.05, 0.1],\n",
" \"classifier__alpha\": [0.5, 1],\n",
" \"classifier__fit_prior\": [True, False],\n",
" },\n",
" scoring=\"f1_macro\",\n",
" cv=3,\n",
" n_jobs=-1,\n",
" verbose=1,\n",
")\n",
"optimisation_pipeline.fit(X, y)\n",
"\n",
"results = pd.DataFrame(optimisation_pipeline.cv_results_)\n",
"results.sort_values(\"rank_test_score\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>Pipeline(steps=[(&#x27;vectorizer&#x27;,\n",
" TfidfVectorizer(max_df=0.05, min_df=20, sublinear_tf=True)),\n",
" (&#x27;classifier&#x27;, MultinomialNB(alpha=0.5, fit_prior=False))])</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 sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" ><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">Pipeline</label><div class=\"sk-toggleable__content\"><pre>Pipeline(steps=[(&#x27;vectorizer&#x27;,\n",
" TfidfVectorizer(max_df=0.05, min_df=20, sublinear_tf=True)),\n",
" (&#x27;classifier&#x27;, MultinomialNB(alpha=0.5, fit_prior=False))])</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" ><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">TfidfVectorizer</label><div class=\"sk-toggleable__content\"><pre>TfidfVectorizer(max_df=0.05, min_df=20, sublinear_tf=True)</pre></div></div></div><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" ><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">MultinomialNB</label><div class=\"sk-toggleable__content\"><pre>MultinomialNB(alpha=0.5, fit_prior=False)</pre></div></div></div></div></div></div></div>"
],
"text/plain": [
"Pipeline(steps=[('vectorizer',\n",
" TfidfVectorizer(max_df=0.05, min_df=20, sublinear_tf=True)),\n",
" ('classifier', MultinomialNB(alpha=0.5, fit_prior=False))])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn import set_config\n",
"\n",
"set_config(display=\"diagram\")\n",
"\n",
"classifier = create_pipeline()\n",
"classifier.set_params(**optimisation_pipeline.best_params_)\n",
"classifier.fit(X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export the model using GreatAI"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[38;5;39m2022-06-25 11:25:32,714 | INFO | Copying file for small-domain-prediction-0\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:32,737 | INFO | Compressing small-domain-prediction-0\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,050 | INFO | Uploading /tmp/tmpgerx8x95/small-domain-prediction-0.tar.gz to Mongo (GridFS)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,107 | INFO | Uploading small-domain-prediction-0.tar.gz 0.26/1.85 MB (14.2%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,109 | INFO | Uploading small-domain-prediction-0.tar.gz 0.52/1.85 MB (28.3%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,112 | INFO | Uploading small-domain-prediction-0.tar.gz 0.78/1.85 MB (42.5%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,114 | INFO | Uploading small-domain-prediction-0.tar.gz 1.04/1.85 MB (56.6%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,116 | INFO | Uploading small-domain-prediction-0.tar.gz 1.31/1.85 MB (70.8%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,117 | INFO | Uploading small-domain-prediction-0.tar.gz 1.57/1.85 MB (84.9%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,120 | INFO | Uploading small-domain-prediction-0.tar.gz 1.83/1.85 MB (99.1%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,120 | INFO | Uploading small-domain-prediction-0.tar.gz 1.85/1.85 MB (100.0%)\u001b[0m\n",
"\u001b[38;5;39m2022-06-25 11:25:33,124 | INFO | Model small-domain-prediction uploaded with version 0\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'small-domain-prediction:0'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from great_ai import save_model\n",
"\n",
"\n",
"save_model(classifier, key=\"small-domain-prediction\", keep_last_n=5)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.4 ('.env': venv)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "02dd6d3afbfa9fbbe1037d64ad9014965528a1ccad21929d6e72f466389a68ad"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}