571 lines
14 KiB
Text
571 lines
14 KiB
Text
{
|
||
"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",
|
||
"\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": 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": "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": 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": [
|
||
"<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>Label</th>\n",
|
||
" <th>Name</th>\n",
|
||
" <th>Text</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>500996</th>\n",
|
||
" <td>13</td>\n",
|
||
" <td>The County Chairman</td>\n",
|
||
" <td>The County Chairman is a 1935 comedy film dir...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>40795</th>\n",
|
||
" <td>2</td>\n",
|
||
" <td>Our Lady of Mount Carmel High School (Baltimor...</td>\n",
|
||
" <td>Our Lady of Mount Carmel High School (OLMC HS...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>131453</th>\n",
|
||
" <td>4</td>\n",
|
||
" <td>Jim Kleinsasser</td>\n",
|
||
" <td>Jimmy Carter Kleinsasser (/ˈklaɪnsɑːsər/; bor...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>122291</th>\n",
|
||
" <td>4</td>\n",
|
||
" <td>August Klingler</td>\n",
|
||
" <td>August Klingler (24 February 1918 – 23 Novemb...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>38965</th>\n",
|
||
" <td>1</td>\n",
|
||
" <td>Fine Fare</td>\n",
|
||
" <td>Fine Fare was the name of a chain of supermar...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>89852</th>\n",
|
||
" <td>3</td>\n",
|
||
" <td>Desmond Devlin</td>\n",
|
||
" <td>Desmond Devlin is an American comedy writer. ...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>481930</th>\n",
|
||
" <td>13</td>\n",
|
||
" <td>A Sister to Assist 'Er (1938 film)</td>\n",
|
||
" <td>A Sister to Assist 'Er is a 1938 British come...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>95008</th>\n",
|
||
" <td>3</td>\n",
|
||
" <td>Megumi Makihara</td>\n",
|
||
" <td>Megumi Makihara (槇原めぐみ or 槙原めぐみ or 慎原めぐみ Maki...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>543571</th>\n",
|
||
" <td>14</td>\n",
|
||
" <td>Annals of Science</td>\n",
|
||
" <td>Annals of Science is a peer-reviewed academic...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>356791</th>\n",
|
||
" <td>9</td>\n",
|
||
" <td>Dula Gavabar</td>\n",
|
||
" <td>Dula Gavabar (Persian: دولاگوابر also Romani...</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" Label Name \\\n",
|
||
"500996 13 The County Chairman \n",
|
||
"40795 2 Our Lady of Mount Carmel High School (Baltimor... \n",
|
||
"131453 4 Jim Kleinsasser \n",
|
||
"122291 4 August Klingler \n",
|
||
"38965 1 Fine Fare \n",
|
||
"89852 3 Desmond Devlin \n",
|
||
"481930 13 A Sister to Assist 'Er (1938 film) \n",
|
||
"95008 3 Megumi Makihara \n",
|
||
"543571 14 Annals of Science \n",
|
||
"356791 9 Dula Gavabar \n",
|
||
"\n",
|
||
" Text \n",
|
||
"500996 The County Chairman is a 1935 comedy film dir... \n",
|
||
"40795 Our Lady of Mount Carmel High School (OLMC HS... \n",
|
||
"131453 Jimmy Carter Kleinsasser (/ˈklaɪnsɑːsər/; bor... \n",
|
||
"122291 August Klingler (24 February 1918 – 23 Novemb... \n",
|
||
"38965 Fine Fare was the name of a chain of supermar... \n",
|
||
"89852 Desmond Devlin is an American comedy writer. ... \n",
|
||
"481930 A Sister to Assist 'Er is a 1938 British come... \n",
|
||
"95008 Megumi Makihara (槇原めぐみ or 槙原めぐみ or 慎原めぐみ Maki... \n",
|
||
"543571 Annals of Science is a peer-reviewed academic... \n",
|
||
"356791 Dula Gavabar (Persian: دولاگوابر also Romani... "
|
||
]
|
||
},
|
||
"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": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"113209 Eisuke Yoshiyuki (吉行 エイスケ Yoshiyuki Eisuke Ma...\n",
|
||
"10443 Schroders plc is a British multinational asse...\n",
|
||
"152897 Patrice Ferri is a retired French association...\n",
|
||
"485929 Honey 2 is a dance film which is a sequel to ...\n",
|
||
"312910 Marjanah is an impact crater in the northern ...\n",
|
||
"Name: Text, dtype: object"
|
||
]
|
||
},
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"X.head()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"113209 3\n",
|
||
"10443 1\n",
|
||
"152897 4\n",
|
||
"485929 13\n",
|
||
"312910 8\n",
|
||
"Name: Label, dtype: int64"
|
||
]
|
||
},
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"Y.head()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"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, 48133)"
|
||
]
|
||
},
|
||
"execution_count": 13,
|
||
"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": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" (0, 44965)\t1\n",
|
||
" (0, 45159)\t1\n",
|
||
" (0, 24477)\t1\n",
|
||
" (0, 30174)\t1\n",
|
||
" (0, 5353)\t1\n",
|
||
" (0, 2617)\t2\n",
|
||
" (0, 4213)\t1\n",
|
||
" (0, 29184)\t1\n",
|
||
" (0, 770)\t1\n",
|
||
" (0, 41370)\t1\n",
|
||
" (0, 2229)\t1\n",
|
||
" (0, 22994)\t1\n",
|
||
" (0, 44731)\t1\n",
|
||
" (0, 22447)\t1\n",
|
||
" (0, 11806)\t1\n",
|
||
" (0, 3406)\t1\n",
|
||
" (0, 29564)\t1\n",
|
||
" (0, 3087)\t1\n",
|
||
" (0, 21905)\t1\n",
|
||
" (0, 38741)\t1\n",
|
||
" (0, 19410)\t3\n",
|
||
" (0, 32904)\t1\n",
|
||
" (0, 30043)\t1\n",
|
||
" (0, 20446)\t1\n",
|
||
" (0, 6733)\t1\n",
|
||
" (0, 18881)\t1\n",
|
||
" (0, 4662)\t2\n",
|
||
" (0, 21408)\t1\n",
|
||
" (0, 44526)\t4\n",
|
||
" (0, 696)\t1\n",
|
||
" (0, 21879)\t1\n",
|
||
" (0, 653)\t1\n",
|
||
" (0, 27)\t1\n",
|
||
" (0, 26417)\t1\n",
|
||
" (0, 47763)\t1\n",
|
||
" (0, 47850)\t1\n",
|
||
" (0, 45736)\t4\n",
|
||
" (0, 13969)\t3\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"print(feature_vector[0])"
|
||
]
|
||
},
|
||
{
|
||
"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, 48133)"
|
||
]
|
||
},
|
||
"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": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"((8000, 48133), (2000, 48133))"
|
||
]
|
||
},
|
||
"execution_count": 18,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"x_train.shape, x_test.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"((8000,), (2000,))"
|
||
]
|
||
},
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"y_train.shape, y_test.shape"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 20,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"clf = GaussianNB().fit(x_train, y_train)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"array([ 5, 10, 4, ..., 5, 1, 14])"
|
||
]
|
||
},
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"y_pred = clf.predict(x_test)\n",
|
||
"y_pred"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 22,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Length of testing data: 2000\n",
|
||
"accuracy_count : 1464\n",
|
||
"accuracy_score : 0.732\n",
|
||
"precision_score : 0.7480649828741391\n",
|
||
"recall_score : 0.732\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
|
||
}
|