示例#1
0
 def learnNew(self, new_attribute_dict, label):
     #mean_new=example_new/(label*example_new*example_new)
     new_attribute_vector = misc.dictToNumpyArray(new_attribute_dict)
     new_partial_mean, new_covariance_dict = self.updateNewPart(
         new_attribute_vector, label, new_attribute_dict)
     new_mean_dict = misc.numpyArrayToDict(new_partial_mean,
                                           list(new_attribute_dict.keys()))
     return new_mean_dict, new_covariance_dict
示例#2
0
    def set_classifier(self):
        self.classifier = clone(self.classifier)

        self.classifier.alpha = 1
        self.classifier.eta0 = 1

        self.classifier.partial_fit(self.init_classifier, [self.y[0]],
                                    np.unique(self.y))
        self.weight_dict = misc.numpyArrayToDict(self.classifier.coef_[0],
                                                 list(self.X[0].keys()))
示例#3
0
 def learnCommon(self, mean_dict, covariance_dict, row_dict, label,
                 full_covariance_dict):
     #order of the values in two dictionaries are the same
     #transform dicts to matrix, vector for math operations
     full_cov_vector = misc.dictToNumpyArray(full_covariance_dict)
     mean_vector = misc.dictToNumpyArray(mean_dict)
     row_vector = misc.dictToNumpyArray(row_dict)
     covariance_matrix = misc.dictToUnitNumpyMatrix(covariance_dict)
     if self.classify_train(row_vector, mean_vector, label):
         return mean_dict, covariance_dict, 1  #large margin classified
     else:
         #recalculate mean and covariance
         mean_vector, covariance_matrix = self.update(
             mean_vector, covariance_matrix, row_vector, label,
             full_cov_vector)
         #transform everything back to labeled dictionary
         new_mean_dict = misc.numpyArrayToDict(mean_vector,
                                               list(mean_dict.keys()))
         new_covariance_dict = misc.numpyMatrixToDict(
             covariance_matrix, list(mean_dict.keys()))
         return new_mean_dict, new_covariance_dict, 0
示例#4
0
    def learnCommon(self, mean_dict, covariance_dict, row_dict,
                    label):  #return 1 if correctly classified
        mean_vector = misc.dictToNumpyArray(mean_dict)
        row_vector = misc.dictToNumpyArray(row_dict)
        covariance_matrix = misc.dictToUnitNumpyMatrix(covariance_dict)
        covsum = np.sum(covariance_matrix)

        #
        V = np.dot(np.dot(row_vector, covariance_matrix), row_vector)
        phi = global_phi / (V / covsum)
        M = (row_vector.dot(mean_vector)) * label
        part1 = -(1 + 2 * phi * M)

        #print("Part 1:"+str(M))

        #print("row vector: "+str(row_vector))
        #print("covariance matrix: "+ str(covariance_matrix))
        #print("v"+str(V))
        part2 = np.sqrt(np.square(part1) - 8 * phi * (M - phi * V))
        #print("part2: "+str(part2))
        part3 = 4 * phi * V
        gamma = (part1 + part2) / part3
        #print("gamma: "+str(gamma))
        alpha = np.max(gamma, 0)
        mean_vector = mean_vector + np.dot(
            np.multiply(alpha * label, covariance_matrix), row_vector)
        #print(mean_vector)
        covariance_matrix = covariance_matrix + np.multiply(
            2 * alpha * phi, np.diag(row_vector))

        #transform everything back to labeled dictionary
        new_mean_dict = misc.numpyArrayToDict(mean_vector,
                                              list(mean_dict.keys()))
        new_covariance_dict = misc.numpyMatrixToDict(covariance_matrix,
                                                     list(mean_dict.keys()))
        return new_mean_dict, new_covariance_dict, 0
示例#5
0
文件: olsf.py 项目: CopperWasp/OLVF
 def learnNew(self, row_dict, label, tao):
     row_vector=misc.dictToNumpyArray(row_dict)
     new_weight_vector= np.multiply(tao*label, row_vector)
     new_weight_dict=misc.numpyArrayToDict(new_weight_vector, list(row_dict.keys()))
     return new_weight_dict
示例#6
0
文件: olsf.py 项目: CopperWasp/OLVF
 def learnCommon(self, weight_dict, row_dict, label, tao):
     weight_vector=misc.dictToNumpyArray(weight_dict)
     row_vector=misc.dictToNumpyArray(row_dict)
     new_weight_vector= weight_vector+ np.multiply(tao*label, row_vector)
     new_weight_dict=misc.numpyArrayToDict(new_weight_vector, list(weight_dict.keys()))
     return new_weight_dict