예제 #1
0
파일: learner.py 프로젝트: Bollegala/MLIB
def test_logreg(model_fname,test_fname,output_fname=None, prob=True,acc=True):
    """
    Predict labels for the test instances using the trained
    model. If prob is set to True, then show class probabilities.
    If acc is set to True and if the test instances have labels,
    then we will predict accuracies for the test instances.
    If an output_fname is specified we will write the predictions to
    the file instead of writing to the terminal.
    """
    pred = PREDICTOR()
    pred.loadModel(model_fname)
    testFile = SEQUENTIAL_FILE_READER(test_fname)
    count = 0
    E = EVALUATOR(pred.n)
    if output_fname:
        output = open(output_fname,"w")
    else:
        output = sys.stdout
    for mv in testFile:
        v = mv["vect"]
        (lbl,prob) = pred.predictVect(v)
        output.write("%d\t%s\n" % (lbl,str(prob)))
        if pred.n == 2 and v.label == -1 :
            trueLabel = 0
        else:
            trueLabel = v.label
        if v.label is not None:
            E.add(trueLabel,lbl)
        count += 1
    testFile.close()
    if acc:
        result = E.getMetrics()
        E.show(result)
    pass
예제 #2
0
 def get_performance(self, fvects):
     """
     Compute precision, recall and F-scores with the current
     weight vector for the fvects using the EVALUATOR.. 
     """
     E = EVALUATOR(self.n)
     pred = PREDICTOR()
     pred.loadWeights(self.w, self.bias, self.n)
     for v in fvects:
         (lbl, prob) = pred.predictVect(v)
         E.add(v.label, lbl)
     return E
예제 #3
0
파일: learner.py 프로젝트: Bollegala/MLIB
 def get_performance(self,fvects):
     """
     Compute precision, recall and F-scores with the current
     weight vector for the fvects using the EVALUATOR.. 
     """
     E = EVALUATOR(self.n)
     pred = PREDICTOR()
     pred.loadWeights(self.w,self.bias,self.n)
     for v in fvects:
         (lbl,prob) = pred.predictVect(v)
         E.add(v.label,lbl)
     return E
예제 #4
0
def test_logreg(model_fname,
                test_fname,
                output_fname=None,
                prob=True,
                acc=True):
    """
    Predict labels for the test instances using the trained
    model. If prob is set to True, then show class probabilities.
    If acc is set to True and if the test instances have labels,
    then we will predict accuracies for the test instances.
    If an output_fname is specified we will write the predictions to
    the file instead of writing to the terminal.
    """
    pred = PREDICTOR()
    pred.loadModel(model_fname)
    testFile = SEQUENTIAL_FILE_READER(test_fname)
    count = 0
    E = EVALUATOR(pred.n)
    if output_fname:
        output = open(output_fname, "w")
    else:
        output = sys.stdout
    for mv in testFile:
        v = mv["vect"]
        (lbl, prob) = pred.predictVect(v)
        output.write("%d\t%s\n" % (lbl, str(prob)))
        if pred.n == 2 and v.label == -1:
            trueLabel = 0
        else:
            trueLabel = v.label
        if v.label is not None:
            E.add(trueLabel, lbl)
        count += 1
    testFile.close()
    if acc:
        result = E.getMetrics()
        E.show(result)
    pass