def testSingle(self, original_row): row = original_row.copy() label = row.pop('class_label', None) #extract the label commonKeys = misc.findCommonKeys( row, self.mean_dict) #do the classifications based on shared keys row_subset = misc.subsetDictionary(row, commonKeys) mean_subset = misc.subsetDictionary(self.mean_dict, commonKeys) row_vector = misc.dictToNumpyArray(row_subset) mean_vector = misc.dictToNumpyArray(mean_subset) covariance_vector = misc.dictToNumpyArray( misc.subsetDictionary( self.covariance_dict, commonKeys)) #this is for alternative classification style if len(covariance_vector) == 0: return False if np.average(covariance_vector) > np.average( list(self.covariance_dict.values())) * 2.2: return np.random.choice([True, False]) if np.sum(mean_vector) != 0: #confidences= np.divide(np.reciprocal(covariance_vector), np.sum(np.reciprocal(covariance_vector))) mean_vector = np.multiply( np.divide(mean_vector, np.sum(np.abs(mean_vector))), np.sum(np.abs(list(self.mean_dict.values())))) #mean_vector= [a*b for a,b in zip(mean_vector,confidences)] return self.algorithm_methods.classify( row_vector, mean_vector, covariance_vector, label) #check if it can classify correctly
def sparsify(self): with warnings.catch_warnings(record=True) as w: if parameters.sparsity_switch == 0: #dont touch return lamb = parameters.sparsity_regularizer trunc = parameters.sparsity_B mean_vector = misc.dictToNumpyArray(self.mean_dict) covariance_vector = misc.dictToNumpyArray(self.covariance_dict) value_vector = covariance_vector if w: print("mean vector: " + str(min(mean_vector))) print("covariance vector: " + str(min(covariance_vector))) warnings.simplefilter("error") sys.exit() num_elem_remove = int(len(value_vector) * (1 - trunc)) if (np.linalg.norm(value_vector, ord=1) > trunc * len(value_vector)): coefficient = np.minimum( 1, lamb / np.linalg.norm(value_vector, ord=1)) copy_mean_dict = self.mean_dict.copy() #multiply by coefficient for key, value in copy_mean_dict.items(): copy_mean_dict[key] = value * coefficient #print("remove elements: "+str(num_elem_remove)) for i in range(0, num_elem_remove): key_to_delete = max(copy_mean_dict, key=lambda k: copy_mean_dict[k]) copy_mean_dict.pop(key_to_delete) self.mean_dict.pop(key_to_delete) self.covariance_dict.pop(key_to_delete)
def updateWeights(self, row, weights, label): row_vec = misc.dictToNumpyArray(row) weights_vec = misc.dictToNumpyArray(weights) self.classifier.coef_ = np.array([weights_vec]) self.classifier.partial_fit([row_vec], [label], np.unique(self.y)) self.weight_dict = self.repackDict(self.classifier.coef_[0], list(weights.keys()))
def testSingle(self, original_row): row=original_row.copy() label=row.pop('class_label', None) #extract the label commonKeys=misc.findCommonKeys(row, self.weight_dict) #do the classifications based on shared keys row_subset=misc.subsetDictionary(row, commonKeys) weight_subset=misc.subsetDictionary(self.weight_dict, commonKeys) row_vector=misc.dictToNumpyArray(row_subset) weight_vector=misc.dictToNumpyArray(weight_subset) loss=np.maximum(0, 1-label*(weight_vector.dot(row_vector))) product=label*(weight_vector.dot(row_vector)) return loss, product #check if it can classify correctly
def testSingle(self, original_row): row = original_row.copy() label = row.pop('class_label', None) #extract the label commonKeys = misc.findCommonKeys( row, self.mean_dict) #do the classifications based on shared keys row_subset = misc.subsetDictionary(row, commonKeys) mean_subset = misc.subsetDictionary(self.mean_dict, commonKeys) row_vector = misc.dictToNumpyArray(row_subset) mean_vector = misc.dictToNumpyArray(mean_subset) covariance_vector = misc.dictToNumpyArray( misc.subsetDictionary( self.covariance_dict, commonKeys)) #this is for alternative classification style return self.classify(row_vector, mean_vector, covariance_vector, label) #check if it can classify correctly
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
def setParameter(self, loss, row): row= misc.dictToNumpyArray(row) if self.selector==0: #OLSF return loss/((np.linalg.norm(row))*((np.linalg.norm(row)))) elif self.selector==1: #OLSF1 return np.minimum(self.C, loss/((np.linalg.norm(row))*((np.linalg.norm(row))))) else: #OLSF2 return loss/((np.linalg.norm(row)*np.linalg.norm(row))+(1/(2*self.C)))
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
def sparsity_step(self): weight_vector=misc.dictToNumpyArray(self.weight_dict) #l1 projection new_weight_vector= np.multiply(np.minimum(1, self.param_lambda/ np.linalg.norm(weight_vector, ord=1)), weight_vector) #truncation return_dict=self.weight_dict if np.linalg.norm(new_weight_vector, ord=0) >= self.B*len(new_weight_vector): num_elem_remove=int(len(new_weight_vector)- np.maximum(1, np.floor(self.B* len(new_weight_vector)))) copy_dict= copy.deepcopy(return_dict) sorted_keys= sorted(copy_dict, key=lambda dict_key: abs(copy_dict[dict_key])) for key in sorted_keys[:num_elem_remove]: del return_dict[key] return return_dict
def test(self): #returns average test accuracy counter = 0 #correct counter for original_row in self.test_dataset: row = original_row.copy() label = row.pop('class_label', None) #extract the label commonKeys = misc.findCommonKeys( row, self.mean_dict) #do the classifications based on shared keys row_subset = misc.subsetDictionary(row, commonKeys) mean_subset = misc.subsetDictionary(self.mean_dict, commonKeys) row_vector = misc.dictToNumpyArray(row_subset) mean_vector = misc.dictToNumpyArray(mean_subset) covariance_vector = misc.dictToNumpyArray( misc.subsetDictionary( self.covariance_dict, commonKeys)) #this is for alternative classification style if self.algorithm_methods.classify( row_vector, mean_vector, covariance_vector, label) == False: #check if it can classify correctly counter += 1 return counter / len( self.test_dataset ) #return number of false classification over all examples
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
def sparsity_step(self): weight_vector = misc.dictToNumpyArray(self.mean_dict) #l1 projection new_weight_vector = np.multiply( np.minimum( 1, olsf.param_lambda / np.linalg.norm(weight_vector, ord=1)), weight_vector) #truncation return_dict = self.mean_dict if np.linalg.norm(new_weight_vector, ord=0) >= olsf.B * len(new_weight_vector): num_elem_remove = int( len(new_weight_vector) - np.maximum(1, np.floor(olsf.B * len(new_weight_vector)))) #print(len(return_dict)) return_dict = dict( sorted(return_dict.items(), key=operator.itemgetter(1), reverse=True)[:num_elem_remove]) #print(len(return_dict)) return return_dict
def predictSingle(self, row, weights, label): row_vec = misc.dictToNumpyArray(row) weights_vec = misc.dictToNumpyArray(weights) #print(weights_vec) self.classifier.coef_ = np.array([weights_vec]) return label != self.classifier.predict([row_vec])
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
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