-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathML Lab 7.enc
1 lines (1 loc) · 8.45 KB
/
ML Lab 7.enc
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"ML Lab 7.ipynb","provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"code","metadata":{"id":"rxSqNBEq7pCL","colab_type":"code","colab":{}},"source":["import numpy as np\n","import sklearn as skl\n","import pandas as pd"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"1mGmJYOM8Hp8","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":224},"outputId":"dd2ffb5e-a145-42b8-913c-8f66d2271904","executionInfo":{"status":"ok","timestamp":1575876306369,"user_tz":-330,"elapsed":4108,"user":{"displayName":"Vishal Nayak","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mCTt-Ft0O7FZnNLCmaQV0OjoOQY-UE6BjazKy6QeTc=s64","userId":"05008619478332300738"}}},"source":["!wget 'http://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/processed.hungarian.data'"],"execution_count":50,"outputs":[{"output_type":"stream","text":["--2019-12-09 07:25:04-- http://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/processed.hungarian.data\n","Resolving archive.ics.uci.edu (archive.ics.uci.edu)... 128.195.10.252\n","Connecting to archive.ics.uci.edu (archive.ics.uci.edu)|128.195.10.252|:80... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 10263 (10K) [application/x-httpd-php]\n","Saving to: ‘processed.hungarian.data’\n","\n","\rprocessed.hungarian 0%[ ] 0 --.-KB/s \rprocessed.hungarian 100%[===================>] 10.02K --.-KB/s in 0s \n","\n","2019-12-09 07:25:04 (228 MB/s) - ‘processed.hungarian.data’ saved [10263/10263]\n","\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"OpwZ-pOx8JuK","colab_type":"code","outputId":"6a5edcee-2977-4538-9f72-1409dde50792","executionInfo":{"status":"ok","timestamp":1575876306373,"user_tz":-330,"elapsed":4073,"user":{"displayName":"Vishal Nayak","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mCTt-Ft0O7FZnNLCmaQV0OjoOQY-UE6BjazKy6QeTc=s64","userId":"05008619478332300738"}},"colab":{"base_uri":"https://localhost:8080/","height":199}},"source":["names = ['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang', 'a', 'b', 'c', 'd', 'heartdisease']\n","heartDisease = pd.read_csv('processed.hungarian.data', names = names)\n","del heartDisease['a']\n","del heartDisease['b']\n","del heartDisease['c']\n","del heartDisease['d']\n","del heartDisease['cp']\n","heartDisease.head()"],"execution_count":51,"outputs":[{"output_type":"execute_result","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>age</th>\n"," <th>sex</th>\n"," <th>trestbps</th>\n"," <th>chol</th>\n"," <th>fbs</th>\n"," <th>restecg</th>\n"," <th>thalach</th>\n"," <th>exang</th>\n"," <th>heartdisease</th>\n"," </tr>\n"," </thead>\n"," <tbody>\n"," <tr>\n"," <th>0</th>\n"," <td>28</td>\n"," <td>1</td>\n"," <td>130</td>\n"," <td>132</td>\n"," <td>0</td>\n"," <td>2</td>\n"," <td>185</td>\n"," <td>0</td>\n"," <td>0</td>\n"," </tr>\n"," <tr>\n"," <th>1</th>\n"," <td>29</td>\n"," <td>1</td>\n"," <td>120</td>\n"," <td>243</td>\n"," <td>0</td>\n"," <td>0</td>\n"," <td>160</td>\n"," <td>0</td>\n"," <td>0</td>\n"," </tr>\n"," <tr>\n"," <th>2</th>\n"," <td>29</td>\n"," <td>1</td>\n"," <td>140</td>\n"," <td>?</td>\n"," <td>0</td>\n"," <td>0</td>\n"," <td>170</td>\n"," <td>0</td>\n"," <td>0</td>\n"," </tr>\n"," <tr>\n"," <th>3</th>\n"," <td>30</td>\n"," <td>0</td>\n"," <td>170</td>\n"," <td>237</td>\n"," <td>0</td>\n"," <td>1</td>\n"," <td>170</td>\n"," <td>0</td>\n"," <td>0</td>\n"," </tr>\n"," <tr>\n"," <th>4</th>\n"," <td>31</td>\n"," <td>0</td>\n"," <td>100</td>\n"," <td>219</td>\n"," <td>0</td>\n"," <td>1</td>\n"," <td>150</td>\n"," <td>0</td>\n"," <td>0</td>\n"," </tr>\n"," </tbody>\n","</table>\n","</div>"],"text/plain":[" age sex trestbps chol fbs restecg thalach exang heartdisease\n","0 28 1 130 132 0 2 185 0 0\n","1 29 1 120 243 0 0 160 0 0\n","2 29 1 140 ? 0 0 170 0 0\n","3 30 0 170 237 0 1 170 0 0\n","4 31 0 100 219 0 1 150 0 0"]},"metadata":{"tags":[]},"execution_count":51}]},{"cell_type":"code","metadata":{"id":"XgCmaw6R-T6w","colab_type":"code","outputId":"762ac8ec-dc59-4f64-8634-6ff0c963d7cd","executionInfo":{"status":"ok","timestamp":1575876306377,"user_tz":-330,"elapsed":4059,"user":{"displayName":"Vishal Nayak","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mCTt-Ft0O7FZnNLCmaQV0OjoOQY-UE6BjazKy6QeTc=s64","userId":"05008619478332300738"}},"colab":{"base_uri":"https://localhost:8080/","height":68}},"source":["heartDisease.columns"],"execution_count":52,"outputs":[{"output_type":"execute_result","data":{"text/plain":["Index(['age', 'sex', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang',\n"," 'heartdisease'],\n"," dtype='object')"]},"metadata":{"tags":[]},"execution_count":52}]},{"cell_type":"code","metadata":{"id":"-5EtNHQlFPpT","colab_type":"code","outputId":"034056aa-e1e2-40f5-c1e1-9d265871a20d","executionInfo":{"status":"ok","timestamp":1575876309926,"user_tz":-330,"elapsed":7590,"user":{"displayName":"Vishal Nayak","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mCTt-Ft0O7FZnNLCmaQV0OjoOQY-UE6BjazKy6QeTc=s64","userId":"05008619478332300738"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["!pip install pgmpy"],"execution_count":53,"outputs":[{"output_type":"stream","text":["Requirement already satisfied: pgmpy in /usr/local/lib/python3.6/dist-packages (0.1.9)\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"h1QbAScR-kot","colab_type":"code","colab":{}},"source":["from pgmpy.models import BayesianModel\n","from pgmpy.estimators import MaximumLikelihoodEstimator\n","\n","model = BayesianModel([('age', 'trestbps'), ('age', 'fbs'), ('sex', 'trestbps'), \n"," ('exang', 'trestbps'),('trestbps','heartdisease'),('fbs','heartdisease'),\n"," ('heartdisease','restecg'),('heartdisease','thalach'),('heartdisease','chol')])"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"12wYa62v73_P","colab_type":"code","colab":{}},"source":["model.fit(heartDisease, estimator = MaximumLikelihoodEstimator)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"JIY6-0kP74J9","colab_type":"code","colab":{}},"source":["from pgmpy.inference import VariableElimination\n","HeartDisease_infer = VariableElimination(model)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"O7a7oBHt8k_X","colab_type":"code","outputId":"67cd065a-c60f-45a6-811b-da7a2482fc59","executionInfo":{"status":"ok","timestamp":1575877148032,"user_tz":-330,"elapsed":2676,"user":{"displayName":"Vishal Nayak","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mCTt-Ft0O7FZnNLCmaQV0OjoOQY-UE6BjazKy6QeTc=s64","userId":"05008619478332300738"}},"colab":{"base_uri":"https://localhost:8080/","height":204}},"source":["q = HeartDisease_infer.query(variables = ['heartdisease'], evidence = {'age': 28})\n","print('\\n\\n',q)"],"execution_count":65,"outputs":[{"output_type":"stream","text":["Finding Elimination Order: : 100%|██████████| 7/7 [00:00<00:00, 1011.30it/s]\n","Eliminating: fbs: 100%|██████████| 7/7 [00:00<00:00, 251.88it/s]"],"name":"stderr"},{"output_type":"stream","text":["\n","\n"," +-----------------+---------------------+\n","| heartdisease | phi(heartdisease) |\n","+=================+=====================+\n","| heartdisease(0) | 0.6505 |\n","+-----------------+---------------------+\n","| heartdisease(1) | 0.3495 |\n","+-----------------+---------------------+\n"],"name":"stdout"},{"output_type":"stream","text":["\n"],"name":"stderr"}]}]}