260 lines
6.0 KiB
Plaintext
260 lines
6.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"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",
|
|
"nrows = 5000"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"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",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"5000\n",
|
|
"5000\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(len(df.index))\n",
|
|
"\n",
|
|
"print(len(topics.index))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"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",
|
|
" <th>2823</th>\n",
|
|
" <td>Use her own logic against her. Pharmaceutical...</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" 0\n",
|
|
"2823 Use her own logic against her. Pharmaceutical..."
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"df.sample()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"vectorizer = TfidfVectorizer(lowercase=True, stop_words=['english'])\n",
|
|
"X = vectorizer.fit_transform(df.to_numpy().ravel())\n",
|
|
"# vectorizer.get_feature_names_out()\n"
|
|
]
|
|
},
|
|
{
|
|
"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",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"tfidfVector = vectorizer.transform(df[0])\n",
|
|
"\n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"GaussianNB()"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"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",
|
|
"gnb.fit(tfidfVector.toarray(), topics)"
|
|
]
|
|
},
|
|
{
|
|
"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",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"testXPath = \"./dev-0/in.tsv.xz\"\n",
|
|
"testYPath = \"./dev-0/out.tsv\"\n",
|
|
"\n",
|
|
"testX = pd.read_csv(testXPath, sep='\\t', 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",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[0 1 1 ... 0 0 0]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"pred = gnb.predict(testXtfidfVector.toarray())\n",
|
|
"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": {
|
|
"hash": "1b132c2ed43285dcf39f6d01712959169a14a721cf314fe69015adab49bb1fd1"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3.8.10 64-bit",
|
|
"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.10"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|