1 of 10

Cookbook for Practice 5.2

Cong Li 李聪

实践5.2攻略

2 of 10

Data for One vs. Rest�一对其余的数据 (1)

In the file ‘perceptron.py’, create a class, ‘BinaryData’, for data used in one vs. rest. In its initialization, pass in the original data & the current class label of interest, & store them in member variables

在文件“perceptron.py”中,创建“BinaryData”类型,并在其初始化函数中接受原来的数据和当前关注的类别标记,存储于成员变量中

3 of 10

Data for One vs. Rest�一对其余的数据 (2)

Now we write the 3 member functions to get data size, the attributes of a data sample, & the its class label. Reuse the original data’s member functions for the first two.

现在我们来写获取数据数量、某个数据的属性及其类别标记的3个成员函数。对前两个,直接调用原来的数据的对应成员函数

4 of 10

Data for One vs. Rest�一对其余的数据 (3)

For the member function to get the class label of the data sample, compare the label w/ the label of interest, & then return 1 or -1 for the one or the rest

对于获得某数据类别标记的成员函数,比较其标记和当前关注的标记,返回1或-1,表示其余

5 of 10

Multi-class Perceptron �多类感知器 (1)

In the same file, create a class of ‘Perceptron’ for multi-class perceptron. Similar to ‘BinaryPerceptron’, take a input parameter for maximum number of rounds in training

在同一个文件中,为多类感知器创建类型“Perceptron”。和 “BinaryPerceptron”类似,在其初始化函数中接受一个最大训练轮数的参数

6 of 10

Multi-class Perceptron �多类感知器 (2)

To train a multi-class perceptron, we need to figure out how many unique classes there are in the training data. We use a dictionary to associate each class label w/ a binary perceptron

为了训练多类感知器,我们需要知道训练数据中一共有多少不同的类别。我们用一个字典,为每种类别标记关联一个两类感知器

7 of 10

Multi-class Perceptron �多类感知器 (3)

For each class label, we create data for one-vs.-rest, & train the binary perceptron

对每种类别,我们创建一对其余的数据,并训练两类感知器

8 of 10

Multi-class Perceptron �多类感知器 (4)

During classification, we loop over all those binary perceptrons to generate scores, & find the one w/ the best score

在分类时,我们遍历所有的两类感知器来获取得分,最终选择得分最高的

9 of 10

Run It 运行之

Based on Practice 4.4, in file ‘test.py’, now import ‘perceptron’ & create the instance of the ‘Perceptron’ class for classification

基于实践4.4,在文件“test.py”中,导入“perceptron”,创建“Perceptron”类型的实例,进行分类

Run it 运行之

10 of 10

The End