thesis-prediction-ml/building-features-from-text-data/15d-Bigrams_CountVectorizer_TfidfTransformer_NaiveBayesClassifier.ipynb
2023-06-18 14:45:35 +02:00

430 lines
11 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",
"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": 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": "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>521705</th>\n",
" <td>14</td>\n",
" <td>Woodstock (novel)</td>\n",
" <td>Woodstock or The Cavalier. A Tale of the Year...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>373693</th>\n",
" <td>10</td>\n",
" <td>Cryptophagidae</td>\n",
" <td>Cryptophagidae is a family of beetles with re...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>333934</th>\n",
" <td>9</td>\n",
" <td>Dehliz-e Yek</td>\n",
" <td>Dehliz-e Yek (Persian: دهليزيك‎ also Romanize...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>125305</th>\n",
" <td>4</td>\n",
" <td>Roland Osabutey</td>\n",
" <td>Roland Osabutey (born 11 March 1980) is a Gha...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>100866</th>\n",
" <td>3</td>\n",
" <td>Ann Voskamp</td>\n",
" <td>Ann Voskamp (born August 10 1973 in Listowel ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>58320</th>\n",
" <td>2</td>\n",
" <td>Robert Bateman High School</td>\n",
" <td>Robert Bateman High School (also known as Rob...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>419470</th>\n",
" <td>11</td>\n",
" <td>Vriesea languida</td>\n",
" <td>Vriesea languida is a species of the genus Vr...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>164430</th>\n",
" <td>5</td>\n",
" <td>Gilbert Wellington Ostrom</td>\n",
" <td>Gilbert Wellington Ostrom (June 1837 – Decemb...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>426098</th>\n",
" <td>11</td>\n",
" <td>Banara regia</td>\n",
" <td>Banara regia is a species of plant in the Sal...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>281236</th>\n",
" <td>8</td>\n",
" <td>Calder River (Western Australia)</td>\n",
" <td>For other Rivers Calder see River Calder (dis...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Label Name \\\n",
"521705 14 Woodstock (novel) \n",
"373693 10 Cryptophagidae \n",
"333934 9 Dehliz-e Yek \n",
"125305 4 Roland Osabutey \n",
"100866 3 Ann Voskamp \n",
"58320 2 Robert Bateman High School \n",
"419470 11 Vriesea languida \n",
"164430 5 Gilbert Wellington Ostrom \n",
"426098 11 Banara regia \n",
"281236 8 Calder River (Western Australia) \n",
"\n",
" Text \n",
"521705 Woodstock or The Cavalier. A Tale of the Year... \n",
"373693 Cryptophagidae is a family of beetles with re... \n",
"333934 Dehliz-e Yek (Persian: دهليزيك‎ also Romanize... \n",
"125305 Roland Osabutey (born 11 March 1980) is a Gha... \n",
"100866 Ann Voskamp (born August 10 1973 in Listowel ... \n",
"58320 Robert Bateman High School (also known as Rob... \n",
"419470 Vriesea languida is a species of the genus Vr... \n",
"164430 Gilbert Wellington Ostrom (June 1837 – Decemb... \n",
"426098 Banara regia is a species of plant in the Sal... \n",
"281236 For other Rivers Calder see River Calder (dis... "
]
},
"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": [],
"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": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(10000, 217434)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count_vectorizer = CountVectorizer(min_df=0, max_df=80, ngram_range=(2, 2))\n",
"\n",
"feature_vector = count_vectorizer.fit_transform(X)\n",
"\n",
"feature_vector.shape"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(10000, 217434)"
]
},
"execution_count": 11,
"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": 12,
"metadata": {},
"outputs": [],
"source": [
"X_dense = feature_vector.todense()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(10000, 217434)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X_dense.shape"
]
},
{
"cell_type": "code",
"execution_count": 14,
"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": 15,
"metadata": {},
"outputs": [],
"source": [
"clf = GaussianNB().fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 7, 12, 6, ..., 1, 12, 3])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_pred = clf.predict(x_test)\n",
"y_pred"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Length of testing data: 2000\n",
"accuracy_count : 1778\n",
"accuracy_score : 0.889\n",
"precision_score : 0.8902761517734769\n",
"recall_score : 0.889\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
}