From de52f93c11c48f2d2e18ffba3e01668713381add Mon Sep 17 00:00:00 2001 From: Saurabh7 Date: Sat, 1 Mar 2014 14:29:25 +0530 Subject: [PATCH 1/2] extend multiclass nb --- .../multiclass/multiclass_reduction.ipynb | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb b/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb index fc65ad7a380..0c50083cc50 100644 --- a/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb +++ b/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb @@ -458,6 +458,56 @@ "language": "python", "metadata": {}, "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Expanding on the idea of creating a generic multiclass machine and then assigning a particular multiclass strategy and a base binary machine, one can also use the [KernelMulticlassMachine](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CKernelMulticlassMachine.html) with a kernel of choice.\n", + "\n", + "Here we will use a [GaussianKernel](http://www.shogun-toolbox.org/doc/en/3.0.0/classshogun_1_1CGaussianKernel.html) with LibSVM as the classifer.\n", + "All we have to do is define a new helper evaluate function with the features defined as in the above examples." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def evaluate(strategy):\n", + " from modshogun import KernelMulticlassMachine, LibSVM, GaussianKernel\n", + " width=2.1\n", + " epsilon=1e-5\n", + " \n", + " kernel=GaussianKernel(feats_train, feats_train, width)\n", + " \n", + " classifier = LibSVM()\n", + "\tclassifier.set_epsilon(epsilon)\n", + "\n", + " mc_machine = KernelMulticlassMachine(strategy, kernel, classifier, lab_train)\n", + "\n", + " t_begin = time.clock()\n", + " mc_machine.train()\n", + " t_train = time.clock() - t_begin\n", + "\n", + " t_begin = time.clock()\n", + " pred_test = mc_machine.apply_multiclass(feats_test)\n", + " t_test = time.clock() - t_begin\n", + "\n", + " evaluator = MulticlassAccuracy()\n", + " acc = evaluator.evaluate(pred_test, lab_test)\n", + "\n", + " print \"training time: %.4f\" % t_train\n", + " print \"testing time: %.4f\" % t_test\n", + " print \"accuracy: %.4f\" % acc\n", + "\n", + "print \"\\nOne-vs-Rest\"\n", + "print \"=\"*60\n", + "evaluate(MulticlassOneVsRestStrategy())\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} From 1817b7416c3dc44d3a94f5b4a5ed6fb91696f892 Mon Sep 17 00:00:00 2001 From: Saurabh7 Date: Thu, 6 Mar 2014 11:23:02 +0530 Subject: [PATCH 2/2] fixed errors --- .../multiclass/multiclass_reduction.ipynb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb b/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb index 0c50083cc50..66c5c9b6783 100644 --- a/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb +++ b/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb @@ -142,7 +142,7 @@ "Xall = mat['data']\n", "\n", "#normalize examples to have norm one\n", - "Xall = Xall / sqrt(sum(Xall**2,0))\n", + "Xall = Xall / np.sqrt(sum(Xall**2,0))\n", "Yall = mat['label'].squeeze()\n", "\n", "# map from 1..10 to 0..9, since shogun\n", @@ -459,6 +459,14 @@ "metadata": {}, "outputs": [] }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Using a kernel multiclass machine" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -473,7 +481,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "def evaluate(strategy):\n", + "def evaluate_multiclass_kernel(strategy):\n", " from modshogun import KernelMulticlassMachine, LibSVM, GaussianKernel\n", " width=2.1\n", " epsilon=1e-5\n", @@ -481,7 +489,7 @@ " kernel=GaussianKernel(feats_train, feats_train, width)\n", " \n", " classifier = LibSVM()\n", - "\tclassifier.set_epsilon(epsilon)\n", + " classifier.set_epsilon(epsilon)\n", "\n", " mc_machine = KernelMulticlassMachine(strategy, kernel, classifier, lab_train)\n", "\n", @@ -502,7 +510,7 @@ "\n", "print \"\\nOne-vs-Rest\"\n", "print \"=\"*60\n", - "evaluate(MulticlassOneVsRestStrategy())\n", + "evaluate_multiclass_kernel(MulticlassOneVsRestStrategy())\n", "\n" ], "language": "python",