paranormal-or-skeptic-ISI-p.../bayes.ipynb
2021-06-30 14:14:34 +02:00

131 lines
2.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from sklearn.naive_bayes import MultinomialNB\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.feature_extraction.text import TfidfVectorizer"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"with open(\"train/in.tsv\") as f:\n",
" x_train = f.readlines()\n",
"\n",
"with open(\"train/expected.tsv\") as f:\n",
" y_train = f.readlines()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 0, 0, ..., 0, 0, 1])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_train = LabelEncoder().fit_transform(y_train)\n",
"y_train"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"pipeline = make_pipeline(TfidfVectorizer(),MultinomialNB())"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"model = pipeline.fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"with open(\"dev-0/in.tsv\") as f:\n",
" x_dev = f.readlines()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"prediction = model.predict(x_dev)\n",
"np.savetxt(\"dev-0/out.tsv\", prediction, fmt='%d')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"with open(\"test-A/in.tsv\") as f:\n",
" x_test = f.readlines()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"prediction = model.predict(x_test)\n",
"np.savetxt(\"test-A/out.tsv\", prediction, fmt='%d')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}