Last active
December 3, 2015 08:54
-
-
Save wadhwasahil/229cbe3b05dcff7cef31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Warning: Maximum number of iterations has been exceeded.\n", | |
"Final Cost= [ 0.24021978]\n", | |
"accuracy= 97.2972972973\n" | |
] | |
} | |
], | |
"source": [ | |
"import csv\n", | |
"import numpy as np\n", | |
"import scipy.optimize, scipy.special\n", | |
"from scipy.optimize import fmin_bfgs\n", | |
"from matplotlib import pyplot, cm\n", | |
"from mpl_toolkits.mplot3d import Axes3D\n", | |
"\n", | |
"with open('../USOpen-men-2013.csv','rb') as csvfile:\n", | |
" spamreader = csv.reader(csvfile)\n", | |
" arr=[]\n", | |
" cnt=0\n", | |
" for row in spamreader:\n", | |
" if cnt==0:\n", | |
" cnt=cnt+1 \n", | |
" else:\n", | |
" arr.append(row)\n", | |
" x=[]\n", | |
" y=[]\n", | |
"# x contains the features\n", | |
"# y contains the class\n", | |
" for row in arr:\n", | |
" y.append([np.float64(row[3])])\n", | |
" for row in arr:\n", | |
" cnt=0\n", | |
" brr=[]\n", | |
" for i in row:\n", | |
" if cnt<6:\n", | |
" cnt=cnt+1\n", | |
" else:\n", | |
" brr.append(i)\n", | |
" x.append(brr) \n", | |
"m=len(x) #no of training examples\n", | |
"n=len(x[0]) # no of features\n", | |
"col=np.ones((m,1))\n", | |
"X=np.hstack((col,x))\n", | |
"theta=np.zeros((n+1,1))\n", | |
"# print len(theta)\n", | |
"for i in range(m):\n", | |
" for j in range(n+1):\n", | |
" if X[i][j]=='' or X[i][j]=='NA':\n", | |
" X[i][j]='0.0'\n", | |
"X=np.array(X,dtype=np.float64) \n", | |
"y=np.array(y,dtype=np.float64)\n", | |
"theta=np.array(theta,dtype=np.float64)\n", | |
"ss=compute_cost(theta,X,y)\n", | |
"result=scipy.optimize.fmin( compute_cost, x0=theta, args=(X, y), maxiter=1500, full_output=True )\n", | |
"# print result[0] # display the parameters theta\n", | |
"for i in range(n+1):\n", | |
" theta[i]=[result[0][i]]\n", | |
"hyp=sigmoid(X.dot(theta))\n", | |
"print 'Final Cost=',compute_cost(theta,X,y) # final reduced cost \n", | |
"cnt=0\n", | |
"for i in range(n+1):\n", | |
" if hyp[i]>0.5:\n", | |
" if y[i]==1:\n", | |
" cnt=cnt+1\n", | |
" else:\n", | |
" if y[i]==0:\n", | |
" cnt=cnt+1\n", | |
"print 'accuracy=',100.0*(float(cnt)/float(n+1)) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def sigmoid(z):\n", | |
" return 1.0/(1+np.exp(-z))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def compute_cost(theta,X,y): #computes cost given predicted and actual values\n", | |
" cnt=0.0\n", | |
" grad=np.zeros((n+1,1))\n", | |
" hyp=sigmoid(X.dot(theta))\n", | |
" z= hyp - y\n", | |
" J=0\n", | |
" for i in range(n+1):\n", | |
" for j in range(m):\n", | |
" grad[i]=grad[i]+((hyp[i]-y[i]))*X[j][i]\n", | |
" grad[i]=grad[i]/m \n", | |
" for i in range(m):\n", | |
" J=J+(y[i]*np.log(hyp[i]))+((1-y[i])*np.log(1-hyp[i]))\n", | |
" J=-J/m\n", | |
" return J" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment