thesis-prediction-ml/building-features-from-text-data/13b-StopwordRemoval_CountVectorizer_NaiveBayesClassifier.ipynb
2023-06-18 14:45:35 +02:00

568 lines
14 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"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": [
"<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>524463</th>\n",
" <td>14</td>\n",
" <td>Open Sesame (manga)</td>\n",
" <td>Open Sesame is a Japanese manga series writte...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>214954</th>\n",
" <td>6</td>\n",
" <td>USS Porter (DDG-78)</td>\n",
" <td>USS Porter (DDG-78) is an Arleigh Burke-class...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>369188</th>\n",
" <td>10</td>\n",
" <td>Chionodes luctuella</td>\n",
" <td>Chionodes luctuella is a moth of the Gelechii...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15537</th>\n",
" <td>1</td>\n",
" <td>Rashtriya Chemicals &amp; Fertilizers</td>\n",
" <td>Rashtriya Chemicals &amp; Fertilizers Ltd. (RCF) ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>434426</th>\n",
" <td>11</td>\n",
" <td>Salix gilgiana</td>\n",
" <td>Salix gilgiana is a species of willow native ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>315086</th>\n",
" <td>8</td>\n",
" <td>Devlins Creek</td>\n",
" <td>Devlins Creek an urban watercourse that is pa...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>133194</th>\n",
" <td>4</td>\n",
" <td>Warren Donald</td>\n",
" <td>Warren Donald (born 7 October 1964) is an Eng...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>138090</th>\n",
" <td>4</td>\n",
" <td>Chris Hatcher (outfielder)</td>\n",
" <td>Christopher Kenneth Hatcher (born January 7 1...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>358873</th>\n",
" <td>9</td>\n",
" <td>Królewskie Ostrzeszów County</td>\n",
" <td>Królewskie [kruˈlɛfskʲɛ] is a village in the ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>108048</th>\n",
" <td>3</td>\n",
" <td>Hamuera Tamahau Mahupuku</td>\n",
" <td>Hamuera Tamahau Mahupuku (c.1842 – 14 January...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}