Newer
Older
Mikolaj Rybinski
committed
"execution_count": null,
"source": [
"df = pd.read_csv(\"data/spiral.csv\")\n",
"\n",
"features = df.iloc[:, :-1]\n",
"labels = df.iloc[:, -1]\n",
"\n",
"x = features.iloc[:, 0]\n",
"y = features.iloc[:, 1]\n",
"color = [[\"steelblue\", \"chocolate\"][l] for l in labels]\n",
"plt.figure(figsize=(6, 6))\n",
"plt.scatter(x, y, color=color)\n",
"\n",
"\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.preprocessing import PolynomialFeatures\n",
"\n",
"clf = LogisticRegression(C=1)\n",
"preproc = PolynomialFeatures(4, include_bias=False)\n",
"\n",
"plt.figure(figsize=(6, 6))\n",
"train_and_plot_decision_surface(\"Logistic regression\", clf, features, labels, preproc=preproc)\n",
"\n",
"\n",
"from sklearn.svm import SVC\n",
"\n",
"clf = SVC(C=500)\n",
"plt.figure(figsize=(6, 6))\n",
"train_and_plot_decision_surface(\"SVC\", clf, features, labels, preproc=None)"
Mikolaj Rybinski
committed
],
"outputs": [],
"metadata": {
"tags": [
"solution"
]
}
},
{
"cell_type": "markdown",
"source": [
"### Comparison of decision surfaces for different classifiers and datasets\n",
"Compare decision surfaces for different classifiers listed below for both `\"data/xor.csv\"` and `\"data/circle.csv\"` (circle) datasets. For which classifiers does it help to add polynomial features? How many degrees suffice?"
Mikolaj Rybinski
committed
],
"metadata": {}
},
{
"cell_type": "code",
Mikolaj Rybinski
committed
"execution_count": null,
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.svm import LinearSVC, SVC\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"\n",
Mikolaj Rybinski
committed
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
Mikolaj Rybinski
committed
"execution_count": null,
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.svm import LinearSVC, SVC\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"\n",
" df = pd.read_csv(dataset)\n",
" features = df.iloc[:, :-1]\n",
" labels = df.iloc[:, -1]\n",
" \n",
" clf = LogisticRegression()\n",
" \n",
" plt.subplot(5, 2, 1)\n",
" train_and_plot_decision_surface(\"LogisticRegression\", clf, features, labels, preproc=None, N=300)\n",
" train_and_plot_decision_surface(\"LogisticRegression\", clf, features, labels, preproc=preproc, N=300)\n",
" train_and_plot_decision_surface(\"LinearSVC\", clf, features, labels, preproc=None, N=300)\n",
" train_and_plot_decision_surface(\"LinearSVC\", clf, features, labels, preproc=preproc, N=300)\n",
" train_and_plot_decision_surface(\"SVC\", clf, features, labels, preproc=None, N=300)\n",
" train_and_plot_decision_surface(\"SVC\", clf, features, labels, preproc=preproc, N=300)\n",
" \n",
" clf = DecisionTreeClassifier()\n",
" plt.subplot(5, 2, 7)\n",
" train_and_plot_decision_surface(\"DecisionTreeClassifier\", clf, features, labels, preproc=None, N=300)\n",
" train_and_plot_decision_surface(\"DecisionTreeClassifier\", clf, features, labels, preproc=preproc, N=300)\n",
"\n",
" clf = KNeighborsClassifier()\n",
" plt.subplot(5, 2, 9)\n",
" train_and_plot_decision_surface(\"KNeighborsClassifier\", clf, features, labels, preproc=None, N=300)\n",
" train_and_plot_decision_surface(\"KNeighborsClassifier\", clf, features, labels, preproc=preproc, N=300)\n",
"try_dataset(\"data/xor.csv\", PolynomialFeatures(2, include_bias=False))\n"
Mikolaj Rybinski
committed
],
"outputs": [],
"metadata": {
"scrolled": true,
"tags": [
"solution"
]
}
{
"cell_type": "code",
Mikolaj Rybinski
committed
"execution_count": null,
"source": [
"try_dataset(\"data/circle.csv\", PolynomialFeatures(4, include_bias=False))"
],
"outputs": [],
"metadata": {
"tags": [
"solution"
]
Mikolaj Rybinski
committed
}
{
"cell_type": "markdown",
"source": [
"## But.. what if there are more than two classes?\n",
"\n",
"\n",
"Previous and the following examples in this script consider two class problems.\n",
"Before we dig deeper into classification, let's say a few words on how to handle more than two classes.\n",
"\n",
"\n",
"<div class=\"alert alert-block alert-warning\"><p><i class=\"fa fa-warning\"></i> \n",
" The general idea for <code>n > 2</code> classes is to build multiple 2-class classifiers and determine a winning class by applying all of them:\n",
"<ul>\n",
" <li>in the <strong>one-vs-rest</strong> (OvR; or <strong>one-vs-all</strong>, OvA) approach build <code>n</code> classifiers for \"label n vs. the rest\";</li>\n",
" <li>in the <strong>one-vs-one</strong> (OvO) approach builds classifiers for `label i vs label j` (in total <code>n x (n - 1) / 2</code> classifiers).</li>\n",
"</ul>\n",
"</p></div>\n",
"\n",
"For new incoming data then all classifiers (`n` or `n x (n -1) / 2`) are applied and the overall winning class gives the final result.\n",
"\n",
"For instance, to classify images of digits:\n",
"\n",
"- we could build 10 classifiers `is it 0 or other digit`, `is it 1 or other digit`, etc.\n",
" \n",
" A new image then would hopefully yield `True` for exactly one of the classifier, in other situations the result is unclear.\n",
" \n",
" \n",
"- we could build 45 classifiers `is it 0 or 1`, `is it 0 or 2`, etc.\n",
"\n",
" For a new image we could choose the final outcome based on which class \"wins\" most often.\n",
"\n",
"\n",
"<div class=\"alert alert-block alert-info\"><p><i class=\"fa fa-info-circle\"></i> \n",
" In <code>scikit-learn</code> many classifiers support multi-class problems out of the box and also offer functionalities to implement <strong>one-vs-rest</strong> or <strong>one-vs-one</strong> in some cases (cf. <a href=\"https://scikit-learn.org/stable/modules/multiclass.html\"><code>scikit-learn</code> multiclass and multilabel algorithms</a>).\n",
Mikolaj Rybinski
committed
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"Copyright (C) 2019-2021 ETH Zurich, SIS ID"
Mikolaj Rybinski
committed
],
"metadata": {}
}
],
"metadata": {
"language_info": {
Mikolaj Rybinski
committed
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2