s444417-paranormal-or-skept.../run.ipynb

260 lines
6.0 KiB
Plaintext
Raw Permalink Normal View History

2022-05-10 23:41:58 +02:00
{
"cells": [
{
"cell_type": "code",
2022-05-18 10:50:06 +02:00
"execution_count": 3,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [],
"source": [
"import lzma\n",
"import sys\n",
"from io import StringIO\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"import pandas as pd\n",
"import numpy\n",
"\n",
"pathX = \"./train/in.tsv.xz\"\n",
"# pathX = \"./train/in.tsv\"\n",
"pathY = \"./train/expected.tsv\"\n",
2022-05-18 10:50:06 +02:00
"nrows = 5000"
2022-05-10 23:41:58 +02:00
]
},
{
"cell_type": "code",
2022-05-18 10:50:06 +02:00
"execution_count": 4,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [],
"source": [
"# data = lzma.open(pathX, mode='rt', encoding='utf-8').read()\n",
"# stringIO = StringIO(data)\n",
"# df = pd.read_csv(stringIO, sep=\"\\t\", header=None)\n",
"df = pd.read_csv(pathX, sep='\\t', nrows=nrows, header=None)\n",
"df = df.drop(df.columns[1], axis=1)\n",
"topics = pd.read_csv(pathY, sep='\\t', nrows=nrows, header=None)"
]
},
{
"cell_type": "code",
2022-05-18 10:50:06 +02:00
"execution_count": 5,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-05-18 10:50:06 +02:00
"5000\n",
"5000\n"
2022-05-10 23:41:58 +02:00
]
}
],
"source": [
"print(len(df.index))\n",
"\n",
"print(len(topics.index))\n"
]
},
{
"cell_type": "code",
2022-05-18 10:50:06 +02:00
"execution_count": 6,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [
{
"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>0</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
2022-05-18 10:50:06 +02:00
" <th>2823</th>\n",
" <td>Use her own logic against her. Pharmaceutical...</td>\n",
2022-05-10 23:41:58 +02:00
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0\n",
2022-05-18 10:50:06 +02:00
"2823 Use her own logic against her. Pharmaceutical..."
2022-05-10 23:41:58 +02:00
]
},
2022-05-18 10:50:06 +02:00
"execution_count": 6,
2022-05-10 23:41:58 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.sample()"
]
},
{
"cell_type": "code",
2022-05-18 10:50:06 +02:00
"execution_count": 8,
2022-05-10 23:41:58 +02:00
"metadata": {},
2022-05-18 10:50:06 +02:00
"outputs": [],
2022-05-10 23:41:58 +02:00
"source": [
"vectorizer = TfidfVectorizer(lowercase=True, stop_words=['english'])\n",
"X = vectorizer.fit_transform(df.to_numpy().ravel())\n",
2022-05-18 10:50:06 +02:00
"# vectorizer.get_feature_names_out()\n"
2022-05-10 23:41:58 +02:00
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"# vectorizer.transform(\"Ala ma kotka\".lower().split())"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"df = df.reset_index()"
]
},
{
"cell_type": "code",
2022-05-18 10:50:06 +02:00
"execution_count": 9,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [],
"source": [
"tfidfVector = vectorizer.transform(df[0])\n",
"\n",
" "
]
},
{
"cell_type": "code",
2022-05-18 10:50:06 +02:00
"execution_count": 11,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2022-05-18 10:50:06 +02:00
"c:\\software\\python3\\lib\\site-packages\\sklearn\\utils\\validation.py:63: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
" return f(*args, **kwargs)\n"
2022-05-10 23:41:58 +02:00
]
},
{
"data": {
"text/plain": [
"GaussianNB()"
]
},
2022-05-18 10:50:06 +02:00
"execution_count": 11,
2022-05-10 23:41:58 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.model_selection import train_test_split\n",
"from sklearn.naive_bayes import GaussianNB\n",
"\n",
"gnb = GaussianNB()\n",
2022-05-18 10:50:06 +02:00
"gnb.fit(tfidfVector.toarray(), topics)"
2022-05-10 23:41:58 +02:00
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"testXPath = \"./dev-0/in.tsv.xz\"\n",
"testYPath = \"./dev-0/expected.tsv\"\n",
"\n",
"testX = pd.read_csv(testXPath, sep='\\t', nrows=nrows, header=None)\n",
"\n",
"testY = pd.read_csv(testYPath, sep='\\t', nrows=nrows, header=None)\n",
"testXtfidfVector = vectorizer.transform(testX[0])\n"
]
},
{
"cell_type": "code",
2022-05-18 10:57:49 +02:00
"execution_count": 14,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [],
"source": [
2022-05-18 10:50:06 +02:00
"testXPath = \"./dev-0/in.tsv.xz\"\n",
"testYPath = \"./dev-0/out.tsv\"\n",
2022-05-10 23:41:58 +02:00
"\n",
2022-05-18 10:57:49 +02:00
"testX = pd.read_csv(testXPath, sep='\\t', header=None)\n",
2022-05-10 23:41:58 +02:00
"\n",
"# testY = pd.read_csv(testYPath, sep='\\t', nrows=nrows, header=None)\n",
"testXtfidfVector = vectorizer.transform(testX[0])\n"
]
},
{
"cell_type": "code",
2022-05-18 10:57:49 +02:00
"execution_count": 15,
2022-05-10 23:41:58 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-05-18 10:57:49 +02:00
"[0 1 1 ... 0 0 0]\n"
2022-05-10 23:41:58 +02:00
]
}
],
"source": [
2022-05-18 10:50:06 +02:00
"pred = gnb.predict(testXtfidfVector.toarray())\n",
2022-05-10 23:41:58 +02:00
"print(pred)\n",
"\n",
"import csv\n",
"with open(testYPath, 'w', newline='') as f_output:\n",
" tsv_output = csv.writer(f_output, delimiter='\\n')\n",
" tsv_output.writerow(pred)"
]
}
],
"metadata": {
"interpreter": {
2022-05-18 10:50:06 +02:00
"hash": "1b132c2ed43285dcf39f6d01712959169a14a721cf314fe69015adab49bb1fd1"
2022-05-10 23:41:58 +02:00
},
"kernelspec": {
2022-05-18 10:50:06 +02:00
"display_name": "Python 3.8.10 64-bit",
2022-05-10 23:41:58 +02:00
"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",
2022-05-18 10:50:06 +02:00
"version": "3.8.10"
2022-05-10 23:41:58 +02:00
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}