《机器学习实战》学习笔记——k近邻算法

1.numpy中一些函数的用法学习

  • shape()用法:

shape : tuple of ints

The elements of the shape tuple give the lengths of the corresponding array dimensions.。

  shape返回一个元组,依次为各维度的长度。shape[0]:第一维长度,shape[1]:第二维长度。

  

  • tile()用法:

numpy.tile(Areps)

Construct an array by repeating A the number of times given by reps.

If reps has length d, the result will have dimension of max(d, A.ndim).  (A.ndim表示A矩阵的维度)

If A.ndim dA is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote A to d-dimensions manually before calling this function.

If A.ndim dreps is promoted to A.ndim by pre-pending 1’s to it. Thus for an A of shape (2, 3, 4, 5), a reps of (2, 2) is treated as (1, 1, 2, 2).

  tile()用法理解有一点难度。假设reps=(2,3,3), 2、3、3分别代表在第一、二、三个维度的重复次数,通俗来说就是第一、二、三个中括号内的元素。若A的维度小于reps,则增加维度为3;若A的维度大于3(假设为4),则默认reps=(1,2,3,3).

  

  • numpy.array()的切片

  对于一维数组,切片用法与python相同;对于多维数组(矩阵)来说,A[x1,x2:x3,x4]中x1表示选取第一维度中的下标x1(矩阵就是第x1行),x2表示从第二个维度中下标为x2开始选取,如下:

  

  注意:x2不允许单独出现,即不能出现A[,x2:],否则会报错。同理,x3表示行下标(第一维),x4表示列下标(第二维)。x3表示选取到哪一行为止(不包含x3),x4表示选取x4这一列。

     

  • argsort()用法

  numpy.argsort(aaxis=-1kind='quicksort'order=None)

  Returns the indices that would sort an array.

  Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.

   返回一个数组由小到大排序之后的下标。axis表示要比较的维度,默认为最后一个维度。

 

2.python中的一些函数学习

  reload()函数,需要从imp模块中引入:from imp import reload

  在python3.x后raw_input()重命名为input()

  os模块中listdir(),列出路径下所有文件名组成的一个列表。

3.代码分析(待补充)

 

posted @ 2017-11-12 14:51  逗比小白鼠  阅读(118)  评论(0编辑  收藏  举报