diff --git a/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb b/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb index fc65ad7a380..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", @@ -458,6 +458,64 @@ "language": "python", "metadata": {}, "outputs": [] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Using a kernel multiclass machine" + ] + }, + { + "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_multiclass_kernel(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", + " classifier.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_multiclass_kernel(MulticlassOneVsRestStrategy())\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {}