Skip to content

Instantly share code, notes, and snippets.

@codekitchen
Created October 19, 2016 18:52
Show Gist options
  • Save codekitchen/ee4ab59e1dbd8024ea88275cb31e3363 to your computer and use it in GitHub Desktop.
Save codekitchen/ee4ab59e1dbd8024ea88275cb31e3363 to your computer and use it in GitHub Desktop.
trying out keras on the pima-indians-diabetes dataset
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/\n",
"\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"import numpy as np\n",
"import urllib.request\n",
"\n",
"seed = 7\n",
"np.random.seed(seed)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"768"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset = np.loadtxt(urllib.request.urlopen(\"http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data\"), delimiter=\",\")\n",
"X = dataset[:, 0:8]\n",
"Y = dataset[:, 8]\n",
"len(dataset)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6. , 148. , 72. , 35. , 0. , 33.6 ,\n",
" 0.627, 50. , 1. ])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset[0]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"68"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trainX = X[0:700]\n",
"trainY = Y[0:700]\n",
"testX = X[700:]\n",
"testY = Y[700:]\n",
"len(testX)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"____________________________________________________________________________________________________\n",
"Layer (type) Output Shape Param # Connected to \n",
"====================================================================================================\n",
"dense_4 (Dense) (None, 12) 108 dense_input_2[0][0] \n",
"____________________________________________________________________________________________________\n",
"dense_5 (Dense) (None, 8) 104 dense_4[0][0] \n",
"____________________________________________________________________________________________________\n",
"dense_6 (Dense) (None, 1) 9 dense_5[0][0] \n",
"====================================================================================================\n",
"Total params: 221\n",
"____________________________________________________________________________________________________\n"
]
}
],
"source": [
"model = Sequential()\n",
"model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))\n",
"model.add(Dense(8, init='uniform', activation='relu'))\n",
"model.add(Dense(1, init='uniform', activation='sigmoid'))\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7fc3a2262be0>"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.fit(trainX, trainY, nb_epoch=150, batch_size=10, verbose=0)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"32/68 [=============>................] - ETA: 0s"
]
},
{
"data": {
"text/plain": [
"'acc: 82.35%'"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scores = model.evaluate(testX, testY)\n",
"\"%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (1.0, 1.0),\n",
" (0.0, 1.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (1.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (1.0, 1.0),\n",
" (0.0, 1.0),\n",
" (0.0, 0.0),\n",
" (1.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 0.0),\n",
" (1.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 1.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (1.0, 1.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 0.0),\n",
" (1.0, 1.0),\n",
" (1.0, 1.0),\n",
" (0.0, 1.0),\n",
" (1.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (0.0, 0.0),\n",
" (1.0, 1.0),\n",
" (0.0, 0.0)]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predictions = model.predict(testX)\n",
"predictions = [round(x[0]) for x in predictions]\n",
"list(zip(testY, predictions))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.5.2+"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment