1 of 10

Cookbook for Practice 8.1

Cong Li 李聪

实践8.1攻略

2 of 10

Binary Logistic Regression�两类对数几率回归 (1)

Start a new file ‘logisticRegression.py’, import ‘numpy’ & ‘math’, create a class of ‘BinaryLogisticRegression’, & in its initialization function take 4 input parameters for the regularization parameter, tolerance in gradient descent, maximum number of iterations, & learning rate.

开启一个新文件“logisticRegression.py”,导入“numpy”和“math”,创建“BinaryLogisticRegression”类型,并在其初始化函数中接受4个参数:调整参数、损失变化容忍度、最大训练轮数的参数和学习速度。

3 of 10

Binary Logistic Regression�两类对数几率回归 (2)

Use 4 member variables to store those parameters

用4个成员变量存储这些参数

Assuming two member variables of the class, bias b & weight array w, are properly maintained, write a scoring member function to calculate the score of an input data sample

假设我们恰当地维持着偏向b和权重w数组这两个成员变量,写一个计分的成员函数来计算某个给定输入数据的得分

4 of 10

Binary Logistic Regression�两类对数几率回归 (3)

Use a member function to translate the class label of negative samples from -1 to 0�用一个成员函数把反例的类别标记从-1转化成0

Write another member function for classification based on the score 再写一个成员函数,基于得分进行分类

5 of 10

Binary Logistic Regression�两类对数几率回归 (4)

Now we come to the training part. At the beginning, initialize the weights & bias. Use a temporarily variable to record the previous loss. In each iteration of training, initialize the gradients & the loss.

现在我们进入训练部分。一开始,初始化权重和偏向,用一个临时变量记录上一轮的损失。在每一轮训练开始之际初始化梯度和损失

6 of 10

Binary Logistic Regression�两类对数几率回归 (5)

 

 

7 of 10

Binary Logistic Regression�两类对数几率回归 (6)

Add the regularization term & print the loss

加入调整项,输出损失

Compare w/ the previous loss. Stop training if the change is within the tolerance. Otherwise update the previous loss

和上一轮的损失进行比较,如果变化小于容忍度,则终止训练。否则更新上一轮的损失

8 of 10

Binary Logistic Regression�两类对数几率回归 (7)

Add the regularization term part to the weight components in the gradient. Divide the gradients w/ the training data size. Update the weights & the bias

为权重部分的梯度加入调整项相关的部分。将梯度除以训练数据量。最后更新权重和偏向

9 of 10

Run It 运行之

Based on Practice 4.1, in file ‘test.py’, now replace the nearest neighbor related lines w/ the logistic regression related lines

基于实践4.1,把最近邻相关的语句换成对数几率回归相关的语句

Run w/ the command line below 用以下命令行运行

test.py 20

10 of 10

The End