1 of 6

Cookbook for Practice 3.1

Cong Li 李聪

实践3.1攻略

2 of 6

Nearest Neighbor 最近邻 (1)

Start a new file ‘nearestNeighbor.py’ w/ a new class. Follow the training part in rote learning.

创建一个包含新的类型的新文件“nearestNeighbor.py”。训练部分参照死记硬背的学习。

3 of 6

Nearest Neighbor 最近邻 (2)

Now we write the ‘classify’ function, starting w/ a local variable for minimal distance & another for the nearest data sample seen so far

现在我们写“classify”函数,为到目前为止遇到的最小距离和最邻近的数据各准备一个局部变量

4 of 6

Nearest Neighbor 最近邻 (3)

Loop over all the training data samples, calculate the distance, & update the minimum distance & the nearest neighbor on demand. Return the class label of the nearest neighbor at the end.

遍历所有的训练数据,计算距离,并根据情况更新最小距离和最近邻。最后返回最近邻的类别。

Distance calculation 距离计算

5 of 6

Run It 运行之

In ‘tester.py’, print something for every 100 data samples classified 在“tester.py”中,每分类100个数据就打印一下

In ‘test.py’, import ‘nearestNeighbor’, follow rote learning to create an instance for performance evaluation

在“test.py”中,导入“nearestNeighbor”,参照死记硬背的学习,创建其中类型的实例,对其分类性能进行评估

Train on ‘usps’, test on ‘usps.t’ (& be very patient)

用“usps”进行训练,用“usps.t”进行评估(并表现出异常的耐心)

6 of 6

The End