示例#1
0
def trainUsingNaiveBayes(Data, delim):
    
    #print "In train Func"
    
    NB = NaiveBayes()
    ##instances = []
    NB.totalInstances = len(Data)
    count = 1
    for sample in Data:
        instance = dataInstance(sample[1],sample[0],delim)
        NB.trainOnline(instance, count)
        count += 1
        
        ##instances.append(instance)
        
        #instances.append(dataInstance(sample[1],sample[0]))
        #print len(instances[-1].feats)
        #print len(instances)
        #dummy = raw_input()
        
        #instance.feats = ""
    
    print
    
    #print "Made instances"
    
    #NB.train(instances)
    NB.smooth()
    
    return NB
示例#2
0
def predictGender(NB, Data, outFile, delim):
    
    status = 0
    
    outFile = open(outFile,'w')
    
    correct = 0
    for sample in Data:
        instance = dataInstance(sample[1],sample[0],delim)
        #print len(instance.feats)
        #dummy = raw_input()
        outputLabel = NB.classify(instance) 
        if outputLabel == instance.classLabel:
            correct += 1
        outFile.write(sample[0]+"\t"+sample[1]+ "\t" + outputLabel +"\n")
        
        status += 1
            
        try:
            dummy = 1/(status%1000)
        except:
            print status,
            
    print
    
    outFile.write(str(round(correct*100.0/len(Data),2)))
    outFile.close()
    
    return round(correct*100.0/len(Data),2)