405 lines
10 KiB
Text
405 lines
10 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"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 TfidfVectorizer\n",
|
|
"\n",
|
|
"from sklearn.naive_bayes import GaussianNB"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"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": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(559999, 3)"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"dbpedia_df.shape"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"dbpedia_df['Label'].unique()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"dbpedia_df = dbpedia_df.sample(10000, replace=False)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"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>252405</th>\n",
|
|
" <td>7</td>\n",
|
|
" <td>Prowers Bridge</td>\n",
|
|
" <td>The Prowers Bridge over the Arkansas River ne...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>504457</th>\n",
|
|
" <td>13</td>\n",
|
|
" <td>Que sera sera (film)</td>\n",
|
|
" <td>Que sera sera (Portuguese: Seja o que Deus Qu...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>270494</th>\n",
|
|
" <td>7</td>\n",
|
|
" <td>The Esplanade (Kenner Louisiana)</td>\n",
|
|
" <td>The Esplanade also known as the Esplanade Mal...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>546058</th>\n",
|
|
" <td>14</td>\n",
|
|
" <td>The 1974 Annual World's Best SF</td>\n",
|
|
" <td>The 1974 Annual World's Best SF is an antholo...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>461279</th>\n",
|
|
" <td>12</td>\n",
|
|
" <td>Just Go (album)</td>\n",
|
|
" <td>Just Go is the ninth studio album by American...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>277143</th>\n",
|
|
" <td>7</td>\n",
|
|
" <td>Steyning Methodist Church</td>\n",
|
|
" <td>Steyning Methodist Church is a Methodist plac...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>266867</th>\n",
|
|
" <td>7</td>\n",
|
|
" <td>Smith Estate (Ridge New York)</td>\n",
|
|
" <td>Smith Estate also known as Longwood Estate - ...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>283712</th>\n",
|
|
" <td>8</td>\n",
|
|
" <td>Yr Eifl</td>\n",
|
|
" <td>Yr Eifl is a mountain on the north coast of t...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>33861</th>\n",
|
|
" <td>1</td>\n",
|
|
" <td>IBM India</td>\n",
|
|
" <td>IBM India Private Limited is the Indian subsi...</td>\n",
|
|
" </tr>\n",
|
|
" <tr>\n",
|
|
" <th>70953</th>\n",
|
|
" <td>2</td>\n",
|
|
" <td>Loreto College of Rose-Hill</td>\n",
|
|
" <td>Loreto College Rose Hill is a private seconda...</td>\n",
|
|
" </tr>\n",
|
|
" </tbody>\n",
|
|
"</table>\n",
|
|
"</div>"
|
|
],
|
|
"text/plain": [
|
|
" Label Name \\\n",
|
|
"252405 7 Prowers Bridge \n",
|
|
"504457 13 Que sera sera (film) \n",
|
|
"270494 7 The Esplanade (Kenner Louisiana) \n",
|
|
"546058 14 The 1974 Annual World's Best SF \n",
|
|
"461279 12 Just Go (album) \n",
|
|
"277143 7 Steyning Methodist Church \n",
|
|
"266867 7 Smith Estate (Ridge New York) \n",
|
|
"283712 8 Yr Eifl \n",
|
|
"33861 1 IBM India \n",
|
|
"70953 2 Loreto College of Rose-Hill \n",
|
|
"\n",
|
|
" Text \n",
|
|
"252405 The Prowers Bridge over the Arkansas River ne... \n",
|
|
"504457 Que sera sera (Portuguese: Seja o que Deus Qu... \n",
|
|
"270494 The Esplanade also known as the Esplanade Mal... \n",
|
|
"546058 The 1974 Annual World's Best SF is an antholo... \n",
|
|
"461279 Just Go is the ninth studio album by American... \n",
|
|
"277143 Steyning Methodist Church is a Methodist plac... \n",
|
|
"266867 Smith Estate also known as Longwood Estate - ... \n",
|
|
"283712 Yr Eifl is a mountain on the north coast of t... \n",
|
|
"33861 IBM India Private Limited is the Indian subsi... \n",
|
|
"70953 Loreto College Rose Hill is a private seconda... "
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"dbpedia_df.sample(10)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(10000, 3)"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"dbpedia_df.shape"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"X = dbpedia_df['Text']\n",
|
|
"\n",
|
|
"Y = dbpedia_df['Label']"
|
|
]
|
|
},
|
|
{
|
|
"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, 48401)"
|
|
]
|
|
},
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tfidf_vectorizer = TfidfVectorizer()\n",
|
|
"\n",
|
|
"feature_vector = tfidf_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, 48401)"
|
|
]
|
|
},
|
|
"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([10, 7, 7, ..., 7, 4, 12])"
|
|
]
|
|
},
|
|
"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 : 1398\n",
|
|
"accuracy_score : 0.699\n",
|
|
"precision_score : 0.7100351307095724\n",
|
|
"recall_score : 0.699\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
|
|
}
|