{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Asssignment 2 - linear perceptron\n", "\n", "A very simple classifier that also illustrates main aspects of machine learning." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Decision boundary for linear perceptron\n", "\n", "Below are the code and data for linear classification." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "\n", "class Perceptron(object):\n", " \"\"\"Perceptron classifier.\n", "\n", " Parameters\n", " ------------\n", " eta : float\n", " Learning rate (between 0.0 and 1.0)\n", " n_iter : int\n", " Passes over the training dataset.\n", "\n", " Attributes\n", " -----------\n", " w_ : 1d-array\n", " Weights after fitting.\n", " errors_ : list\n", " Number of misclassifications in every epoch.\n", "\n", " \"\"\"\n", " def __init__(self, eta=0.01, n_iter=10):\n", " self.eta = eta\n", " self.n_iter = n_iter\n", "\n", " def fit(self, X, y):\n", " \"\"\"Fit training data.\n", "\n", " Parameters\n", " ----------\n", " X : {array-like}, shape = [n_samples, n_features]\n", " Training vectors, where n_samples is the number of samples and\n", " n_features is the number of features.\n", " y : array-like, shape = [n_samples]\n", " Target values.\n", "\n", " Returns\n", " -------\n", " self : object\n", "\n", " \"\"\"\n", " self.w_ = np.zeros(1 + X.shape[1])\n", " self.errors_ = []\n", "\n", " for _ in range(self.n_iter):\n", " errors = 0\n", " for xi, target in zip(X, y):\n", " update = self.eta * (target - self.predict(xi))\n", " self.w_[1:] += update * xi\n", " self.w_[0] += update\n", " errors += int(update != 0.0)\n", " self.errors_.append(errors)\n", " return self\n", "\n", " def net_input(self, X):\n", " \"\"\"Calculate net input\"\"\"\n", " return np.dot(X, self.w_[1:]) + self.w_[0]\n", "\n", " def predict(self, X):\n", " \"\"\"Return class label after unit step\"\"\"\n", " return np.where(self.net_input(X) >= 0.0, 1, -1)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | 0 | \n", "1 | \n", "2 | \n", "3 | \n", "4 | \n", "
---|---|---|---|---|---|
145 | \n", "6.7 | \n", "3.0 | \n", "5.2 | \n", "2.3 | \n", "Iris-virginica | \n", "
146 | \n", "6.3 | \n", "2.5 | \n", "5.0 | \n", "1.9 | \n", "Iris-virginica | \n", "
147 | \n", "6.5 | \n", "3.0 | \n", "5.2 | \n", "2.0 | \n", "Iris-virginica | \n", "
148 | \n", "6.2 | \n", "3.4 | \n", "5.4 | \n", "2.3 | \n", "Iris-virginica | \n", "
149 | \n", "5.9 | \n", "3.0 | \n", "5.1 | \n", "1.8 | \n", "Iris-virginica | \n", "