Task 1 done

This commit is contained in:
Jonas Weinz 2018-05-02 21:06:19 +02:00
parent 2c625f7f92
commit 6c4358ced0

View File

@ -225,19 +225,9 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"start training…\n",
"training done\n",
"Accuracy: 0.768551324916413\n"
]
}
],
"outputs": [],
"source": [
"def model_01(X,y,tX,ty, max_size=1000):\n",
" #classifier = DecisionTreeClassifier(criterion='entropy')\n",
@ -255,17 +245,9 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.8936074654423873\n"
]
}
],
"outputs": [],
"source": [
"def model_02(tX,ty):\n",
" m2_y = nltk.pos_tag([w['word'] for w in tX])\n",
@ -283,7 +265,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@ -292,9 +274,9 @@
" 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_sents) * cut)\n",
" train_sents = corpus_sents[:size]\n",
" test_sents = corpus_sents[size:]\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",
@ -320,24 +302,139 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 10,
"metadata": {},
"outputs": [],
"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": [
"accs_p1 = [0] * 3\n",
"names_p1 = [\"P1.1\", \"P1.2\", \"P1.3\"]\n",
"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()\n",
"brown_sents = nltk.corpus.brown.sents()\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"
" 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"
]
},
{