1 of 25

Cookbook for Practice 9.1

Cong Li 李聪

实践9.1攻略

2 of 25

Different Context Sizes�不同的上下文大小 (1)

Based on Practice 8.2, rename the file ‘interestData.py’ to ‘interestDataEnsemble.py’, change the class name accordingly

基于实践8.2,将文件“interestData.py”重命名为“interestDataEnsemble.py”,对类型名也作相应改动

3 of 25

Different Context Sizes�不同的上下文大小 (2)

In the member function ‘get_items_and_label’, rewrite the for loop to find & return the position of ‘interest’ as well

在成员函数“get_items_and_label”中,重写for循环,找到“interest”的位置并追加返回该位置

Variable to locate the position of ‘interest’

用于找到“interest”位置的变量

4 of 25

Different Context Sizes�不同的上下文大小 (2)

In the member function ‘get_items_and_label’, rewrite the for loop to find & return the position of ‘interest’ as well

在成员函数“get_items_and_label”中,重写for循环,找到“interest”的位置并追加返回该位置

Change the for loop type to iterate all the positions, not all the word items

改变for循环的类型,遍历所有的位置,而非所有的词项

5 of 25

Different Context Sizes�不同的上下文大小 (2)

In the member function ‘get_items_and_label’, rewrite the for loop to find & return the position of ‘interest’ as well

在成员函数“get_items_and_label”中,重写for循环,找到“interest”的位置并追加返回该位置

Record the position of ‘interest’

记录“interest”的位置

6 of 25

Different Context Sizes�不同的上下文大小 (2)

In the member function ‘get_items_and_label’, rewrite the for loop to find & return the position of ‘interest’ as well

在成员函数“get_items_and_label”中,重写for循环,找到“interest”的位置并追加返回该位置

Return the position

返回该位置

7 of 25

Different Context Sizes�不同的上下文大小 (2)

In the member function ‘get_items_and_label’, rewrite the for loop to find & return the position of ‘interest’ as well

在成员函数“get_items_and_label”中,重写for循环,找到“interest”的位置并追加返回该位置

Modify member function ‘build_dict’ to accept the new return value 修改“build_dict”成员函数以接受新返回的值

Accept new return value

接受新返回值

8 of 25

Different Context Sizes�不同的上下文大小 (3)

Rewrite the member function ‘convert_features_and_label’. Add a new input parameter for context size. Calculate the left & the right side of the context given the position of ‘interest’.

重写成员函数“convert_features_and_label”。为上下文大小加一个新的输入参数。根据“interest”的位置计算上下文的左右边界

9 of 25

Different Context Sizes�不同的上下文大小 (4)

In converting word items to the attributes (features), rewrite the for loop to use based on the left and right side of the context

当把词项转换成属性时,基于上下文左右边界重写for循环

10 of 25

Different Context Sizes�不同的上下文大小 (5)

Now modify the initialization function. Recall that the original initialization function read the input file & convert each valid line into the attributes & a class label. Here we want to convert each valid line to a set of contexts w/ different sizes & a class label

现在修改初始化函数。回忆一下,原先的初始化函数读取输入文件,把每一有效行转换成属性和类别标记的。这里我们要把每一有效行转换成一组不同大小的上下文和类别标记

At the beginning, use an array for different context size: 1, 3, 5, 7, & full context 在起始处,用一个数组记录不同的上下文大小:1,3,5,7和所有上下文

11 of 25

Different Context Sizes�不同的上下文大小 (6)

Then rewrite the processing of each line. Here we use a 2D array for contexts w/ different sizes. Each context is an 1D array of attributes

然后我们重写对每一行的处理。这里我们用一个二维数组来表示不同大小的上下文。每一个上下文是一个一维数组

12 of 25

Modify Classifiers �修改分类器 (1)

In ‘perceptron.py’, in the class ‘Perceptron’ add a new member function ‘score_all’ to return the scores of all the class labels in a dictionary

在“perceptron.py”中的“Perceptron”类型中,加入一个新的成员函数“score_all”,返回所有类别标记的分数的字典

13 of 25

Modify Classifiers �修改分类器 (2)

Similarly in the class ‘LogisticRegression’ in ‘logisticRegression.py’, add a new member function ‘score_all’ to return the scores

类似地,在“logisticRegression.py”中的“LogisticRegression”类型中,加入一个新的成员函数“score_all”,返回所有的分数

14 of 25

Learning Algorithm Instances�学习算法实例 (1)

Create a new file ‘ensemble.py’ & import the required components

创建一个新文件“ensemble.py”,导入需要的组件

Create a class ‘ClassifierFactory’ & accept an algorithm name w/ a set of parameters as the input 创建一个新类型“Classifier-Factory”,在初始化函数中接受一个算法的名字和一组相应的参数

15 of 25

Learning Algorithm Instances�学习算法实例 (2)

Add a member function ‘get_classifier’ to return a new learning algorithm instance based on the classifier name & parameters

加一个成员函数“get_classifier”用作基于学习算法的名字和参数返回一个新的学习算法实例

16 of 25

Ensemble Learning 合奏学习 (1)

Add another class ‘Ensemble’ & in its initialization function, taking a parameter for the factory which will return a new learning algorithm instance on demand

创建另一个新的类型“Ensemble”,在初始化函数中,接受一个生成器参数,能够按需返回一个新的学习算法的实例

17 of 25

Ensemble Learning 合奏学习 (2)

In training, first get the training data to see how many contexts w/ different sizes there are to determine how many classifiers need to be trained. Then in a loop, get the learning algorithm instance from the factory

训练时,先从训练数据中察看有多少不同大小的上下文,以决定要训练多少个分类器。然后在循环中通过生成器获取学习算法的实例

18 of 25

Ensemble Learning 合奏学习 (3)

Now construct a new set of training data for the context with a certain size from the aggregated training data, train the classifier & put the classifier in the ensemble list

从聚合的训练数据中构造一个特定大小上下文的训练数据集,用来训练分类器,并把训练好的分类器放进合奏列表中

19 of 25

Ensemble Learning 合奏学习 (4)

In classification, for each class label, add the scores (weighted voting) from all the classifiers together using a dictionary

分类时,对每一个类别标记,把所有的分类器给出的分数加起来(带权投票),记在一个字典里

20 of 25

Ensemble Learning 合奏学习 (5)

Finally get the result by choosing the highest voted label

最后,获取最高票数的类别标记作为分类结果

21 of 25

Main Program 主程序 (1)

Create a new file ‘test.py’ & import the required components

创建一个新文件“test.py”,导入所需组件

Write the main program, starting from reading the data from the file & constructing the contexts w/ different sizes

写主程序,起始处从文件中读入数据,并构造出不同大小的上下文

22 of 25

Main Program 主程序 (2)

Randomly split the data into the training part & the evaluation part

把数据随机划分成训练部分和评估部分

23 of 25

Main Program 主程序 (3)

Create a factory generating perceptron instances, use it to create an ensemble classifier, & evaluate its performance

创建一个生成感知器实例的生成器,用它创建一个合奏分类器,并评价其准确率

Add a pause before going forward

继续运行前加入一个停顿

24 of 25

Main Program 主程序 (4)

Create another factory generating logistic regression instances, use it to create an ensemble classifier, & evaluate its performance

创建一个生成对数几率回归实例的生成器,用它创建一个合奏分类器,并评价其准确率

Run it 运行之

If you have enough time (e.g., 1 hour), hit enter to continue

如果你有足够的时间(例如1小时),按回车继续

25 of 25

The End