{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Study2-2.ipynb","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyMbSg9a+A+QNggJqS2HJI0c"},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"code","metadata":{"id":"9CyBAGpKonW1","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":127},"outputId":"b28eefb0-53bb-4bde-9bde-3020acc75373","executionInfo":{"status":"ok","timestamp":1584870466446,"user_tz":-540,"elapsed":41193,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["import pandas as pd\n","from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":1,"outputs":[{"output_type":"stream","text":["Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n","\n","Enter your authorization code:\n","··········\n","Mounted at /content/drive\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"QMh0VdaYo2xG","colab_type":"code","colab":{}},"source":["df = pd.read_csv('/content/drive/My Drive/Colab Notebooks/tensorflow/data/heart disease dataset.csv')"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"pD2MjeVDpR58","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":81},"outputId":"96534752-3912-450a-955e-47f5a20d3c01","executionInfo":{"status":"ok","timestamp":1584870571109,"user_tz":-540,"elapsed":4694,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["import numpy as np\n","import tensorflow as tf"],"execution_count":3,"outputs":[{"output_type":"display_data","data":{"text/html":["

\n","The default version of TensorFlow in Colab will soon switch to TensorFlow 2.x.
\n","We recommend you upgrade now \n","or ensure your notebook will continue to use TensorFlow 1.x via the %tensorflow_version 1.x magic:\n","more info.

\n"],"text/plain":[""]},"metadata":{"tags":[]}}]},{"cell_type":"code","metadata":{"id":"_KIdK5L-pamW","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":224},"outputId":"10c47737-9719-4baf-e1bd-0f9f389be0de","executionInfo":{"status":"ok","timestamp":1584870572933,"user_tz":-540,"elapsed":960,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["df.head()"],"execution_count":4,"outputs":[{"output_type":"execute_result","data":{"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
maleageeducationcurrentSmokercigsPerDayBPMedsprevalentStrokeprevalentHypdiabetestotCholsysBPdiaBPBMIheartRateglucoseTenYearCHD
01394.000.00.0000195.0106.070.026.9780.077.00
10462.000.00.0000250.0121.081.028.7395.076.00
21481.0120.00.0000245.0127.580.025.3475.070.00
30613.0130.00.0010225.0150.095.028.5865.0103.01
40463.0123.00.0000285.0130.084.023.1085.085.00
\n","
"],"text/plain":[" male age education currentSmoker ... BMI heartRate glucose TenYearCHD\n","0 1 39 4.0 0 ... 26.97 80.0 77.0 0\n","1 0 46 2.0 0 ... 28.73 95.0 76.0 0\n","2 1 48 1.0 1 ... 25.34 75.0 70.0 0\n","3 0 61 3.0 1 ... 28.58 65.0 103.0 1\n","4 0 46 3.0 1 ... 23.10 85.0 85.0 0\n","\n","[5 rows x 16 columns]"]},"metadata":{"tags":[]},"execution_count":4}]},{"cell_type":"code","metadata":{"id":"Z_nzzWl_pb7-","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":323},"outputId":"681fc2d3-4df6-4d8b-b7a4-4bdd1e9ff572","executionInfo":{"status":"ok","timestamp":1584870603426,"user_tz":-540,"elapsed":1141,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["df.dtypes"],"execution_count":6,"outputs":[{"output_type":"execute_result","data":{"text/plain":["male int64\n","age int64\n","education float64\n","currentSmoker int64\n","cigsPerDay float64\n","BPMeds float64\n","prevalentStroke int64\n","prevalentHyp int64\n","diabetes int64\n","totChol float64\n","sysBP float64\n","diaBP float64\n","BMI float64\n","heartRate float64\n","glucose float64\n","TenYearCHD int64\n","dtype: object"]},"metadata":{"tags":[]},"execution_count":6}]},{"cell_type":"code","metadata":{"id":"5OnrFwtwp19J","colab_type":"code","colab":{}},"source":["df = df.fillna(df.mean())"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"RnXOFkgXphZ0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":251},"outputId":"3625179c-fef0-46cd-c1de-67bc97dbfb53","executionInfo":{"status":"ok","timestamp":1584870686419,"user_tz":-540,"elapsed":798,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["data = df.values\n","print(data)"],"execution_count":9,"outputs":[{"output_type":"stream","text":["[[ 1. 39. 4. ... 80. 77.\n"," 0. ]\n"," [ 0. 46. 2. ... 95. 76.\n"," 0. ]\n"," [ 1. 48. 1. ... 75. 70.\n"," 0. ]\n"," ...\n"," [ 0. 48. 2. ... 84. 86.\n"," 0. ]\n"," [ 0. 44. 1. ... 86. 81.96675325\n"," 0. ]\n"," [ 0. 52. 2. ... 80. 107.\n"," 0. ]]\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"snfuyk4bqUSo","colab_type":"code","colab":{}},"source":["def min_max_scalar(data):\n"," numerator = data - np.min(data, 0)\n"," denominator = np.max(data, 0) - np.min(data, 0)\n"," return numerator / (denominator + 1e-7)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"q7NuzyYgppow","colab_type":"code","colab":{}},"source":["x_data = data[:, 0 : -1]\n","x_data = min_max_scalar(x_data)\n","y_data = data[:, [-1]]"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"bVAKQsOHqTS1","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":53},"outputId":"a8d262d2-e1b2-4776-9d3f-29b26a69bde5","executionInfo":{"status":"ok","timestamp":1584870964052,"user_tz":-540,"elapsed":557,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["print(x_data.shape)\n","print(y_data.shape)"],"execution_count":24,"outputs":[{"output_type":"stream","text":["(4238, 15)\n","(4238, 1)\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"zsu043KkqZgh","colab_type":"code","colab":{}},"source":["X = tf.placeholder(tf.float32, shape = [None, 15])\n","Y = tf.placeholder(tf.float32, shape = [None, 1])"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"fMKN4uXSqkXh","colab_type":"code","colab":{}},"source":["W = tf.Variable(tf.random_normal([15, 1]), name = 'weight')\n","b = tf.Variable(tf.random_normal([1]), name = 'bias')"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"7rFJqw0nqmCY","colab_type":"code","colab":{}},"source":["hypothesis = tf.sigmoid(tf.matmul(X, W) + b)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"cdJNXoXiqnJx","colab_type":"code","colab":{}},"source":["cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"9bTAU1_NqpNC","colab_type":"code","colab":{}},"source":["train = tf.train.GradientDescentOptimizer(learning_rate = 0.1).minimize(cost)"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"sGGNwnOTqqOf","colab_type":"code","colab":{}},"source":["predicted = tf.cast(hypothesis > 0.5, dtype = tf.float32)\n","accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype = tf.float32))"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"E_2OluRAqrLc","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":935},"outputId":"8ae6c580-8e5b-49c6-8a33-899e005cfde9","executionInfo":{"status":"ok","timestamp":1584871026466,"user_tz":-540,"elapsed":9386,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["sess = tf.Session()\n","sess.run(tf.global_variables_initializer())\n","\n","for step in range(10001):\n"," cost_val, _ = sess.run([cost, train], feed_dict = {X: x_data, Y: y_data})\n"," if step % 200 == 0:\n"," print(step, cost_val)"],"execution_count":39,"outputs":[{"output_type":"stream","text":["0 1.2466245\n","200 0.4480425\n","400 0.41504902\n","600 0.40316257\n","800 0.39811605\n","1000 0.3954196\n","1200 0.393662\n","1400 0.3923488\n","1600 0.39128524\n","1800 0.39038438\n","2000 0.38960224\n","2200 0.38891292\n","2400 0.38829935\n","2600 0.38774902\n","2800 0.38725245\n","3000 0.386802\n","3200 0.3863908\n","3400 0.38601407\n","3600 0.38566738\n","3800 0.38534704\n","4000 0.3850497\n","4200 0.38477305\n","4400 0.38451484\n","4600 0.38427305\n","4800 0.38404617\n","5000 0.38383275\n","5200 0.38363165\n","5400 0.3834416\n","5600 0.38326207\n","5800 0.38309202\n","6000 0.38293055\n","6200 0.38277727\n","6400 0.3826317\n","6600 0.3824931\n","6800 0.38236102\n","7000 0.38223526\n","7200 0.38211522\n","7400 0.38200057\n","7600 0.3818911\n","7800 0.38178653\n","8000 0.38168642\n","8200 0.3815906\n","8400 0.38149893\n","8600 0.38141114\n","8800 0.38132688\n","9000 0.3812462\n","9200 0.38116875\n","9400 0.38109443\n","9600 0.38102305\n","9800 0.3809546\n","10000 0.38088867\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"AuRSOG09qsJ0","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":35},"outputId":"be592ab1-6df4-4786-ca53-ecd01ce98004","executionInfo":{"status":"ok","timestamp":1584871028613,"user_tz":-540,"elapsed":874,"user":{"displayName":"Yoon Jack","photoUrl":"","userId":"04923927567667044980"}}},"source":["a = sess.run(accuracy, feed_dict = {X: x_data, Y: y_data})\n","print(\"Accuracy : \", a)"],"execution_count":40,"outputs":[{"output_type":"stream","text":["Accuracy : 0.8518169\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"a_burp5_rAZ0","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]}