sport-text-classification-ball/run.ipynb
2022-05-11 20:22:28 +02:00

117 lines
2.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "835530c2-0129-4b5f-a41c-f1870ca1307f",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sklearn\n",
"import pandas as pd\n",
"from sklearn.metrics import accuracy_score\n",
"from gzip import open as open_gz\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn.naive_bayes import MultinomialNB\n",
"from sklearn.pipeline import make_pipeline"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "5db73671-80b9-4099-85a0-08ecf77250d1",
"metadata": {},
"outputs": [],
"source": [
"def evaluation(x, path_out, model):\n",
" results = model.predict(x)\n",
"\n",
" with open(path_out, 'wt') as file:\n",
" for r in results:\n",
" file.write(str(r) + '\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2120f43e-d587-4481-a04c-dea9520cecec",
"metadata": {},
"outputs": [],
"source": [
"train = pd.read_csv('train/train.tsv', header = None, sep = '\\t', error_bad_lines = False)\n",
"\n",
"x_train = train[1]\n",
"y_train = train[0]\n",
"x_dev = pd.read_csv('dev-0/in.tsv',header = None, sep = '/t',engine = 'python')\n",
"x_dev = x_dev[0]\n",
"x_test = pd.read_csv('test-A/in.tsv',header = None, sep = '/t',engine = 'python')\n",
"x_test = x_test[0]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "cdfefa84-e535-48e8-844a-10e24b2a3555",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Pipeline(steps=[('tfidfvectorizer', TfidfVectorizer()),\n",
" ('multinomialnb', MultinomialNB())])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = make_pipeline(TfidfVectorizer(), MultinomialNB())\n",
"model.fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "e1b7fe0c-a21a-42cf-8cd4-46ee932d5282",
"metadata": {},
"outputs": [],
"source": [
"evaluation(x_dev,'dev-0/out.tsv', model)\n",
"evaluation(x_test,'test-A/out.tsv', model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a269ea0-eefd-4907-bf65-9bb53ad296b7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}