This commit is contained in:
s434695 2021-07-06 13:14:54 +02:00
parent e3855d5411
commit 8fe98c622c
2 changed files with 184 additions and 22 deletions

View File

@ -1,6 +1,188 @@
{
"cells": [],
"metadata": {},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fc8ba0a3",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from nltk.tokenize import word_tokenize\n",
"from nltk import pos_tag\n",
"from nltk.corpus import stopwords\n",
"from nltk.stem import WordNetLemmatizer\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from collections import defaultdict\n",
"from nltk.corpus import wordnet as wn\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn import model_selection, naive_bayes, svm\n",
"from sklearn.metrics import accuracy_score\n",
"from sklearn.pipeline import make_pipeline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b082c37a",
"metadata": {},
"outputs": [],
"source": [
"with open(\"train/in.tsv\", encoding='utf-8') as f:\n",
" x_train = f.readlines()\n",
"\n",
"\n",
"with open(\"train/expected.tsv\", encoding='utf-8') as f:\n",
" y_train = f.readlines()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2ccc7d71",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"289579"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(x_train)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f6bd3ba5",
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(500)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "eefd7b1b",
"metadata": {},
"outputs": [],
"source": [
"with open(\"svm/in.tsv\", encoding='utf-8') as f:\n",
" x_dev = f.readlines()\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d50a164b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"289579"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"len(y_train)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6696a4d0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 0, 0, ..., 0, 0, 1], dtype=int64)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_train = LabelEncoder().fit_transform(y_train)\n",
"y_train"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "5488f657",
"metadata": {},
"outputs": [],
"source": [
"pipeline = make_pipeline(TfidfVectorizer(),svm.SVC())"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "ddbb1608",
"metadata": {},
"outputs": [],
"source": [
"model = pipeline.fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b286bec3",
"metadata": {},
"outputs": [],
"source": [
"prediction = model.predict(x_dev)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0b1fac69",
"metadata": {},
"outputs": [],
"source": [
"np.savetxt(\"svm/outfull.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": 5
}

View File

@ -79,26 +79,6 @@
" x_dev = f.readlines()\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "269c7b94",
"metadata": {},
"outputs": [],
"source": [
"x_train = x_train[:100000]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f3f6e9f0",
"metadata": {},
"outputs": [],
"source": [
"y_train = y_train[:100000]"
]
},
{
"cell_type": "code",
"execution_count": 8,