def predict_proba(self, pairs, features): features = np.array([feature+[1] for feature in features]) probs = [0.0 for i in xrange(len(features))] for i in xrange(len(features)): feature = features[i] if pairs[i][self.field_for_model_num] not in self.val_map_model: lr_val = logisticVal(self.global_para, feature) else: model_idx = self.val_map_model[pairs[i][self.field_for_model_num]] lr_val = logisticVal(self.model_para[model_idx], feature) probs[i] = lr_val return probs
def predict(self, pairs, features): features = np.array([feature+[1] for feature in features]) targets = [0 for i in xrange(len(features))] for i in xrange(len(features)): feature = features[i] if pairs[i][self.field_for_model_num] not in self.val_map_model: lr_val = logisticVal(self.global_para, feature) else: model_idx = self.val_map_model[pairs[i][self.field_for_model_num]] lr_val = logisticVal(self.model_para[model_idx], feature) if lr_val > 0.5: targets[i] = 1 return targets
def predict_proba(self, pairs, features): features = np.array([feature + [1] for feature in features]) probs = [0.0 for i in xrange(len(features))] for i in xrange(len(features)): feature = features[i] if pairs[i][self.field_for_model_num] not in self.val_map_model: lr_val = logisticVal(self.global_para, feature) else: model_idx = self.val_map_model[pairs[i][ self.field_for_model_num]] lr_val = logisticVal(self.model_para[model_idx], feature) probs[i] = lr_val return probs
def predict(self, pairs, features): features = np.array([feature + [1] for feature in features]) targets = [0 for i in xrange(len(features))] for i in xrange(len(features)): feature = features[i] if pairs[i][self.field_for_model_num] not in self.val_map_model: lr_val = logisticVal(self.global_para, feature) else: model_idx = self.val_map_model[pairs[i][ self.field_for_model_num]] lr_val = logisticVal(self.model_para[model_idx], feature) if lr_val > 0.5: targets[i] = 1 return targets
def stochasticGraidentDescent(self, feature, target, model_idx): lr_val = logisticVal(self.model_para[model_idx], feature) avg_model_para = np.mean(self.model_para, 0) self.model_para[model_idx] = self.model_para[model_idx]\ + self.lr*(target*feature-lr_val*feature - self.C*self.model_para[model_idx] - self.eta*(self.model_para[model_idx]-avg_model_para))
def calLossVal(self): correct_num = 0 for i in xrange(self.nInstance): feature = self.features[i] target = self.targets[i] if self.pairs[i][self.field_for_model_num] in self.val_map_model: model_idx = self.val_map_model[self.pairs[i][self.field_for_model_num]] lr_val = logisticVal(self.model_para[model_idx], feature) if np.abs(lr_val-0.5)*target>0: correct_num += 1 return 1-1.0*correct_num/self.nInstance
def calLossVal(self): correct_num = 0 for i in xrange(self.nInstance): feature = self.features[i] target = self.targets[i] if self.pairs[i][self.field_for_model_num] in self.val_map_model: model_idx = self.val_map_model[self.pairs[i][ self.field_for_model_num]] lr_val = logisticVal(self.model_para[model_idx], feature) if np.abs(lr_val - 0.5) * target > 0: correct_num += 1 return 1 - 1.0 * correct_num / self.nInstance
def calLikelihood(self): #pred_prob = np.array([0.0 for i in xrange(self.nInstance)]) loglikelihood = 0.0 for i in xrange(self.nInstance): feature = self.features[i] target = self.targets[i] if self.pairs[i][self.field_for_model_num] in self.val_map_model: model_idx = self.val_map_model[self.pairs[i][self.field_for_model_num]] pred_prob = logisticVal(self.model_para[model_idx], feature) if target == 1: loglikelihood += np.log(pred_prob) else: loglikelihood += np.log(1-pred_prob) #loglikelihood = np.dot(np.log(pred_prob).reshape(1, self.nInstance), self.targets)\ # + np.dot(np.log(1-pred_prob).reshape(1, self.nInstance), 1-self.targets) return loglikelihood
def calLikelihood(self): #pred_prob = np.array([0.0 for i in xrange(self.nInstance)]) loglikelihood = 0.0 for i in xrange(self.nInstance): feature = self.features[i] target = self.targets[i] if self.pairs[i][self.field_for_model_num] in self.val_map_model: model_idx = self.val_map_model[self.pairs[i][ self.field_for_model_num]] pred_prob = logisticVal(self.model_para[model_idx], feature) if target == 1: loglikelihood += np.log(pred_prob) else: loglikelihood += np.log(1 - pred_prob) #loglikelihood = np.dot(np.log(pred_prob).reshape(1, self.nInstance), self.targets)\ # + np.dot(np.log(1-pred_prob).reshape(1, self.nInstance), 1-self.targets) return loglikelihood