KNNclassLearner.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:machine-learning-for-trading 作者: arjun-joshua 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号