Files
labs/Module-5/4.HMMs_Implementation.ipynb

106 lines
2.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "qkY7YQDJBlH9"
},
"outputs": [],
"source": [
"ob_types = ('W','N')\n",
"# W for working and N for not working\n",
"states = ('L','M')\n",
"#M is the malicious and L is Legitimate"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9uDupL1ZCYWK"
},
"outputs": [],
"source": [
"import numpy as np\n",
"observation = ('W','W','W','N')\n",
"start = np.matrix('0.1 0.9')\n",
"transition = np.matrix('0.7 0.3 ; 0.1 0.9')\n",
"emission = np.matrix('0.2 0.8 ; 0.4 0.6')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "b2nYpyLCDwRE",
"outputId": "3eb61a16-39a3-4cdf-82b4-975249b7ac75"
},
"outputs": [],
"source": [
"from hidden_markov import hmm\n",
"ob_types = ('W','N')\n",
"# W for working and N for not working\n",
"states = ('L','M')\n",
"#M is the malicious and L is Legitimate\n",
"observation = ('W','W','W','N')\n",
"start = np.matrix('0.1 0.9')\n",
"transition = np.matrix('0.7 0.3 ; 0.1 0.9')\n",
"emission = np.matrix('0.2 0.8 ; 0.4 0.6')\n",
"_hmm = hmm(states, ob_types, start, transition,emission)\n",
"print(\"Forward algorithm\")\n",
"print(_hmm.forward_algo(observation))\n",
"print(\"Viterbi algorithm: \")\n",
"print(_hmm.viterbi(observation))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tU3B6LwuEjdg"
},
"source": [
"Forward algorithm gives us the probability of an observerd sequence in the HMM\n",
"\n",
"Viterbi algorithm is used to find out the most likely sequence of hidden states that can generate the given set of observations"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "HMMs Implementation udemy.ipynb",
"provenance": []
},
"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.8.8"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}