from weka.classifiers import Classifier,Evaluation
from weka.core.classes import Random
def return_classifier_and_estimate(cls_name,options,plot_roc=True):
cvp=Classifier(classname='weka.classifiers.meta.CVParameterSelection')
opt=['-W',cls_name,'-X','10']
for par in options:
opt.append('-P')
opt.append(par)
cvp.options=opt
evl=Evaluation(data)
evl.crossvalidate_model(cvp,data,10,Random(1))
print(evl.percent_correct)
print(evl.summary())
print(evl.class_details()) # add detailed accuracy by class
print(evl.matrix())
if(plot_roc):
import weka.plot.classifiers as plcls # NB: matplotlib is required
plcls.plot_roc(evl, class_index=[0, 1], wait=True)
return cvp
def get_ytest_yprediction(test,classifier):
evl=Evaluation(test)
evl.test_model(classifier,test)
ytest=[]
ypred=[]
for index, inst in enumerate(test):
pred = classifier.classify_instance(inst)
ypred.append(pred)
return ytest,ypred