{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "from sklearn.model_selection import train_test_split\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.feature_extraction import text \n", "\n", "from sklearn.naive_bayes import GaussianNB\n", "\n", "from nltk.tokenize import word_tokenize\n", "from nltk.probability import FreqDist" ] }, { "cell_type": "code", "execution_count": 3, "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": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(559999, 3)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df.shape" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df['Label'].unique()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "dbpedia_df = dbpedia_df.sample(10000, replace=False)" ] }, { "cell_type": "code", "execution_count": 7, "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
42752011Guzmania remyiGuzmania remyi is a species of plant in the B...
52214514Villa AuroreVilla Aurore is a novel written in French by ...
46255212Monk's CasinoMonk's Casino is a live album by German free ...
24601Kiss TechnologyKiss Technology is an entertainment technolog...
837433S.M. ZakirS.M. Zakir (born 4 February 1969 in Kota Bhar...
55488714New York Law JournalThe New York Law Journal founded in 1888 is a...
2708577Latham United Methodist ChurchLatham United Methodist Church is a historic ...
3239899NalavadiNalavadi is a village in Dharwad district in ...
1933525Richard Ottley (judge)Sir Richard Ottley was the 5th Chief Justice ...
781042Umatilla High School (Oregon)Umatilla High School is a public high school ...
\n", "
" ], "text/plain": [ " Label Name \\\n", "427520 11 Guzmania remyi \n", "522145 14 Villa Aurore \n", "462552 12 Monk's Casino \n", "2460 1 Kiss Technology \n", "83743 3 S.M. Zakir \n", "554887 14 New York Law Journal \n", "270857 7 Latham United Methodist Church \n", "323989 9 Nalavadi \n", "193352 5 Richard Ottley (judge) \n", "78104 2 Umatilla High School (Oregon) \n", "\n", " Text \n", "427520 Guzmania remyi is a species of plant in the B... \n", "522145 Villa Aurore is a novel written in French by ... \n", "462552 Monk's Casino is a live album by German free ... \n", "2460 Kiss Technology is an entertainment technolog... \n", "83743 S.M. Zakir (born 4 February 1969 in Kota Bhar... \n", "554887 The New York Law Journal founded in 1888 is a... \n", "270857 Latham United Methodist Church is a historic ... \n", "323989 Nalavadi is a village in Dharwad district in ... \n", "193352 Sir Richard Ottley was the 5th Chief Justice ... \n", "78104 Umatilla High School is a public high school ... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df.sample(10)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 3)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dbpedia_df.shape" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "X = dbpedia_df['Text']\n", "\n", "Y = dbpedia_df['Label']" ] }, { "cell_type": "code", "execution_count": 10, "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": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 217709)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_vectorizer = CountVectorizer(ngram_range=(2, 2))\n", "\n", "feature_vector = count_vectorizer.fit_transform(X)\n", "\n", "feature_vector.shape" ] }, { "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, 217709)" ] }, "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": [], "source": [ "clf = GaussianNB().fit(x_train, y_train)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 1, 5, ..., 4, 7, 8])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred = clf.predict(x_test)\n", "y_pred" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Length of testing data: 2000\n", "accuracy_count : 1812\n", "accuracy_score : 0.906\n", "precision_score : 0.9071432275572098\n", "recall_score : 0.906\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 }