如何为k Nearest Neighbors分类提高Python实现的效率?

时间:2017-03-13 12:52:48

标签: python algorithm performance nearest-neighbor supervised-learning

我尝试在Python中为图像实现k最近邻分类算法,这是我的代码
 


    def classifyImage(self, RGBAValsForOneImage, kVal):
            redMaster = RGBAValsForOneImage[0]
            greenMaster = RGBAValsForOneImage[1]
            blueMaster = RGBAValsForOneImage[2]
            alphaMaster = RGBAValsForOneImage[3]
            L2DistanceDictionary = {}
            kLabels = []

for i in range(self.nImagesTrain): print(str("comparing to image nr " + str(i))) L2Norm = 0 # label = self.LabelsTraining[i] for j in range(self.nPixel): redGreenBlueAlpha = self.RGBAPixelValuesTraining[i, j, :] redCompare = redGreenBlueAlpha[0] greenCompare = redGreenBlueAlpha[1] blueCompare = redGreenBlueAlpha[2] alphaCompare = redGreenBlueAlpha[3] L2Norm += np.sqrt((redCompare - redMaster) ** 2 + (greenCompare - greenMaster) ** 2 + (blueCompare - blueMaster) ** 2 + (alphaCompare - alphaMaster) ** 2)[0] L2Norm *= 100 L2NormInt = int(L2Norm) alreadyThere = L2DistanceDictionary.get(L2NormInt, []) alreadyThere.append(i) L2DistanceDictionary[L2NormInt] = alreadyThere theSortedKeys = sorted(L2DistanceDictionary.keys()) howManyUntilNow = 0 for i in range(0, len(L2DistanceDictionary)): thekey = theSortedKeys[i] thevalue = L2DistanceDictionary[thekey] howMany = len(thevalue) for z in range(0, howMany): if howManyUntilNow < kVal: kLabels.append(self.LabelsTraining[thevalue[z]]) else: break labels_to_count = (label for label in kLabels) c = Counter(labels_to_count) winLabel, count = c.most_common(1)[0] return winLabel

classifyImage函数的基本思想是将需要分类的图像像素的RGBA值与训练数据集中其他图像的RGBA值进行比较,并返回标记中的最常见标记。 k最近邻居。

代码的问题在于速度非常慢。有没有办法提高效率?我几乎从不用Python编写代码,所以可能有很简单的方法来改进这段代码。

0 个答案:

没有答案