{ "cells": [ { "cell_type": "markdown", "id": "c1491f9d", "metadata": {}, "source": [ "**Example 4.40.** Classify emails represented by the binary term-to-document vectors as \"spam\" and \"not spam\". Consider the following dataset.\n", "\n", "| | \"and\" | \"offer\" | \"the\" | \"of\" | \"sale\" | $y_i$ |\n", "| - | - | - | - | - | - | - |\n", "| | | | | | | |\n", "| $\\mathbf{\\hat x}_1$ | 1 | 1 | 0 | 1 | 1 | $+1$ (spam) |\n", "| $\\mathbf{\\hat x}_2$ | 0 | 0 | 1 | 1 | 0 | $-1$ (not spam) |\n", "| $\\mathbf{\\hat x}_3$ | 0 | 1 | 1 | 0 | 0 | $+1$ (spam) |\n", "| $\\mathbf{\\hat x}_4$ | 1 | 0 | 0 | 1 | 0 | $-1$ (not spam) |\n", "| | | | | | | | \n", "| $\\mathbf{\\hat x}_5$ | 1 | 0 | 1 | 0 | 1 | $+1$ (spam) |\n", "| $\\mathbf{\\hat x}_6$ | 1 | 0 | 1 | 1 | 0 | $-1$ (not spam) |\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6e1dc389-7410-4593-8373-1a640224d1a9", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "Xh = np.array([[ 1 , 1 , 0 , 1 , 1], # x_1 spam\n", " [ 0 , 0 , 1 , 1 , 0], # x_2 not spam\n", " [ 0 , 1 , 1 , 0 , 0], # x_3 spam\n", " [ 1 , 0 , 0 , 1 , 0], # x_4 not spam\n", " [ 1 , 0 , 1 , 0 , 1], # x_5 spam\n", " [ 1 , 0 , 1 , 1 , 0] # x_6 not spam\n", " ])\n", "y = np.array([1,-1,1,-1,1,-1])\n", "X = np.hstack((np.ones((Xh.shape[0],1)), Xh)) # Affine -> Homogeneous form" ] }, { "cell_type": "code", "execution_count": 2, "id": "0d68c106-6acc-43e7-aaa2-b8013adba42f", "metadata": {}, "outputs": [], "source": [ "# Perceptron algorithm selecting ik without replacement\n", "def Perceptron(X,y, K=100):\n", " theta = np.zeros(X.shape[1])\n", " k = 0\n", " while (k 0):\n", " break\n", " return theta" ] }, { "cell_type": "code", "execution_count": 3, "id": "6a44173a", "metadata": {}, "outputs": [], "source": [ "# Perceptron algorithm selecting ik with replacement\n", "def PerceptronR(X,y, K=100):\n", " theta = np.zeros(X.shape[1])\n", " k = 0\n", " while (k 0):\n", " break\n", " return theta" ] }, { "cell_type": "code", "execution_count": 4, "id": "864f91f5-0286-4697-9832-f7cb204c1bfc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "iteration 1\n", "iteration 2\n", "iteration 3\n", "iteration 4\n", "theta: [ 0. 0. 2. 0. -1. 1.]\n", "predicted labels: [ 1. -1. 1. -1. 1. -1.]\n" ] } ], "source": [ "theta = Perceptron(X[:4], y[:4])\n", "print(\"theta: \" + str(theta))\n", "print(\"predicted labels: \" + str(np.sign(X @ theta)))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 5 }