{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "from sklearn.model_selection import train_test_split\n", "\n", "from sklearn.metrics import accuracy_score\n", "from sklearn.metrics import precision_score\n", "from sklearn.metrics import recall_score\n", "\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "from sklearn.naive_bayes import GaussianNB" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "dbpedia_df = pd.read_csv('./datasets/dbpedia_csv/train.csv', \n", " skiprows=1, names = ['Label', 'Name', 'Text'])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(559999, 3)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DBPedia classes\n", "\n", "- Company\n", "- EducationalInstitution\n", "- Artist\n", "- Athlete\n", "- OfficeHolder\n", "- MeanOfTransportation\n", "- Building\n", "- NaturalPlace\n", "- Village\n", "- Animal\n", "- Plant\n", "- Album\n", "- Film\n", "- WrittenWork" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df['Label'].unique()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "dbpedia_df = dbpedia_df.sample(10000, replace=False)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LabelNameText
52446314Open Sesame (manga)Open Sesame is a Japanese manga series writte...
2149546USS Porter (DDG-78)USS Porter (DDG-78) is an Arleigh Burke-class...
36918810Chionodes luctuellaChionodes luctuella is a moth of the Gelechii...
155371Rashtriya Chemicals & FertilizersRashtriya Chemicals & Fertilizers Ltd. (RCF) ...
43442611Salix gilgianaSalix gilgiana is a species of willow native ...
3150868Devlins CreekDevlins Creek an urban watercourse that is pa...
1331944Warren DonaldWarren Donald (born 7 October 1964) is an Eng...
1380904Chris Hatcher (outfielder)Christopher Kenneth Hatcher (born January 7 1...
3588739Królewskie Ostrzeszów CountyKrólewskie [kruˈlɛfskʲɛ] is a village in the ...
1080483Hamuera Tamahau MahupukuHamuera Tamahau Mahupuku (c.1842 – 14 January...
\n", "
" ], "text/plain": [ " Label Name \\\n", "524463 14 Open Sesame (manga) \n", "214954 6 USS Porter (DDG-78) \n", "369188 10 Chionodes luctuella \n", "15537 1 Rashtriya Chemicals & Fertilizers \n", "434426 11 Salix gilgiana \n", "315086 8 Devlins Creek \n", "133194 4 Warren Donald \n", "138090 4 Chris Hatcher (outfielder) \n", "358873 9 Królewskie Ostrzeszów County \n", "108048 3 Hamuera Tamahau Mahupuku \n", "\n", " Text \n", "524463 Open Sesame is a Japanese manga series writte... \n", "214954 USS Porter (DDG-78) is an Arleigh Burke-class... \n", "369188 Chionodes luctuella is a moth of the Gelechii... \n", "15537 Rashtriya Chemicals & Fertilizers Ltd. (RCF) ... \n", "434426 Salix gilgiana is a species of willow native ... \n", "315086 Devlins Creek an urban watercourse that is pa... \n", "133194 Warren Donald (born 7 October 1964) is an Eng... \n", "138090 Christopher Kenneth Hatcher (born January 7 1... \n", "358873 Królewskie [kruˈlɛfskʲɛ] is a village in the ... \n", "108048 Hamuera Tamahau Mahupuku (c.1842 – 14 January... " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df.sample(10)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 3)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df.shape" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "X = dbpedia_df['Text']\n", "\n", "Y = dbpedia_df['Label']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "77514 Phillips University was a private coeducation...\n", "341059 Nowiny [nɔˈvinɨ] is a village in the administ...\n", "517834 Capricious Summer (Czech: Rozmarné léto) is a...\n", "215388 Maumelle Ordnance Works Locomotive 1 is a gas...\n", "250923 Grace Reformed Church is a historic church lo...\n", "Name: Text, dtype: object" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.head()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "77514 2\n", "341059 9\n", "517834 13\n", "215388 6\n", "250923 7\n", "Name: Label, dtype: int64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y.head()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def summarize_classification(y_test, y_pred):\n", "\n", " acc = accuracy_score(y_test, y_pred, normalize=True)\n", " num_acc = accuracy_score(y_test, y_pred, normalize=False)\n", " prec = precision_score(y_test, y_pred, average='weighted')\n", " recall = recall_score(y_test, y_pred, average='weighted')\n", " \n", " print(\"Length of testing data: \", len(y_test))\n", " print(\"accuracy_count : \" , num_acc)\n", " print(\"accuracy_score : \" , acc)\n", " print(\"precision_score : \" , prec)\n", " print(\"recall_score : \", recall)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 47886)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_vectorizer = CountVectorizer(stop_words='english')\n", "\n", "feature_vector = count_vectorizer.fit_transform(X)\n", "\n", "feature_vector.shape" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " (0, 15329)\t1\n", " (0, 28116)\t1\n", " (0, 39106)\t1\n", " (0, 42005)\t1\n", " (0, 14627)\t1\n", " (0, 19453)\t1\n", " (0, 34423)\t1\n", " (0, 7888)\t1\n", " (0, 30095)\t1\n", " (0, 40200)\t1\n", " (0, 19281)\t1\n", " (0, 36984)\t1\n", " (0, 17534)\t1\n", " (0, 9770)\t1\n", " (0, 42720)\t1\n", " (0, 20178)\t1\n", " (0, 9116)\t1\n", " (0, 12475)\t1\n", " (0, 9188)\t1\n", " (0, 9127)\t1\n", " (0, 2428)\t1\n", " (0, 786)\t1\n", " (0, 666)\t1\n", " (0, 39115)\t1\n", " (0, 42819)\t1\n", " (0, 29834)\t1\n", " (0, 14115)\t2\n", " (0, 24524)\t1\n", " (0, 13596)\t1\n", " (0, 19001)\t1\n", " (0, 20458)\t1\n", " (0, 9679)\t1\n", " (0, 32870)\t1\n", " (0, 42838)\t2\n", " (0, 31628)\t2\n" ] } ], "source": [ "print(feature_vector[0])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "X_dense = feature_vector.todense()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 47886)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_dense.shape" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "x_train, x_test, y_train, y_test = train_test_split(X_dense, Y, test_size = 0.2)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((8000, 47886), (2000, 47886))" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train.shape, x_test.shape" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((8000,), (2000,))" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_train.shape, y_test.shape" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "clf = GaussianNB().fit(x_train, y_train)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 8, 13, 4, ..., 6, 4, 13])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred = clf.predict(x_test)\n", "y_pred" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Length of testing data: 2000\n", "accuracy_count : 1472\n", "accuracy_score : 0.736\n", "precision_score : 0.746912711601029\n", "recall_score : 0.736\n" ] } ], "source": [ "summarize_classification(y_test, y_pred)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }