Skip to content

Commit

Permalink
Merge pull request #1914 from Saurabh7/multiclassnb
Browse files Browse the repository at this point in the history
extend multiclass nb
  • Loading branch information
karlnapf committed Mar 6, 2014
2 parents 3da5868 + 1817b74 commit 4245294
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {}
Expand Down

0 comments on commit 4245294

Please sign in to comment.