\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{algorithm,algpseudocode}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{float}
\usepackage{subcaption}
\usepackage{enumitem}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0.0, 0.5, 0.0}
\definecolor{aaltoBlue}{RGB}{0, 51, 102}
\definecolor{pinegreen}{cmyk}{0.92,0,0.59,0.25}
\definecolor{royalblue}{cmyk}{1,0.50,0,0}
\definecolor{lavander}{cmyk}{0,0.48,0,0}
\definecolor{violet}{cmyk}{0.79,0.88,0,0}
\definecolor{lightblue}{RGB}{173, 216, 230}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,arrows.meta,positioning,fit,shapes.geometric,shapes.misc,intersections,patterns,decorations.pathreplacing,backgrounds,matrix}
\usepgfplotslibrary{fillbetween,groupplots}
\setcounter{secnumdepth}{-\maxdimen}
% Long unbreakable tokens (the ISO/IEC author names in citations, term names
% in the title) otherwise overflow the margin by a few points; a modest
% emergency stretch lets TeX loosen the paragraph instead.
\emergencystretch=2em
\usepackage{mleftright}
% Project macros used by this entry (auto-extracted).
\DeclareMathOperator*{\argmax}{arg\,max}
\newcommand{\clusteridx}{c}
\newcommand{\dataset}{\mathcal{D}}
\newcommand{\decreg}[1]{\mathcal{R}_{#1}}
\newcommand{\feature}{x}
\newcommand{\featuredim}{d}
\newcommand{\featuretrafo}{\phi}
\newcommand{\featurevec}{\vx}
\newcommand{\hypothesis}{h}
\newcommand{\labelspace}{\mathcal{Y}}
\newcommand{\normgeneric}[2]{\mleft\lVert #1 \mright\rVert_{#2}}
\newcommand{\nrcategories}{K}
\newcommand{\offset}{b}
\newcommand{\predictedlabel}{\widehat{\truelabel}}
\newcommand{\reals}{\mathbb{R}}
\newcommand{\regparam}{\alpha}
\newcommand{\sampleidx}{r}
\newcommand{\samplesize}{m}
\newcommand{\trainset}{\dataset^{(\rm train)}}
\newcommand{\truelabel}{y}
\newcommand{\vw}[0]{{\bf w}}
\newcommand{\vx}[0]{{\bf x}}
\newcommand{\weights}{\vw}

\begin{document}

\section{linear classifier}

\subsection*{Author and editors}
Alexander Jung\textsuperscript{*}, Department of Computer Science, Aalto University, Espoo, Finland (ORCID \mbox{\href{https://orcid.org/0000-0001-7538-0990}{0000-0001-7538-0990}}) \\
Editors: Konstantina Olioumtsevits (ORCID \mbox{\href{https://orcid.org/0000-0003-2697-8659}{0000-0003-2697-8659}}), Ekkehard Schnoor (ORCID \mbox{\href{https://orcid.org/0000-0001-8252-1499}{0000-0001-8252-1499}}), Aalto University, Espoo, Finland \\
\textsuperscript{*}Corresponding author: \href{mailto:alex.jung@aalto.fi}{alex.jung@aalto.fi}



\subsection{Abstract}
A linear classifier is a classifier whose
		decision regions are separated by hyperplanes. Each
		linear classifier is obtained by comparing the value of a linear
		hypothesis map against a threshold. For binary classification, the
		sign of the value can be used to determine the predicted
		label, and the absolute value divided by the Euclidean norm
		of the weight vector is the distance of the
		data point from the decision boundary. The
		model parameters of a linear classifier are the weight
		vector of the underlying hypothesis map and the
		offset. They are learned by empirical risk minimization (ERM) with different
		loss functions: logistic regression uses the logistic loss and the
		support vector machine (SVM) the hinge loss. For multi-class classification, a linear
		classifier is characterized by several linear model
		hypothesis maps, one per label value, and the resulting
		decision regions are intersections of halfspaces.

\subsection{Definition}
An aerial photograph of a wine-growing
	region is divided into small areas, and each area is to be classified as
	vineyard or not. Each area is a data point characterized by two
	features obtained from the photograph
	(Fig.~\ref{fig_linclass_vineyard_dict}). The label of each area indicates whether
		the area belongs to a vineyard ($\truelabel = 1$) or not
		($\truelabel = -1$). The labeled areas form the training set. A
		linear classifier separates the two classes by a straight line, its
		decision boundary, which is fixed by a normal vector, and
		it classifies a new area by its signed distance from that
		line.
		\begin{figure}[H]
		\begin{center}
		\begin{tikzpicture}
		\begin{axis}[
			width=0.86\linewidth, height=0.52\linewidth,
			xlabel={$\feature_{1}$},
			ylabel={$\feature_{2}$},
			xmin=0, xmax=48, ymin=-4.5, ymax=11.5,
			xtick=\empty, ytick=\empty,
			axis lines=left,
			legend style={draw=none, fill=none, font=\footnotesize,
				at={(0.99,0.99)}, anchor=north east},
			tick label style={font=\small}, label style={font=\small},
			]
			\addplot[only marks, mark=*, mark size=1.6pt, color=gray,
				opacity=0.65] table[col sep=comma, x=x1, y=x2]
				{linclass_vineyard.csv};
			\addlegendentry{vineyard, $\truelabel = +1$}
			\addplot[only marks, mark=triangle, mark size=2.6pt,
				color=black] table[col sep=comma, x=x1, y=x2]
				{linclass_other.csv};
			\addlegendentry{no vineyard, $\truelabel = -1$}
			\addplot[black, thick] table[col sep=comma, x=x1, y=x2]
				{linclass_boundary.csv};
			\addlegendentry{decision boundary $\hypothesis(\featurevec) = 0$}
			\addplot[only marks, mark=square, mark size=2.8pt, color=black]
				table[col sep=comma, x=x1, y=x2]{linclass_new.csv};
			\addlegendentry{new data point $\featurevec$}
			\addplot[black, thick, ->, forget plot] table[col sep=comma, x=x1, y=x2]
				{linclass_normal.csv};
			\node[font=\small, anchor=north] at (axis cs:42.0,-1.9) {$\weights$};
			\addplot[black, thick, <->, forget plot]
				table[col sep=comma, x=x1, y=x2]
				{linclass_drop.csv};
			\node[font=\small, anchor=north] at (axis cs:18.1,-2.3)
				{$|\hypothesis(\featurevec)| / \normgeneric{\weights}{2}$};
			\node[font=\small] at (axis cs:36,0.3)
				{$\hypothesis(\featurevec) > 0$};
			\node[font=\small] at (axis cs:12,6.5)
				{$\hypothesis(\featurevec) < 0$};
		\end{axis}
		\end{tikzpicture}
		\end{center}
		\caption{Scatterplot of data points, each
			characterized by two features and a binary label.
			The solid line is the decision boundary
			$\hypothesis(\featurevec) = \weights^{\top} \featurevec + \offset
			= 0$ of a linear classifier learned from these data points,
			and the single-headed arrow is its normal vector $\weights$.
			The double-headed arrow spans $|\hypothesis(\featurevec)| /
			\normgeneric{\weights}{2}$, the distance from the
			decision boundary of a new data point $\featurevec$
			(open square) that is not in the training set. Data generated by
			\texttt{pythondemos/linclass.py}
			\label{fig_linclass_vineyard_dict}}
		\end{figure}
		A linear classifier is a classifier whose
		decision regions are separated by hyperplanes in
		$\reals^{\featuredim}$ (Bishop, 2006, Sect.~4.1.1;
		Shalev-Shwartz and Ben-David, 2014, Sect.~9.1). For binary classification, with
		label space $\labelspace = \{-1, +1\}$, each linear classifier
		can be written as the thresholding rule $\predictedlabel =
		\operatorname{sign}(\hypothesis(\featurevec))$ with the
		hypothesis
		\[
			\hypothesis(\featurevec) = \weights^{\top} \featurevec
			+ \offset \text{.}
		\]
		The hypothesis map is parameterized by the weight vector
		$\weights \in \reals^{\featuredim} \setminus \{\mathbf{0}\}$ and the
		offset $\offset \in \reals$, which is also called the threshold
		or the bias. A data point with
		feature vector $\featurevec$ is classified as $\predictedlabel =
		1$ if $\hypothesis(\featurevec) \geq 0$ and as $\predictedlabel =
		-1$ otherwise. The resulting decision boundary is the
		hyperplane
		\[
			\{ \featurevec \in \reals^{\featuredim} : \weights^{\top}
			\featurevec + \offset = 0 \} \text{,}
		\]
		with normal vector $\weights$. This boundary separates two
		halfspaces, which are the decision regions
		$\decreg{1} = \{ \featurevec : \hypothesis(\featurevec) \geq 0 \}$
		and $\decreg{-1} = \{ \featurevec : \hypothesis(\featurevec) < 0 \}$.
		The feature vectors on the decision boundary, with
		$\hypothesis(\featurevec) = 0$, are assigned to $\decreg{1}$ by
		convention.
\par
		Consider a feature vector $\featurevec$ and any vector
		$\featurevec'$ on the decision boundary,
		$\hypothesis(\featurevec') = 0$. Subtracting
		$\weights^{\top} \featurevec' + \offset = 0$ from
		$\hypothesis(\featurevec)$ gives
		\[
			\hypothesis(\featurevec) = \weights^{\top}
			\big( \featurevec - \featurevec' \big) \text{.}
		\]
		The Cauchy-Schwarz inequality bounds the right-hand side in
		magnitude by $\normgeneric{\weights}{2} \normgeneric{\featurevec -
		\featurevec'}{2}$ (Axler, 2024, Thm.~6.14). Every vector of the
		decision boundary is therefore at distance at least
		$|\hypothesis(\featurevec)| / \normgeneric{\weights}{2}$ from
		$\featurevec$. That distance is attained by the
		orthogonal projection
		\[
			\featurevec' = \featurevec - \frac{\hypothesis(\featurevec)}
			{\normgeneric{\weights}{2}^{2}} \, \weights \text{.}
		\]
		The distance of $\featurevec$ from the decision boundary
		is thus $|\hypothesis(\featurevec)| / \normgeneric{\weights}{2}$
		(Bishop, 2006, Sect.~4.1.1;
		Hastie et al., 2009, Sect.~4.5, Eq.~(4.40)).
		Fig.~\ref{fig_linclass_vineyard_dict} illustrates the distance
		to the decision boundary for one data point.
\par
		The value $|\hypothesis(\featurevec)|$ is a measure of the noise
		tolerance at the feature vector $\featurevec$. Perturbing $\featurevec$ by a
		vector $\boldsymbol{\delta}$ changes the hypothesis
		value by $\weights^{\top} \boldsymbol{\delta}$, which the
		Cauchy-Schwarz inequality bounds in magnitude by
		$\normgeneric{\weights}{2} \normgeneric{\boldsymbol{\delta}}{2}$.
		The sign of $\hypothesis(\featurevec)$, and with it the
		prediction, is therefore unchanged by every perturbation with
		$\normgeneric{\boldsymbol{\delta}}{2} < |\hypothesis(\featurevec)|
		/ \normgeneric{\weights}{2}$.
\par
		Multiplying $\hypothesis(\featurevec)$ by the
		label $\truelabel$ gives the margin. A positive
		margin means that the data point is correctly classified,
		a negative margin that it is misclassified, and a zero
		margin that it lies on the decision boundary, where the
		rule above assigns $\predictedlabel = 1$.
\par
		The model parameters $(\weights, \offset)$ are learned from a
		training set $\trainset = \{ (\featurevec^{(\sampleidx)},
		\truelabel^{(\sampleidx)}) \}_{\sampleidx = 1}^{\samplesize}$ by
		empirical risk minimization (ERM). Using the $0/1$ loss results in an objective function
		that is piecewise constant, and minimizing it is
		computationally hard in general (Shalev-Shwartz and Ben-David, 2014, Sect.~9.1).
		Practical methods replace the $0/1$ loss by a convex
		loss of the margin: logistic regression uses the logistic loss
		and the support vector machine (SVM) uses the hinge loss. For a linearly
		separable training set, the SVM with a sufficiently small
		regularization parameter $\regparam$, the factor that
		scales its penalty term, learns the linear classifier whose
		smallest margin is largest
		(Shalev-Shwartz and Ben-David, 2014, Sect.~15.1, Eq.~(15.1)). Both learn the
		model parameters of a linear classifier by minimizing a
		convex objective function: gradient descent (GD) minimizes the smooth
		objective function of logistic regression, and subgradient descent the non-smooth
		objective function of the SVM
		(Murphy, 2012, Sect.~8.3.2;
		Shalev-Shwartz and Ben-David, 2014, Sect.~14.2.3, Example~14.2). The
		squared error loss is a function of the margin as well,
		since $(\truelabel - \hypothesis(\featurevec))^{2}
		= (1 - \truelabel \, \hypothesis(\featurevec))^{2}$ for
		$\truelabel \in \{-1, +1\}$, so linear regression,
		ridge regression, and least absolute shrinkage and selection operator (Lasso) applied to the
		labels $\pm 1$ also learn linear classifiers
		(Bishop, 2006, Sect.~4.1.3).
\par
		For multi-class problems, with label space $\labelspace = \{1,
		\ldots, \nrcategories\}$ and $\nrcategories > 2$, a linear
		classifier uses one
		linear model hypothesis
		$g_{\clusteridx}(\featurevec) = \big( \weights^{(\clusteridx)}
		\big)^{\top} \featurevec + \offset^{(\clusteridx)}$ per
		label value $\clusteridx \in \labelspace$ and predicts the
		value with the largest score,
		$\predictedlabel = \argmax_{\clusteridx \in \labelspace}
		g_{\clusteridx}(\featurevec)$. The decision region
		$\decreg{\clusteridx}$ is then an intersection of
		$\nrcategories - 1$ halfspaces and is therefore a
		convex set. The border between two decision regions
		$\decreg{\clusteridx}$ and $\decreg{\clusteridx'}$ lies in the
		hyperplane on which the two scores agree
		(Bishop, 2006, Sect.~4.1.2, Eq.~(4.10)). The resulting
		partition of the feature space is shown in
		Fig.~\ref{fig_linclass_multiclass_dict} for a linear classifier
		with $\nrcategories = 3$ classes.
		\begin{figure}[H]
			\centering
			\begin{tikzpicture}[>=Stealth, font=\small]
				\begin{scope}
				\clip (0,0) rectangle (6.0,4.0);
				\fill[gray!8] (2.6,1.9) -- (0,3.5) -- (0,0) -- (3.9,0)
					-- cycle;
				\fill[gray!20] (2.6,1.9) -- (3.9,0) -- (6.1,0)
					-- (6.1,2.9) -- cycle;
				\fill[gray!32] (2.6,1.9) -- (6.1,2.9) -- (6.1,4.1)
					-- (0,4.1) -- (0,3.5) -- cycle;
				\draw[thick] (2.6,1.9) -- (0,3.5);
				\draw[thick] (2.6,1.9) -- (3.9,0);
				\draw[thick] (2.6,1.9) -- (6.1,2.9);
				\end{scope}
				\draw[->, thin] (0,0) -- (6.6,0)
					node[right, font=\scriptsize] {$\feature_{1}$};
				\draw[->, thin] (0,0) -- (0,4.5)
					node[above, font=\scriptsize] {$\feature_{2}$};
				\node[font=\scriptsize] at (1.3,1.0) {$\decreg{1}$};
				\node[font=\scriptsize] at (4.8,1.1) {$\decreg{2}$};
				\node[font=\scriptsize] at (2.6,3.4) {$\decreg{3}$};
				\node[font=\scriptsize, anchor=west] at (3.35,1.0)
					{$g_{1} = g_{2}$};
				\node[font=\scriptsize, anchor=south west] at (0.6,2.9)
					{$g_{1} = g_{3}$};
				\node[font=\scriptsize, anchor=south] at (4.6,2.45)
					{$g_{2} = g_{3}$};
			\end{tikzpicture}
			\caption{A linear classifier for $\nrcategories = 3$
				label values partitions the feature space
				$\reals^{2}$ into three decision regions
				$\decreg{1}, \decreg{2}, \decreg{3}$. Each border lies in
				the hyperplane on which the two competing scores
				agree, and each decision region is an intersection of
				halfspaces and therefore convex
				\label{fig_linclass_multiclass_dict}}
		\end{figure}
		A linear classifier can be generalized to allow for non-linear
		decision boundaries by using a feature transformation.
		Applying a feature transformation $\featuretrafo:
		\reals^{\featuredim} \to \reals^{\featuredim'}$ first and a linear
		classifier to $\featuretrafo(\featurevec)$ afterwards gives a
		decision boundary that is a hyperplane in
		$\reals^{\featuredim'}$ but non-linear in the original
		feature space. A kernel method constructs such a
		feature transformation from a kernel, and a deep net
		uses its first layers as a trainable feature transformation
		(Bishop and Bishop, 2024, Sect.~6.3.3;
		Schölkopf and Smola, 2002, Sect.~2.2).

\subsection{Cross References}
classifier, binary classification, decision boundary, decision region, hyperplane, halfspace, linear model, margin, logistic regression, SVM, ERM, $0/1$ loss, ridge regression, Lasso, feature transformation

\subsection{References}
\begin{enumerate}
  \item Axler (2024). Linear Algebra Done Right. 4th ed., Springer, Cham, Switzerland.
  \item Bishop (2006). Pattern Recognition and Machine Learning. Springer Science+Business Media, New York, NY, USA.
  \item Bishop and Bishop (2024). Deep Learning: Foundations and Concepts. Springer Nature, Cham, Switzerland.
  \item Schölkopf and Smola (2002). Learning with Kernels: Support Vector Machines, Regularization, Optimization, and Beyond. MIT Press, Cambridge, MA, USA.
  \item Murphy (2012). Machine Learning: A Probabilistic Perspective. MIT Press, Cambridge, MA, USA.
  \item Shalev-Shwartz and Ben-David (2014). Understanding Machine Learning: From Theory to Algorithms. Cambridge Univ. Press, New York, NY, USA.
  \item Hastie et al. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction. 2nd ed., Springer Science+Business Media, New York, NY, USA.
\end{enumerate}

\end{document}
