1 of 5

Cookbook for Practice 3.2

Cong Li 李聪

实践3.2攻略

2 of 5

Install NumPy 安装NumPy

Go to the directory w/ the file ‘numpy-….whl’, & run the command line below

进入文件“numpy-….whl”所在目录,运行以下命令行

pip install numpy-….whl

3 of 5

NumPy Array NumPy数组

In ‘uspsData.py’, import ‘numpy’

在文件“uspsData.py”中,导入“numpy”

During data reading, now for each data item, we use the ‘numpy.array’ class (rather than the normal array) to store the attributes 在读文件时,我们用“numpy.array”类型(而非常规数组)来存储每个数据的属性

4 of 5

NumPy Calculation NumPy计算

In ‘nearestNeighbor.py’, import ‘numpy’

在文件“nearestNeighbor.py”中,导入“numpy”

Now use the ‘-’ operator to get the difference of the two ‘numpy.array’ instances, the result is also an instance of ‘numpy.array’. Then use the ‘numpy.linalg.norm’ function to calculate its L1 norm. The result is actually the distance we want. 现在用减号运算来获取两个“numpy.array”类型的实例之差,运算结果也是“numpy.array”类型的实例。然后再用“numpy.linalg.norm”函数来计算这个实例的L1范数,其结果正是我们要的距离

Run it 运行之

Element-wise operation

为每个元素计算

5 of 5

The End