Skip to content

Instantly share code, notes, and snippets.

@PatWie
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatWie/035d4c45eb9eef06ebe5 to your computer and use it in GitHub Desktop.
Save PatWie/035d4c45eb9eef06ebe5 to your computer and use it in GitHub Desktop.
logistic Regression
{"nbformat_minor": 0, "cells": [{"execution_count": 25, "cell_type": "code", "source": "# read data from csv file\ndata = readdlm(\"breast-cancer.csv\", ',');\n# get shape of data \n(m,d) = size(data)\n", "outputs": [{"execution_count": 25, "output_type": "execute_result", "data": {"text/plain": "(263,10)"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 29, "cell_type": "code", "source": "# first half is training set\ny = data[1:int(m/2),1]; \ny[y .== -1] = 0 \nx = data[1:int(m/2),2:end];\nno = sum(x,1)\n# normalize\nx = x./no;\n# augment by one dimension\nx = [ones(int(m/2),1) x]\n\n\n(_,d) = size(x)\n\n# second half is test set\ny_test = data[int(m/2):end,1];\ny_test[y_test .== -1] = 0\nx_test = data[int(m/2):end,2:end];\n# normalize\nx_test = x_test./no;\n# augment by one dimension\nx_test = [ones(size(x_test,1),1) x_test];\n\nprint(size(x))\nprint(size(y))\nprint(size(x_test))\nprint(size(y_test))", "outputs": [{"output_type": "stream", "name": "stdout", "text": "(132,10)(132,)(132,10)(132,)"}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 32, "cell_type": "code", "source": "\u03b2 = zeros(d,1) # initial guess\n\u03b7 = 0.03 # learning rate\nmaxIter = 10000;\n\nfor k=1:maxIter\n \u03b2 = \u03b2 + \u03b7*sum((y-1.0./(1.0+exp(-x*\u03b2))) .* x,1)';\nend", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 33, "cell_type": "code", "source": "# in sample error\nprediction = 1.0./(1.0+exp(-x*\u03b2))\nprint(\"in sample accuracy \",100/132.0*sum(int(int(prediction .> 0.5).==y)),\"\\n\")\n\n# out of sample error\nprediction = 1.0./(1.0+exp(-x_test*\u03b2))\nprint(\"in sample accuracy \",100/132.0*sum(int(int(prediction .> 0.5).==y_test)),\"\\n\")", "outputs": [{"output_type": "stream", "name": "stdout", "text": "in sample accuracy 75.0\nout of sample accuracy 70.45454545454545\n"}], "metadata": {"collapsed": false, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Julia 0.3.6", "name": "julia 0.3", "language": "julia"}, "language_info": {"version": "0.3.6", "name": "julia"}, "language": "Julia"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment