def predict(self, Xtest): testSize = Xtest.shape[0] ytest = np.zeros(testSize) for index in range(testSize): testData = Xtest[index, 0:self.lastCol] prob0 = 0 prob1 = 0 for ft in range(self.lastCol): try: prob0 += log( utils.calculateprob(Xtest[index, ft], self.class0Mean[ft, ], self.class0Std[ft, ])) prob1 += log( utils.calculateprob(Xtest[index, ft], self.class1Mean[ft, ], self.class1Std[ft, ])) except ValueError: if utils.calculateprob(Xtest[index, ft], self.class0Mean[ft, ], self.class0Std[ft, ]) == 0: prob0 += 0 elif utils.calculateprob(Xtest[index, ft], self.class1Mean[ft, ], self.class1Std[ft, ]) == 0: prob1 += 0 ytest[index] = np.float64(0) if prob0 > prob1 else np.float(1) return ytest
def predict(self, Xtest): if self.params['notusecolumnones']: Xtest = Xtest[:, np.array(xrange(Xtest.shape[1] - 1))] #ttemp=self.skGNB.predict(Xtest) ''' print self.skGNB.theta_ print self.sigma print "" print "" print self.skGNB.sigma_ print self.variance ''' dim = [Xtest.shape[0], Xtest.shape[1]] probNB1 = [[ utils.calculateprob(Xtest[r, c], self.sigma[1][c], math.sqrt(self.variance[1][c])) for c in xrange(dim[1]) ] for r in xrange(dim[0])] #print "Shape Of probNB1",len(probNB1),len(probNB1[0]) probNB0 = [[ utils.calculateprob(Xtest[r, c], self.sigma[0][c], math.sqrt(self.variance[0][c])) for c in xrange(dim[1]) ] for r in xrange(dim[0])] NB1MLPR = (np.prod(probNB1, axis=1)) * self.probY[1] #print "Shape of NB1MLPR",len(NB1MLPR) #print NB1MLPR NB0MLPR = (np.prod(probNB0, axis=1)) * self.probY[0] ytest = [1 if NB1MLPR[x] > NB0MLPR[x] else 0 for x in xrange(dim[0])] return ytest
def predict(self, Xtest): # noOfFeatures = Xtest.shape[1] if not self.getparams()['usecolumnones']: Xtest = Xtest[:, :-1] ytest = np.zeros(Xtest.shape[0]) for i in range(0, Xtest.shape[0]): prob_Class0 = self.ymean_Class0 prob_Class1 = self.ymean_Class1 for j in range(0, len(self.mean_Class0)): prob_Class0 = prob_Class0 * utils.calculateprob(Xtest[i][j], self.mean_Class0[j], self.std_Class0[j]) prob_Class1 = prob_Class1 * utils.calculateprob(Xtest[i][j], self.mean_Class1[j], self.std_Class1[j]) if prob_Class0 < prob_Class1: ytest[i] = 1 return ytest
def predict(self, Xtest): Xtest = Xtest[:,0:self.features] numsamples = Xtest.shape[0] ytest = np.empty(numsamples) for i in xrange(numsamples): zeroprobs = self.priozero oneprobs = self.prioone for f in xrange(self.features): zeroprobs = zeroprobs * utils.calculateprob(Xtest[i,f], self.meanstdev[0,0,f], self.meanstdev[0,1,f]) oneprobs = oneprobs * utils.calculateprob(Xtest[i,f], self.meanstdev[1,0,f], self.meanstdev[1,1,f]) if (zeroprobs <= oneprobs): ytest[i] = 1 else: ytest[i] = 0 return ytest
def likelihoodcal(self, dpt, distparams, featurelength): if featurelength >= 0: mu = distparams[featurelength]['mu'] sigma = distparams[featurelength]['sigma'] j = utils.calculateprob(dpt[featurelength], mu, sigma) return j * self.likelihoodcal(dpt, distparams, featurelength-1) else: return 1
def likelihoodcal(self, dpt, distparams, featurelength): if featurelength >= 0: mu = distparams[featurelength]['mu'] sigma = distparams[featurelength]['sigma'] j = utils.calculateprob(dpt[featurelength], mu, sigma) return j * self.likelihoodcal(dpt, distparams, featurelength - 1) else: return 1
def probbydivide(self, dividedDS, Xtest): prob = {} for val, var in dividedDS.items(): prob[val] = 1 for i in range(len(var)): mean, stdev = var[i] x = Xtest[i] probability = utils.calculateprob(x, mean, stdev) prob[val] = prob[val] * probability return prob
def probbydivide(self,dividedDS, Xtest): prob = {} for val, var in dividedDS.items(): prob[val] = 1 for i in range(len(var)): mean, stdev = var[i] x = Xtest[i] probability=utils.calculateprob(x, mean, stdev) prob[val]=prob[val]* probability return prob
def calculate_best(self, x): probability1 = 1 probability0 = 1 # computing product of all p(x_i|y) for i in range(0, len(x)): # probability of zero happening, ignoring the denom and the 1/2 at the beginning since they are the same for both probability0 *= utils.calculateprob(x[i], self.mv0[i][0], self.mv0[i][1]) # probability of one happening, ignoring the denom and the 1/2 at the beginning since they are the same for both probability1 *= utils.calculateprob(x[i], self.mv1[i][0], self.mv1[i][1]) # now multiplty by p(y) probability0 *= self.freq0 probability1 *= self.freq1 # pick the larger of the two probabilities if (probability0 > probability1): return 0 else: return 1
def pred(xvec): p = {} for Value, groups in self.groupings.iteritems(): #class0: (mean1,stddev1), (mean2,stddev2).... #class1: (mean1,stddev1), (mean2,stddev2).... p[Value] = 1 for i in range(len(groups)): mean, stdev = groups[i] x = xvec[i] p[Value] *= utils.calculateprob(x, mean, stdev) #only for 2 classes if p[0] > p[1]: return 0 else: return 1
def predict(self, Xtest): if self.params['usecolumnones'] == False: Xtest = Xtest[:, :-1] ytest = [] for row in Xtest: p_zero = 1 p_one = 1 for datapoint_index in range(row.shape[0]): p_zero = p_zero * (utils.calculateprob( row[datapoint_index], self.Xtrain_Zero_Mean[datapoint_index], self.Xtrain_Zero_Std[datapoint_index])) p_one = p_one * (utils.calculateprob( row[datapoint_index], self.Xtrain_One_Mean[datapoint_index], self.Xtrain_One_Std[datapoint_index])) if p_one >= p_zero: ytest.append(1) else: ytest.append(0) return ytest
def predict(self, Xtest): predictions = [] for tt in range(Xtest.shape[0]): inputVector = Xtest[tt] probabilities = {} for classValue, classSummaries in self.summaries.iteritems(): probabilities[classValue] = 1 for i in range(len(classSummaries)): mean, stdev = classSummaries[i] x = inputVector[i] probabilities[classValue] *= utils.calculateprob(x, mean, stdev) bestLabel, bestProb = None, -1 for classValue, probability in probabilities.iteritems(): if bestLabel is None or probability > bestProb: bestProb = probability bestLabel = classValue predictions.append(bestLabel) return predictions
def predict(self, Xtest): predictions = [] for tt in range(Xtest.shape[0]): inputVector = Xtest[tt] probabilities = {} for classValue, classSummaries in self.summaries.iteritems(): probabilities[classValue] = 1 for i in range(len(classSummaries)): mean, stdev = classSummaries[i] x = inputVector[i] probabilities[classValue] *= utils.calculateprob( x, mean, stdev) bestLabel, bestProb = None, -1 for classValue, probability in probabilities.iteritems(): if bestLabel is None or probability > bestProb: bestProb = probability bestLabel = classValue predictions.append(bestLabel) return predictions
def predict(self, Xtest): ytest = [] for i in range(0, Xtest.shape[0]): feat_prob_all = [] for k in range(0, 2): feat_prob = [] for j in range(0, self.nof): featname = "Feature" + str(j) mean = self.prob_table[featname][k]["mu"] stdev = self.prob_table[featname][k]["sig"] fprob = utils.calculateprob(Xtest[i, j], mean, stdev) feat_prob.append(fprob) feat_prob_all.append(feat_prob) prob_pos = np.prod(np.array(feat_prob_all[0])) * self.prior_prob[0] prob_neg = np.prod(np.array(feat_prob_all[1])) * self.prior_prob[1] if prob_pos > prob_neg: ytest.append(0) else: ytest.append(1) ytest = np.asarray(ytest) return ytest