Merge branch 'jonas'
This commit is contained in:
commit
9a1bda0316
@ -205,24 +205,14 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Exercises:"
|
"## Exercise 01:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Exercise 01\n"
|
"### Performance 1\n"
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 10,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"accs = [0] * 5\n",
|
|
||||||
"names = [\"M1\", \"M2\", \"M3\", \"M4\", \"M5\"]\n"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -235,28 +225,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 7,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"start training…\n",
|
|
||||||
"training done\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"annotated_sent = nltk.corpus.treebank.tagged_sents()\n",
|
"def model_01(X,y,tX,ty, max_size=1000):\n",
|
||||||
"\n",
|
|
||||||
"X,y,tX,ty = create_training_and_test_set(annotated_sentences=annotated_sent, \n",
|
|
||||||
" relative_cutoff=0.8)\n",
|
|
||||||
"\n",
|
|
||||||
" #classifier = DecisionTreeClassifier(criterion='entropy')\n",
|
" #classifier = DecisionTreeClassifier(criterion='entropy')\n",
|
||||||
" from sklearn.neural_network import MLPClassifier\n",
|
" from sklearn.neural_network import MLPClassifier\n",
|
||||||
"model01_clf = train_classifier(X,y,MLPClassifier(),max_size=10000)\n",
|
" model01_clf = train_classifier(X,y,MLPClassifier(),max_size=1000)\n",
|
||||||
"accs[0] = test_classifier(clf=clf, tX=tX, ty=ty)"
|
" return test_classifier(clf=model01_clf, tX=tX, ty=ty)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -266,79 +243,198 @@
|
|||||||
"#### Model 02"
|
"#### Model 02"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 13,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"0"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 13,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"accs[1]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 8,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"'\\nimport matplotlib.pyplot as plt\\nimport numpy as np\\n\\nweights = clf.named_steps[\\'classifier\\'].feature_importances_\\nlabels = clf.named_steps[\\'vectorizer\\'].get_feature_names()\\n\\n#sort\\nweights, labels = (list(t) for t in zip(*sorted(zip(weights, labels))))\\n\\n#fig_1, ax_1 = plt.subplots()\\n#plt.bar(np.arange(len(weights)), weights)\\n#plt.xticks(np.arange(len(weights)), labels, rotation=90)\\n#plt.show()\\n\\nprint(\"Most important features:\")\\npprint.pprint(list(reversed(labels[-20:])))\\nprint(\"with weights: \")\\npprint.pprint(list(reversed(weights[-20:])))\\n'"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 8,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"'''\n",
|
"def model_02(tX,ty):\n",
|
||||||
"import matplotlib.pyplot as plt\n",
|
" m2_y = nltk.pos_tag([w['word'] for w in tX])\n",
|
||||||
"import numpy as np\n",
|
" # compare results\n",
|
||||||
"\n",
|
" n_correct = sum((1 if m2_y[i][1] == ty[i] else 0) for i in range(len(ty)))\n",
|
||||||
"weights = clf.named_steps['classifier'].feature_importances_\n",
|
" return n_correct / len(ty)"
|
||||||
"labels = clf.named_steps['vectorizer'].get_feature_names()\n",
|
|
||||||
"\n",
|
|
||||||
"#sort\n",
|
|
||||||
"weights, labels = (list(t) for t in zip(*sorted(zip(weights, labels))))\n",
|
|
||||||
"\n",
|
|
||||||
"#fig_1, ax_1 = plt.subplots()\n",
|
|
||||||
"#plt.bar(np.arange(len(weights)), weights)\n",
|
|
||||||
"#plt.xticks(np.arange(len(weights)), labels, rotation=90)\n",
|
|
||||||
"#plt.show()\n",
|
|
||||||
"\n",
|
|
||||||
"print(\"Most important features:\")\n",
|
|
||||||
"pprint.pprint(list(reversed(labels[-20:])))\n",
|
|
||||||
"print(\"with weights: \")\n",
|
|
||||||
"pprint.pprint(list(reversed(weights[-20:])))\n",
|
|
||||||
"'''"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"```\n",
|
"#### Model 03"
|
||||||
"from sklearn import tree\n",
|
]
|
||||||
"import graphviz\n",
|
},
|
||||||
"dot_data = tree.export_graphviz(clf.named_steps['classifier'], out_file='test',\n",
|
{
|
||||||
" feature_names=labels,\n",
|
"cell_type": "code",
|
||||||
" filled=True, rounded=True, \n",
|
"execution_count": 9,
|
||||||
" special_characters=True)\n",
|
"metadata": {},
|
||||||
"#graph = graphviz.Source(dot_data)\n",
|
"outputs": [],
|
||||||
"#graph\n",
|
"source": [
|
||||||
"```"
|
"def model_03(corpus_tagged, corpus_sents, cut=0.8):\n",
|
||||||
|
" \n",
|
||||||
|
" patterns = [(r'.*ing$', 'VBG'), (r'.*ed$', 'VBD'), (r'.*es$', 'VBZ'), (r'.*ould$', 'MD'), (r'.*\\'s$', 'NN$'), \n",
|
||||||
|
" (r'.*s$', 'NNS'), (r'^-?[0-9]+(.[0-9]+)?$', 'CD'), (r'.*', 'NN')]\n",
|
||||||
|
" \n",
|
||||||
|
" s = int(len(corpus_tagged) * cut)\n",
|
||||||
|
" train_sents = corpus_tagged[:s]\n",
|
||||||
|
" test_sents = corpus_tagged[s:]\n",
|
||||||
|
" \n",
|
||||||
|
" models = {\n",
|
||||||
|
" 'def_model': nltk.DefaultTagger('NN'),\n",
|
||||||
|
" 'regexp_model': nltk.RegexpTagger(patterns),\n",
|
||||||
|
" 'uni_model': nltk.UnigramTagger(train_sents),\n",
|
||||||
|
" 'bi_model': nltk.BigramTagger(train_sents),\n",
|
||||||
|
" 'tri_model': nltk.TrigramTagger(train_sents)\n",
|
||||||
|
" }\n",
|
||||||
|
" \n",
|
||||||
|
" performance = {}\n",
|
||||||
|
" for name,model in models.items():\n",
|
||||||
|
" performance[name] = model.evaluate(test_sents)\n",
|
||||||
|
" \n",
|
||||||
|
" return performance\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Applying models on Datasets"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"P1.1\n",
|
||||||
|
"start training…\n",
|
||||||
|
"training done\n",
|
||||||
|
"Accuracy: 0.7712959728529368\n",
|
||||||
|
"P1.2\n",
|
||||||
|
"P1.3\n",
|
||||||
|
"P1.4\n",
|
||||||
|
"start training…\n",
|
||||||
|
"training done\n",
|
||||||
|
"Accuracy: 0.6410882090489463\n",
|
||||||
|
"P1.5\n",
|
||||||
|
"P1.6\n",
|
||||||
|
"{'P1.1': 0.7712959728529368,\n",
|
||||||
|
" 'P1.2': 0.8936074654423873,\n",
|
||||||
|
" 'P1.3 -- bi_model': 0.1132791057437996,\n",
|
||||||
|
" 'P1.3 -- def_model': 0.1447677029791906,\n",
|
||||||
|
" 'P1.3 -- regexp_model': 0.24232746145017217,\n",
|
||||||
|
" 'P1.3 -- tri_model': 0.06736863116922003,\n",
|
||||||
|
" 'P1.3 -- uni_model': 0.8608213982733669,\n",
|
||||||
|
" 'P1.4': 0.6410882090489463,\n",
|
||||||
|
" 'P1.5': 0.6044583741861567,\n",
|
||||||
|
" 'P1.6 -- bi_model': 0.1132791057437996,\n",
|
||||||
|
" 'P1.6 -- def_model': 0.1447677029791906,\n",
|
||||||
|
" 'P1.6 -- regexp_model': 0.24232746145017217,\n",
|
||||||
|
" 'P1.6 -- tri_model': 0.06736863116922003,\n",
|
||||||
|
" 'P1.6 -- uni_model': 0.8608213982733669}\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"performances = {}\n",
|
||||||
|
"\n",
|
||||||
|
"treebank_tagged = nltk.corpus.treebank.tagged_sents()\n",
|
||||||
|
"treebank_sents = nltk.corpus.treebank.sents()\n",
|
||||||
|
"\n",
|
||||||
|
"brown_tagged = nltk.corpus.brown.tagged_sents()#(categories='news')\n",
|
||||||
|
"brown_sents = nltk.corpus.brown.sents()#(categories='news')\n",
|
||||||
|
"\n",
|
||||||
|
"X1,y1,tX1,ty1 = create_training_and_test_set(annotated_sentences=treebank_tagged, \n",
|
||||||
|
" relative_cutoff=0.8)\n",
|
||||||
|
"\n",
|
||||||
|
"X2,y2,tX2,ty2 = create_training_and_test_set(annotated_sentences=brown_tagged, \n",
|
||||||
|
" relative_cutoff=0.8)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"P1.1\")\n",
|
||||||
|
"performances['P1.1'] = model_01(X1,y1,tX1,ty1)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"P1.2\")\n",
|
||||||
|
"performances['P1.2'] = model_02(tX1,ty1)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"P1.3\")\n",
|
||||||
|
"p3 = model_03(treebank_tagged, treebank_sents)\n",
|
||||||
|
"for k,v in p3.items():\n",
|
||||||
|
" performances[\"P1.3 -- \" + k] = v\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"P1.4\")\n",
|
||||||
|
"performances['P1.4'] = model_01(X2,y2,tX2,ty2)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"P1.5\")\n",
|
||||||
|
"performances['P1.5'] = model_02(tX2,ty2)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"P1.6\")\n",
|
||||||
|
"p6 = model_03(brown_tagged, brown_sents)\n",
|
||||||
|
"for k,v in p3.items():\n",
|
||||||
|
" performances[\"P1.6 -- \" + k] = v\n",
|
||||||
|
"\n",
|
||||||
|
"pprint.pprint(performances)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Plotting Data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"application/vnd.jupyter.widget-view+json": {
|
||||||
|
"model_id": "80f8a41746b340f09c5d16a3071c7384",
|
||||||
|
"version_major": 2,
|
||||||
|
"version_minor": 0
|
||||||
|
},
|
||||||
|
"text/html": [
|
||||||
|
"<p>Failed to display Jupyter Widget of type <code>FigureCanvasNbAgg</code>.</p>\n",
|
||||||
|
"<p>\n",
|
||||||
|
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
|
||||||
|
" that the widgets JavaScript is still loading. If this message persists, it\n",
|
||||||
|
" likely means that the widgets JavaScript library is either not installed or\n",
|
||||||
|
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
|
||||||
|
" Widgets Documentation</a> for setup instructions.\n",
|
||||||
|
"</p>\n",
|
||||||
|
"<p>\n",
|
||||||
|
" If you're reading this message in another frontend (for example, a static\n",
|
||||||
|
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
|
||||||
|
" it may mean that your frontend doesn't currently support widgets.\n",
|
||||||
|
"</p>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"FigureCanvasNbAgg()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"#weights = clf.named_steps['classifier'].feature_importances_\n",
|
||||||
|
"#labels = clf.named_steps['vectorizer'].get_feature_names()\n",
|
||||||
|
"\n",
|
||||||
|
"#sort\n",
|
||||||
|
"#weights, labels = (list(t) for t in zip(*sorted(zip(weights, labels))))\n",
|
||||||
|
"\n",
|
||||||
|
"fig_1, ax_1 = plt.subplots()\n",
|
||||||
|
"plt.bar(np.arange(len(performances)), performances.values())\n",
|
||||||
|
"plt.xticks(np.arange(len(performances)), performances.keys(), rotation=30, ha='right')\n",
|
||||||
|
"plt.tight_layout()\n",
|
||||||
|
"plt.show()\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -365,7 +461,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.6.3"
|
"version": "3.6.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
Loading…
Reference in New Issue
Block a user