{ "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.text import TfidfTransformer\n", "\n", "from sklearn.naive_bayes import GaussianNB" ] }, { "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
44587912Lucas (album)Lucas is the second album by Ghostly Internat...
1208544Pat McGeheePatrick Henry McGehee (July 2 1888 – December...
44960812One Too Many HeartsOne Too Many Hearts is the third EP by Americ...
1715125Arken ArystanovArkén Kenesbékovich Arystánov (Kazakh: Аркен ...
268681Cineplex Odeon FilmsCineplex Odeon Films (also known as Cineplex ...
40249811SteirachneSteirachne is a genus of grass in the Poaceae...
1023533Harry ThumannHarry Thumann (28 February 1952 – 2001) was a...
3373859Chah SheykhChah Sheykh (Persian: چاه شيخ‎ also Romanized...
48517613Emmtan-MaganEm Magan is a 2006 Tamil drama film directed ...
2951298Hot Springs RangeThe Hot Springs Range is a mountain range in ...
\n", "
" ], "text/plain": [ " Label Name \\\n", "445879 12 Lucas (album) \n", "120854 4 Pat McGehee \n", "449608 12 One Too Many Hearts \n", "171512 5 Arken Arystanov \n", "26868 1 Cineplex Odeon Films \n", "402498 11 Steirachne \n", "102353 3 Harry Thumann \n", "337385 9 Chah Sheykh \n", "485176 13 Emmtan-Magan \n", "295129 8 Hot Springs Range \n", "\n", " Text \n", "445879 Lucas is the second album by Ghostly Internat... \n", "120854 Patrick Henry McGehee (July 2 1888 – December... \n", "449608 One Too Many Hearts is the third EP by Americ... \n", "171512 Arkén Kenesbékovich Arystánov (Kazakh: Аркен ... \n", "26868 Cineplex Odeon Films (also known as Cineplex ... \n", "402498 Steirachne is a genus of grass in the Poaceae... \n", "102353 Harry Thumann (28 February 1952 – 2001) was a... \n", "337385 Chah Sheykh (Persian: چاه شيخ‎ also Romanized... \n", "485176 Em Magan is a 2006 Tamil drama film directed ... \n", "295129 The Hot Springs Range is a mountain range in ... " ] }, "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": 11, "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, 48318)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_vectorizer = CountVectorizer()\n", "\n", "feature_vector = count_vectorizer.fit_transform(X)\n", "\n", "feature_vector.shape" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 48318)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tfidf_transformer = TfidfTransformer()\n", "\n", "feature_vector = tfidf_transformer.fit_transform(feature_vector)\n", "\n", "feature_vector.shape" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "X_dense = feature_vector.todense()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 48318)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_dense.shape" ] }, { "cell_type": "code", "execution_count": 17, "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": 18, "metadata": {}, "outputs": [], "source": [ "clf = GaussianNB().fit(x_train, y_train)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 1, 3, ..., 12, 10, 5])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred = clf.predict(x_test)\n", "y_pred" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Length of testing data: 2000\n", "accuracy_count : 1438\n", "accuracy_score : 0.719\n", "precision_score : 0.7284791775245764\n", "recall_score : 0.719\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 }