def query(self, points):
"""
@summary: Estimate a set of test points given the model we built.
@param points: should be a numpy array with each row corresponding to a specific query.
@returns the estimated values according to the saved model.
"""
pred = np.zeros(points.shape[0]) #initialize prediction vector
for i in range(0, points.shape[0]): #iterate over each test example
sqDist = np.zeros(np.shape(self.dataY)) #initialize squared distances vector
for j in range(0,self.dataX.shape[1]):
sqDist[:,0] += (points[i,j] - self.dataX[:,j])**2
sqDist = np.concatenate((sqDist, self.dataY), axis=1)
sqDist = np.asarray(sorted(sqDist, key=lambda x:x[0]))
# classify: calculate mode & no. of counts of modal value
pred[i], binCounts = stats.mode(sqDist[0:self.k,1])
return pred
KNNclassLearner.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录