Skip to content

Commit

Permalink
OvO plots
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabh7 committed Mar 18, 2014
1 parent a4b7540 commit 9ad4aea
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb
Expand Up @@ -523,7 +523,7 @@
"source": [
"So we have seen that we can classify multiclass samples using a base binary machine. If we dwell on this a bit more, we can easily spot the intuition behind this.\n",
"\n",
"The MulticlassOneVsRestStrategy classifies one class against the rest of the classes. This is done for each and every class by training a separate classifier for it.So we will have total $k$ classifiers where $k$ is the number of classes.\n",
"The `MulticlassOneVsRestStrategy` classifies one class against the rest of the classes. This is done for each and every class by training a separate classifier for it.So we will have total $k$ classifiers where $k$ is the number of classes.\n",
"\n",
"Just to see this in action lets create some data using the gaussian mixture model class ([GMM](http://www.shogun-toolbox.org/doc/en/3.0.0/classshogun_1_1CGMM.html)) from which we sample the data points.Four different classes are created and plotted."
]
Expand Down Expand Up @@ -584,6 +584,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"_=jet()\n",
"_=scatter(traindata[0,:], traindata[1,:], c=trainlab, s=100)"
],
"language": "python",
Expand Down Expand Up @@ -671,6 +672,70 @@
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `MulticlassOneVsOneStrategy` is a bit different with more number of machines.\n",
"Since it trains a classifer for each pair of classes, we will have a total of $\\frac{k*(k-1)}{2}$ submachines for $k$ classes.Binary classification then takes place on each pair.\n",
"Let's visualize this in a plot."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"C=2.0\n",
" \n",
"bin_machine = LibLinear()\n",
"bin_machine.set_bias_enabled(True)\n",
"bin_machine.set_C(C, C)\n",
"\n",
"mc_machine1 = LinearMulticlassMachine(MulticlassOneVsOneStrategy(), feats_tr, bin_machine, labels)\n",
"mc_machine1.train()\n",
"\n",
"out1=mc_machine1.apply_multiclass(grid) #main output\n",
"z1=out1.get_labels().reshape((size, size))\n",
"\n",
"sub_out10=mc_machine1.get_submachine_outputs(0) #first submachine\n",
"sub_out11=mc_machine1.get_submachine_outputs(1) #second submachine\n",
"\n",
"z10=sub_out10.get_labels().reshape((size, size))\n",
"z11=sub_out11.get_labels().reshape((size, size))\n",
"\n",
"no_color=array([5.0 for i in xrange(num)])\n",
"\n",
"figure(figsize=(20,5))\n",
"subplot(131, title=\"Submachine 1\") #plot submachine and traindata\n",
"c10=pcolor(x, y, z10)\n",
"_=contour(x, y, z10, linewidths=1, colors='black', hold=True)\n",
"lab1=concatenate((l0,l1,no_color,no_color))\n",
"_=scatter(traindata[0,:], traindata[1,:], c=lab1, cmap='gray', s=100)\n",
"_=colorbar(c10)\n",
"\n",
"subplot(132, title=\"Submachine 2\")\n",
"c11=pcolor(x, y, z11)\n",
"_=contour(x, y, z11, linewidths=1, colors='black', hold=True)\n",
"lab2=concatenate((l0, no_color, l2, no_color))\n",
"_=scatter(traindata[0,:], traindata[1,:], c=lab2, cmap=\"gray\", s=100)\n",
"_=colorbar(c11)\n",
"\n",
"subplot(133, title=\"Multiclass output\")\n",
"c12=pcolor(x, y, z1)\n",
"_=contour(x, y, z1, linewidths=1, colors='black', hold=True)\n",
"_=colorbar(c12) \n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first two plots help us visualize how the submachines do binary classification for each pair. The class with maximum votes is chosen for test samples, leading to a refined multiclass output as in the last plot."
]
}
],
"metadata": {}
Expand Down

0 comments on commit 9ad4aea

Please sign in to comment.