Compare commits

...

9 Commits

5 changed files with 201 additions and 189 deletions

View File

@@ -334,8 +334,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"len(ready_data1a)\n", "# len(ready_data1a)\n",
"# plt.pcolormesh(ready_data1[0])" "# plt.pcolormesh(ready_data1[0])\n",
"ready_data1a[0].max().max()"
] ]
}, },
{ {
@@ -345,7 +346,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"for i in range(6):\n", "for i in range(6):\n",
" plt.pcolormesh(ready_data1a[i])\n", " plt.pcolormesh(ready_data1a[i], cmap=\"jet\", vmax=0.03, vmin=0.0)\n",
" plt.colorbar() \n",
" plt.title(f'STFT Magnitude for case {i} sensor 1')\n", " plt.title(f'STFT Magnitude for case {i} sensor 1')\n",
" plt.xlabel(f'Frequency [Hz]')\n", " plt.xlabel(f'Frequency [Hz]')\n",
" plt.ylabel(f'Time [sec]')\n", " plt.ylabel(f'Time [sec]')\n",
@@ -535,8 +537,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# len(y_data[0])\n", "len(y_data[0])\n",
"y_data" "# y_data"
] ]
}, },
{ {
@@ -619,137 +621,15 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"accuracies1 = []\n", "def train_and_evaluate_model(model, model_name, sensor_label, x_train, y_train, x_test, y_test):\n",
"accuracies2 = []\n", " model.fit(x_train, y_train)\n",
"\n", " y_pred = model.predict(x_test)\n",
"\n", " accuracy = accuracy_score(y_test, y_pred) * 100\n",
"# 1. Random Forest\n", " return {\n",
"rf_model1 = RandomForestClassifier()\n", " \"model\": model_name,\n",
"rf_model1.fit(x_train1, y_train)\n", " \"sensor\": sensor_label,\n",
"rf_pred1 = rf_model1.predict(x_test1)\n", " \"accuracy\": accuracy\n",
"acc1 = accuracy_score(y_test, rf_pred1) * 100\n", " }"
"accuracies1.append(acc1)\n",
"# format with color coded if acc1 > 90\n",
"acc1 = f\"\\033[92m{acc1:.2f}\\033[00m\" if acc1 > 90 else f\"{acc1:.2f}\"\n",
"print(\"Random Forest Accuracy for sensor 1:\", acc1)\n",
"rf_model2 = RandomForestClassifier()\n",
"rf_model2.fit(x_train2, y_train)\n",
"rf_pred2 = rf_model2.predict(x_test2)\n",
"acc2 = accuracy_score(y_test, rf_pred2) * 100\n",
"accuracies2.append(acc2)\n",
"# format with color coded if acc2 > 90\n",
"acc2 = f\"\\033[92m{acc2:.2f}\\033[00m\" if acc2 > 90 else f\"{acc2:.2f}\"\n",
"print(\"Random Forest Accuracy for sensor 2:\", acc2)\n",
"# print(rf_pred)\n",
"# print(y_test)\n",
"\n",
"# 2. Bagged Trees\n",
"bagged_model1 = BaggingClassifier(estimator=DecisionTreeClassifier(), n_estimators=10)\n",
"bagged_model1.fit(x_train1, y_train)\n",
"bagged_pred1 = bagged_model1.predict(x_test1)\n",
"acc1 = accuracy_score(y_test, bagged_pred1) * 100\n",
"accuracies1.append(acc1)\n",
"# format with color coded if acc1 > 90\n",
"acc1 = f\"\\033[92m{acc1:.2f}\\033[00m\" if acc1 > 90 else f\"{acc1:.2f}\"\n",
"print(\"Bagged Trees Accuracy for sensor 1:\", acc1)\n",
"bagged_model2 = BaggingClassifier(estimator=DecisionTreeClassifier(), n_estimators=10)\n",
"bagged_model2.fit(x_train2, y_train)\n",
"bagged_pred2 = bagged_model2.predict(x_test2)\n",
"acc2 = accuracy_score(y_test, bagged_pred2) * 100\n",
"accuracies2.append(acc2)\n",
"# format with color coded if acc2 > 90\n",
"acc2 = f\"\\033[92m{acc2:.2f}\\033[00m\" if acc2 > 90 else f\"{acc2:.2f}\"\n",
"print(\"Bagged Trees Accuracy for sensor 2:\", acc2)\n",
"\n",
"# 3. Decision Tree\n",
"dt_model = DecisionTreeClassifier()\n",
"dt_model.fit(x_train1, y_train)\n",
"dt_pred1 = dt_model.predict(x_test1)\n",
"acc1 = accuracy_score(y_test, dt_pred1) * 100\n",
"accuracies1.append(acc1)\n",
"# format with color coded if acc1 > 90\n",
"acc1 = f\"\\033[92m{acc1:.2f}\\033[00m\" if acc1 > 90 else f\"{acc1:.2f}\"\n",
"print(\"Decision Tree Accuracy for sensor 1:\", acc1)\n",
"dt_model2 = DecisionTreeClassifier()\n",
"dt_model2.fit(x_train2, y_train)\n",
"dt_pred2 = dt_model2.predict(x_test2)\n",
"acc2 = accuracy_score(y_test, dt_pred2) * 100\n",
"accuracies2.append(acc2)\n",
"# format with color coded if acc2 > 90\n",
"acc2 = f\"\\033[92m{acc2:.2f}\\033[00m\" if acc2 > 90 else f\"{acc2:.2f}\"\n",
"print(\"Decision Tree Accuracy for sensor 2:\", acc2)\n",
"\n",
"# 4. KNeighbors\n",
"knn_model = KNeighborsClassifier()\n",
"knn_model.fit(x_train1, y_train)\n",
"knn_pred1 = knn_model.predict(x_test1)\n",
"acc1 = accuracy_score(y_test, knn_pred1) * 100\n",
"accuracies1.append(acc1)\n",
"# format with color coded if acc1 > 90\n",
"acc1 = f\"\\033[92m{acc1:.2f}\\033[00m\" if acc1 > 90 else f\"{acc1:.2f}\"\n",
"print(\"KNeighbors Accuracy for sensor 1:\", acc1)\n",
"knn_model2 = KNeighborsClassifier()\n",
"knn_model2.fit(x_train2, y_train)\n",
"knn_pred2 = knn_model2.predict(x_test2)\n",
"acc2 = accuracy_score(y_test, knn_pred2) * 100\n",
"accuracies2.append(acc2)\n",
"# format with color coded if acc2 > 90\n",
"acc2 = f\"\\033[92m{acc2:.2f}\\033[00m\" if acc2 > 90 else f\"{acc2:.2f}\"\n",
"print(\"KNeighbors Accuracy for sensor 2:\", acc2)\n",
"\n",
"# 5. Linear Discriminant Analysis\n",
"lda_model = LinearDiscriminantAnalysis()\n",
"lda_model.fit(x_train1, y_train)\n",
"lda_pred1 = lda_model.predict(x_test1)\n",
"acc1 = accuracy_score(y_test, lda_pred1) * 100\n",
"accuracies1.append(acc1)\n",
"# format with color coded if acc1 > 90\n",
"acc1 = f\"\\033[92m{acc1:.2f}\\033[00m\" if acc1 > 90 else f\"{acc1:.2f}\"\n",
"print(\"Linear Discriminant Analysis Accuracy for sensor 1:\", acc1)\n",
"lda_model2 = LinearDiscriminantAnalysis()\n",
"lda_model2.fit(x_train2, y_train)\n",
"lda_pred2 = lda_model2.predict(x_test2)\n",
"acc2 = accuracy_score(y_test, lda_pred2) * 100\n",
"accuracies2.append(acc2)\n",
"# format with color coded if acc2 > 90\n",
"acc2 = f\"\\033[92m{acc2:.2f}\\033[00m\" if acc2 > 90 else f\"{acc2:.2f}\"\n",
"print(\"Linear Discriminant Analysis Accuracy for sensor 2:\", acc2)\n",
"\n",
"# 6. Support Vector Machine\n",
"svm_model = SVC()\n",
"svm_model.fit(x_train1, y_train)\n",
"svm_pred1 = svm_model.predict(x_test1)\n",
"acc1 = accuracy_score(y_test, svm_pred1) * 100\n",
"accuracies1.append(acc1)\n",
"# format with color coded if acc1 > 90\n",
"acc1 = f\"\\033[92m{acc1:.2f}\\033[00m\" if acc1 > 90 else f\"{acc1:.2f}\"\n",
"print(\"Support Vector Machine Accuracy for sensor 1:\", acc1)\n",
"svm_model2 = SVC()\n",
"svm_model2.fit(x_train2, y_train)\n",
"svm_pred2 = svm_model2.predict(x_test2)\n",
"acc2 = accuracy_score(y_test, svm_pred2) * 100\n",
"accuracies2.append(acc2)\n",
"# format with color coded if acc2 > 90\n",
"acc2 = f\"\\033[92m{acc2:.2f}\\033[00m\" if acc2 > 90 else f\"{acc2:.2f}\"\n",
"print(\"Support Vector Machine Accuracy for sensor 2:\", acc2)\n",
"\n",
"# 7. XGBoost\n",
"xgboost_model = XGBClassifier()\n",
"xgboost_model.fit(x_train1, y_train)\n",
"xgboost_pred1 = xgboost_model.predict(x_test1)\n",
"acc1 = accuracy_score(y_test, xgboost_pred1) * 100\n",
"accuracies1.append(acc1)\n",
"# format with color coded if acc1 > 90\n",
"acc1 = f\"\\033[92m{acc1:.2f}\\033[00m\" if acc1 > 90 else f\"{acc1:.2f}\"\n",
"print(\"XGBoost Accuracy:\", acc1)\n",
"xgboost_model2 = XGBClassifier()\n",
"xgboost_model2.fit(x_train2, y_train)\n",
"xgboost_pred2 = xgboost_model2.predict(x_test2)\n",
"acc2 = accuracy_score(y_test, xgboost_pred2) * 100\n",
"accuracies2.append(acc2)\n",
"# format with color coded if acc2 > 90\n",
"acc2 = f\"\\033[92m{acc2:.2f}\\033[00m\" if acc2 > 90 else f\"{acc2:.2f}\"\n",
"print(\"XGBoost Accuracy:\", acc2)"
] ]
}, },
{ {
@@ -758,8 +638,59 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"print(accuracies1)\n", "# Define models for sensor1\n",
"print(accuracies2)" "models_sensor1 = {\n",
" # \"Random Forest\": RandomForestClassifier(),\n",
" # \"Bagged Trees\": BaggingClassifier(estimator=DecisionTreeClassifier(), n_estimators=10),\n",
" # \"Decision Tree\": DecisionTreeClassifier(),\n",
" # \"KNN\": KNeighborsClassifier(),\n",
" # \"LDA\": LinearDiscriminantAnalysis(),\n",
" \"SVM\": SVC(),\n",
" \"XGBoost\": XGBClassifier()\n",
"}\n",
"\n",
"results_sensor1 = []\n",
"for name, model in models_sensor1.items():\n",
" res = train_and_evaluate_model(model, name, \"sensor1\", x_train1, y_train, x_test1, y_test)\n",
" results_sensor1.append(res)\n",
" print(f\"{name} on sensor1: Accuracy = {res['accuracy']:.2f}%\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"models_sensor2 = {\n",
" # \"Random Forest\": RandomForestClassifier(),\n",
" # \"Bagged Trees\": BaggingClassifier(estimator=DecisionTreeClassifier(), n_estimators=10),\n",
" # \"Decision Tree\": DecisionTreeClassifier(),\n",
" # \"KNN\": KNeighborsClassifier(),\n",
" # \"LDA\": LinearDiscriminantAnalysis(),\n",
" \"SVM\": SVC(),\n",
" \"XGBoost\": XGBClassifier()\n",
"}\n",
"\n",
"results_sensor2 = []\n",
"for name, model in models_sensor2.items():\n",
" res = train_and_evaluate_model(model, name, \"sensor2\", x_train2, y_train, x_test2, y_test)\n",
" results_sensor2.append(res)\n",
" print(f\"{name} on sensor2: Accuracy = {res['accuracy']:.2f}%\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"all_results = {\n",
" \"sensor1\": results_sensor1,\n",
" \"sensor2\": results_sensor2\n",
"}\n",
"\n",
"print(all_results)"
] ]
}, },
{ {
@@ -771,36 +702,48 @@
"import numpy as np\n", "import numpy as np\n",
"import matplotlib.pyplot as plt\n", "import matplotlib.pyplot as plt\n",
"\n", "\n",
"models = [rf_model, bagged_model, dt_model, knn_model, lda_model, svm_model, xgboost_model]\n", "def prepare_plot_data(results_dict):\n",
"model_names = [\"Random Forest\", \"Bagged Trees\", \"Decision Tree\", \"KNN\", \"LDA\", \"SVM\", \"XGBoost\"]\n", " # Gather unique model names\n",
" models_set = {entry['model'] for sensor in results_dict.values() for entry in sensor}\n",
" models = sorted(list(models_set))\n",
" \n", " \n",
"bar_width = 0.35 # Width of each bar\n", " # Create dictionaries mapping sensor -> accuracy list ordered by model name\n",
"index = np.arange(len(model_names)) # Index for the bars\n", " sensor_accuracies = {}\n",
" for sensor, entries in results_dict.items():\n",
" # Build a mapping: model -> accuracy for the given sensor\n",
" mapping = {entry['model']: entry['accuracy'] for entry in entries}\n",
" # Order the accuracies consistent with the sorted model names\n",
" sensor_accuracies[sensor] = [mapping.get(model, 0) for model in models]\n",
" \n", " \n",
"# Plotting the bar graph\n", " return models, sensor_accuracies\n",
"plt.figure(figsize=(14, 8))\n",
"\n", "\n",
"# Bar plot for Sensor 1\n", "def plot_accuracies(models, sensor_accuracies):\n",
"plt.bar(index, accuracies1, width=bar_width, color='blue', label='Sensor 1')\n", " bar_width = 0.35\n",
" x = np.arange(len(models))\n",
" sensors = list(sensor_accuracies.keys())\n",
" \n", " \n",
"# Bar plot for Sensor 2\n", " plt.figure(figsize=(10, 6))\n",
"plt.bar(index + bar_width, accuracies2, width=bar_width, color='orange', label='Sensor 2')\n", " # Assume two sensors for plotting grouped bars\n",
" plt.bar(x - bar_width/2, sensor_accuracies[sensors[0]], width=bar_width, color='blue', label=sensors[0])\n",
" plt.bar(x + bar_width/2, sensor_accuracies[sensors[1]], width=bar_width, color='orange', label=sensors[1])\n",
" \n", " \n",
"# Add values on top of each bar\n", " # Add text labels on top of bars\n",
"for i, acc1, acc2 in zip(index, accuracies1, accuracies2):\n", " for i, (a1, a2) in enumerate(zip(sensor_accuracies[sensors[0]], sensor_accuracies[sensors[1]])):\n",
" plt.text(i, acc1 + .1, f'{acc1:.2f}%', ha='center', va='bottom', color='black')\n", " plt.text(x[i] - bar_width/2, a1 + 0.1, f\"{a1:.2f}%\", ha='center', va='bottom', color='black')\n",
" plt.text(i + bar_width, acc2 + 1, f'{acc2:.2f}%', ha='center', va='bottom', color='black')\n", " plt.text(x[i] + bar_width/2, a2 + 0.1, f\"{a2:.2f}%\", ha='center', va='bottom', color='black')\n",
" \n", " \n",
"# Customize the plot\n", " plt.xlabel('Model Name')\n",
"plt.xlabel('Model Name →')\n", " plt.ylabel('Accuracy (%)')\n",
"plt.ylabel('Accuracy →')\n", " plt.title('Accuracy of Classifiers for Each Sensor')\n",
"plt.title('Accuracy of classifiers for Sensors 1 and 2 with 513 features')\n", " plt.xticks(x, models)\n",
"plt.xticks(index + bar_width / 2, model_names) # Set x-tick positions\n",
" plt.legend()\n", " plt.legend()\n",
"plt.ylim(0, 100)\n", " plt.ylim(0, 105)\n",
" plt.tight_layout()\n",
" plt.show()\n",
"\n", "\n",
"# Show the plot\n", "# Use the functions\n",
"plt.show()\n" "models, sensor_accuracies = prepare_plot_data(all_results)\n",
"plot_accuracies(models, sensor_accuracies)\n"
] ]
}, },
{ {

View File

View File

@@ -0,0 +1,78 @@
% % A new command that enables us to enter bi-lingual (Slovene and English) terms
% % syntax: \addterm[options]{label}{Slovene}{Slovene first use}{English}{Slovene
% % description}
% \newcommand{\addterm}[6][]{
% \newglossaryentry{#2}{
% name={#3 (angl.\ #5)},
% first={#4 (\emph{#5})},
% text={#3},
% sort={#3},
% description={#6},
% #1 % pass additional options to \newglossaryentry
% }
% }
% % A new command that enables us to enter (English) acronyms with bi-lingual
% % (Slovene and English) long versions
% % syntax: \addacronym[options]{label}{abbreviation}{Slovene long}{Slovene first
% % use long}{English long}{Slovene description}
% \newcommand{\addacronym}[7][]{
% % Create the main glossary entry with \newacronym
% % \newacronym[key-val list]{label}{abbrv}{long}
% \newacronym[
% name={#4 (angl.\ #6,\ #3)},
% first={\emph{#5} (angl.\ \emph{#6},\ \emph{#3})},
% sort={#4},
% description={#7},
% #1 % pass additional options to \newglossaryentry
% ]
% {#2}{#3}{#4}
% % Create a cross-reference from the abbreviation to the main glossary entry by
% % creating an auxiliary glossary entry (note: we set the label of this entry
% % to '<original label>_auxiliary' to avoid clashes)
% \newglossaryentry{#2_auxiliary}{
% name={#3},
% sort={#3},
% description={\makefirstuc{#6}},
% see=[See:]{#2}
% }
% }
% % Change the text of the cross-reference links to the Slovene long version.
% \renewcommand*{\glsseeitemformat}[1]{\emph{\acrlong{#1}}.}
% Define the Indonesian term and link it to the English term
\newglossaryentry{jaringansaraf}{
name=Jaringan Saraf,
description={The Indonesian term for \gls{nn}}
}
% \newglossaryentry{pemelajaranmesin}{
% name=Pemelajaran Mesin,
% description={Lihat \gls{machinelearning}}
% }
% Define the English term and link it to its acronym
\newglossaryentry{neuralnetwork}{
name=Neural Network,
description={A computational model inspired by the human brain, see \gls{nn}}
}
% \newglossaryentry{machinelearning}{
% name=Machine Learning,
% description={A program or system that trains a model from input data. The trained model can make useful predictions from new (never-before-seen) data drawn from the same distribution as the one used to train the model.}}
% \newglossaryentry{pemelajaranmesin}{
% name={pemelajaran mesin (angl.\ #5)},
% first={pemelajaran mesin (\emph{machine learning})},
% text={pemelajaran mesin},
% sort={ },
% description={#6},
% #1 % pass additional options to \newglossaryentry
% }
\longnewglossaryentry{machinelearning}{name={machine learning}}
{A program or system that trains a model from input data. The trained model can make useful predictions from new (never-before-seen) data drawn from the same distribution as the one used to train the model.}
\newterm[see={machinelearning}]{pemelajaranmesin}
% \newglossaryentry{pemelajaran mesin}{}
% \addterm{machinelearning}{pemelajaran mesin}{pemelajaran mesin}{machine learning}{A program or system that trains a model from input data. The trained model can make useful predictions from new (never-before-seen) data drawn from the same distribution as the one used to train the model.}
\newacronym
[description={statistical pattern recognition technique}]
{svm}{SVM}{support vector machine}

View File

@@ -16,22 +16,19 @@
\input{preamble/macros} \input{preamble/macros}
\begin{document} \begin{document}
\input{frontmatter/maketitle}
\maketitle \input{frontmatter/maketitle_secondary}
\frontmatter \frontmatter
\input{frontmatter/approval}\clearpage % \input{frontmatter/approval}\clearpage
\input{frontmatter/originality}\clearpage % \input{frontmatter/originality}\clearpage
\input{frontmatter/acknowledgement}\clearpage % \input{frontmatter/acknowledgement}\clearpage
\tableofcontents \tableofcontents
\clearpage \clearpage
\mainmatter \mainmatter
\pagestyle{fancyplain} \pagestyle{fancyplain}
% Include content
\include{content/abstract}
\include{content/introduction}
\include{chapters/01_introduction} \include{chapters/01_introduction}
\include{content/chapter2} \include{chapters/id/02_literature_review/index}
\include{content/conclusion} \include{chapters/id/03_methodology/index}
% Bibliography % Bibliography
% \bibliographystyle{IEEEtran} % \bibliographystyle{IEEEtran}

View File

@@ -24,15 +24,14 @@
\RequirePackage{svg} % Allows including SVG images directly \RequirePackage{svg} % Allows including SVG images directly
\RequirePackage{indentfirst} % Makes first paragraph after headings indented \RequirePackage{indentfirst} % Makes first paragraph after headings indented
\RequirePackage{float} % Provides [H] option to force figure/table placement \RequirePackage{float} % Provides [H] option to force figure/table placement
\RequirePackage[style=apa, backend=biber, language=indonesian]{biblatex}
% Polyglossia set language % Polyglossia set language
+ \setdefaultlanguage[variant=indonesian]{malay} % Proper Indonesian language setup \setdefaultlanguage[variant=indonesian]{malay} % Proper Indonesian language setup
+ \setotherlanguage{english} % Enables English as secondary language \setotherlanguage{english} % Enables English as secondary language
\DefineBibliographyStrings{english}{% % Customizes bibliography text
+ \DefineBibliographyStrings{english}{% % Customizes bibliography text andothers={dkk\adddot}, % Changes "et al." to "dkk."
+ andothers={dkk\adddot}, % Changes "et al." to "dkk." pages={hlm\adddot}, % Changes "pp." to "hlm."
+ pages={hlm\adddot}, % Changes "pp." to "hlm." }
+ }
% Conditionally load the watermark package and settings % Conditionally load the watermark package and settings
\if@draftmark \if@draftmark
@@ -56,8 +55,6 @@
\setsansfont{Arial} \setsansfont{Arial}
\setmonofont{Courier New} \setmonofont{Courier New}
% Metadata commands
\input{metadata}
\newcommand{\setthesisinfo}[7]{% \newcommand{\setthesisinfo}[7]{%
\renewcommand{\thesistitle}{#1}% \renewcommand{\thesistitle}{#1}%
@@ -112,9 +109,6 @@
% \titlespacing*{\chapter}{0pt}{-10pt}{20pt} % \titlespacing*{\chapter}{0pt}{-10pt}{20pt}
% Redefine \maketitle
\renewcommand{\maketitle}{\input{frontmatter/maketitle}}
% Chapter & Section format % Chapter & Section format
\renewcommand{\cftchapfont}{\normalsize\MakeUppercase} \renewcommand{\cftchapfont}{\normalsize\MakeUppercase}
% \renewcommand{\cftsecfont}{} % \renewcommand{\cftsecfont}{}