Example #1
0
def weka_local_generic_learner(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()
    model = jp.JClass(input_dict['weka_class'])()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'Generic_Weka_learner': sclassifier}
Example #2
0
def weka_local_generic_learner(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()
    model = jp.JClass(input_dict['weka_class'])()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'Generic_Weka_learner': sclassifier}
Example #3
0
def weka_local_libsvm(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()
    model = jp.JClass('weka.classifiers.functions.LibSVM')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'LibSVM_learner': sclassifier}
Example #4
0
def weka_local_arff_to_weka_instances(input_dict):
    '''
    Reads a dataset into a format suitable for WEKA methods
    '''

    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    tmp = common.TemporaryFile(suffix='.arff')
    tmp.writeString(input_dict['arff'])

    try:
        class_index = int(input_dict['class_index'])
    except:
        class_index = None

    source = jp.JClass('weka.core.converters.ConverterUtils$DataSource')(
        tmp.name)
    instances = source.getDataSet()

    if class_index is None:
        print 'Warning: class is set to the last attribute!'
        class_index = instances.numAttributes() - 1
    elif class_index == -1:
        class_index = instances.numAttributes() - 1

    instances.setClassIndex(class_index)

    return {'instances': common.serialize_weka_object(instances)}
Example #5
0
def weka_local_libsvm(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()
    model = jp.JClass('weka.classifiers.functions.LibSVM')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'LibSVM_learner': sclassifier}
Example #6
0
def weka_local_arff_to_weka_instances(input_dict):
    '''
    Reads a dataset into a format suitable for WEKA methods
    '''

    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    tmp = common.TemporaryFile(suffix='.arff')
    tmp.writeString(input_dict['arff'])

    try:
        class_index = int(input_dict['class_index'])
    except:
        class_index = None

    source = jp.JClass('weka.core.converters.ConverterUtils$DataSource')(tmp.name)
    instances = source.getDataSet()

    if class_index is None:
        print 'Warning: class is set to the last attribute!'
        class_index = instances.numAttributes() - 1
    elif class_index == -1:
        class_index = instances.numAttributes() - 1

    instances.setClassIndex(class_index)

    return {'instances': common.serialize_weka_object(instances)}
Example #7
0
def weka_local_multilayer_perceptron(input_dict):
    '''Feedforward artificial neural network, using backpropagation to classify instances
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.MultilayerPerceptron')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'Multilayer_Perceptron_learner': sclassifier}
Example #8
0
def weka_local_smo(input_dict):
    '''A support vector classifier, trained using the Sequential Minimal Optimization (SMO) algorithm
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.SMO')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'SMO_learner': sclassifier}
Example #9
0
def weka_local_multilayer_perceptron(input_dict):
    '''Feedforward artificial neural network, using backpropagation to classify instances
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.MultilayerPerceptron')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'Multilayer_Perceptron_learner': sclassifier}
Example #10
0
def weka_local_random_forest(input_dict):
    '''Random Forest learner by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomForest')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'RandomForest_learner': sclassifier}
Example #11
0
def weka_local_k_star(input_dict):
    '''Instance-Based learner K* by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.KStar')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'KStar_learner': sclassifier}
Example #12
0
def weka_local_naive_bayes(input_dict):
    '''Naive Bayes classifier provided by Weka. Naive Bayes is a simple probabilistic classifier based on applying the Bayes' theorem.
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.bayes.NaiveBayes')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'Naive_Bayes_learner': sclassifier}
Example #13
0
def weka_local_k_star(input_dict):
    '''Instance-Based learner K* by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.KStar')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'KStar_learner': sclassifier}
Example #14
0
def weka_local_random_forest(input_dict):
    '''Random Forest learner by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomForest')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'RandomForest_learner': sclassifier}
Example #15
0
def weka_local_naive_bayes(input_dict):
    '''Naive Bayes classifier provided by Weka. Naive Bayes is a simple probabilistic classifier based on applying the Bayes' theorem.
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.bayes.NaiveBayes')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'Naive_Bayes_learner': sclassifier}
Example #16
0
def weka_local_smo(input_dict):
    '''A support vector classifier, trained using the Sequential Minimal Optimization (SMO) algorithm
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.SMO')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'SMO_learner': sclassifier}
Example #17
0
def weka_local_zeror(input_dict):
    '''Weka's rulesZeroR classifier: predicts the mean (for a numeric class) or the mode (for a nominal class).
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.ZeroR')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'classifier': sclassifier}
Example #18
0
def weka_local_jrip(input_dict):
    '''The RIPPER rule learner by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.JRip')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'JRip_learner': sclassifier}
Example #19
0
def weka_local_rep_tree(input_dict):
    '''A REP Tree, which is a fast decision tree learner. Builds a decision/regression tree using information gain/variance and prunes it using reduced-error pruning
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.REPTree')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'REPTree_learner': sclassifier}
Example #20
0
def weka_local_rep_tree(input_dict):
    '''A REP Tree, which is a fast decision tree learner. Builds a decision/regression tree using information gain/variance and prunes it using reduced-error pruning
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.REPTree')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'REPTree_learner': sclassifier}
Example #21
0
def weka_local_zeror(input_dict):
    '''Weka's rulesZeroR classifier: predicts the mean (for a numeric class) or the mode (for a nominal class).
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.ZeroR')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'classifier': sclassifier}
Example #22
0
def weka_local_jrip(input_dict):
    '''The RIPPER rule learner by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.JRip')()
    model.setOptions(common.parse_options(input_dict['params']))
    sclassifier = common.serialize_weka_object(model)
    return {'JRip_learner': sclassifier}
Example #23
0
def weka_local_j48(input_dict):
    '''Weka decision tree learner J48
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.J48')()
    model.setOptions(common.parse_options(input_dict['params']))

    sclassifier = common.serialize_weka_object(model)
    return {'J48_learner': sclassifier}
Example #24
0
def weka_local_j48(input_dict):
    '''Weka decision tree learner J48
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.J48')()
    model.setOptions(common.parse_options(input_dict['params']))

    sclassifier = common.serialize_weka_object(model)
    return {'J48_learner': sclassifier}
Example #25
0
def weka_local_ibk(input_dict):
    '''K-nearest neighbours classifier by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.IBk')()
    model.setOptions(common.parse_options(input_dict['params']))

    sclassifier = common.serialize_weka_object(model)
    return {'IBk_learner': sclassifier}
Example #26
0
def weka_local_random_tree(input_dict):
    '''A tree that considers K randomly chosen attributes at each node, and performs no pruning
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomTree')()
    model.setOptions(common.parse_options(input_dict['params']))

    sclassifier = common.serialize_weka_object(model)
    return {'RandomTree_learner': sclassifier}
Example #27
0
def weka_local_random_tree(input_dict):
    '''A tree that considers K randomly chosen attributes at each node, and performs no pruning
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomTree')()
    model.setOptions(common.parse_options(input_dict['params']))

    sclassifier = common.serialize_weka_object(model)
    return {'RandomTree_learner': sclassifier}
Example #28
0
def weka_local_ibk(input_dict):
    '''K-nearest neighbours classifier by Weka
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.IBk')()
    model.setOptions(common.parse_options(input_dict['params']))

    sclassifier = common.serialize_weka_object(model)
    return {'IBk_learner': sclassifier}
Example #29
0
def k_star(params=None):
    '''Instance-Based learner K* by Weka

    :param params: parameters in textual form to pass to the KStar Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.KStar')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #30
0
def k_star(params=None):
    '''Instance-Based learner K* by Weka

    :param params: parameters in textual form to pass to the KStar Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.KStar')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #31
0
def random_forest(params=None):
    '''Random Forest learner by Weka

    :param params: parameters in textual form to pass to the RandomForest Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomForest')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #32
0
def rep_tree(params=None):
    '''A REP Tree, which is a fast decision tree learner. Builds a decision/regression tree using information gain/variance and prunes it using reduced-error pruning

    :param params: parameters in textual form to pass to the REPTree Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.REPTree')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #33
0
def random_forest(params=None):
    '''Random Forest learner by Weka

    :param params: parameters in textual form to pass to the RandomForest Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomForest')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #34
0
def naive_bayes(params=None):
    '''Naive Bayes classifier provided by Weka. Naive Bayes is a simple probabilistic classifier based on applying the Bayes' theorem.

    :param params: parameters in textual form to pass to the NaiveBayes Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.bayes.NaiveBayes')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #35
0
def rep_tree(params=None):
    '''A REP Tree, which is a fast decision tree learner. Builds a decision/regression tree using information gain/variance and prunes it using reduced-error pruning

    :param params: parameters in textual form to pass to the REPTree Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.REPTree')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #36
0
def rules_jrip(params=None):
    '''The RIPPER rule learner by Weka

    :param params: parameters in textual form to pass to the JRip Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.JRip')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #37
0
def rules_zeror(params=None):
    '''Weka's rulesZeroR classifier: predicts the mean (for a numeric class) or the mode (for a nominal class).

    :param params: parameters in textual form to pass to the rulesZeroR Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.ZeroR')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #38
0
def ibk(params=None):
    '''K-nearest neighbours classifier by Weka

    :param params: parameters in textual form to pass to the IBk Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.IBk')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #39
0
def logistic(params=None):
    '''Logistic regression by Weka

    :param params: parameters in textual form to pass to the Logistic Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.Logistic')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #40
0
def smo(params=None):
    '''A support vector classifier, trained using the Sequential Minimal Optimization (SMO) algorithm

    :param params: parameters in textual form to pass to the SMO Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.SMO')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #41
0
def naive_bayes(params=None):
    '''Naive Bayes classifier provided by Weka. Naive Bayes is a simple probabilistic classifier based on applying the Bayes' theorem.

    :param params: parameters in textual form to pass to the NaiveBayes Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.bayes.NaiveBayes')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #42
0
def multilayer_perceptron(params=None):
    '''Feedforward artificial neural network, using backpropagation to classify instances

    :param params: parameters in textual form to pass to the MultilayerPerceptron Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.MultilayerPerceptron')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #43
0
def rules_zeror(params=None):
    '''Weka's rulesZeroR classifier: predicts the mean (for a numeric class) or the mode (for a nominal class).

    :param params: parameters in textual form to pass to the rulesZeroR Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.ZeroR')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #44
0
def rules_jrip(params=None):
    '''The RIPPER rule learner by Weka

    :param params: parameters in textual form to pass to the JRip Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.rules.JRip')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #45
0
def random_tree(params=None):
    '''A tree that considers K randomly chosen attributes at each node, and performs no pruning

    :param params: parameters in textual form to pass to the RandomTree Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomTree')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #46
0
def logistic(params=None):
    '''Logistic regression by Weka

    :param params: parameters in textual form to pass to the Logistic Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.Logistic')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #47
0
def ibk(params=None):
    '''K-nearest neighbours classifier by Weka

    :param params: parameters in textual form to pass to the IBk Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.lazy.IBk')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #48
0
def smo(params=None):
    '''A support vector classifier, trained using the Sequential Minimal Optimization (SMO) algorithm

    :param params: parameters in textual form to pass to the SMO Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.SMO')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #49
0
def multilayer_perceptron(params=None):
    '''Feedforward artificial neural network, using backpropagation to classify instances

    :param params: parameters in textual form to pass to the MultilayerPerceptron Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.functions.MultilayerPerceptron')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #50
0
def random_tree(params=None):
    '''A tree that considers K randomly chosen attributes at each node, and performs no pruning

    :param params: parameters in textual form to pass to the RandomTree Weka class
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.RandomTree')()
    model.setOptions(common.parse_options(params))
    return WekaClassifier(common.serialize_weka_object(model))
Example #51
0
def j48(params=None):
    '''Weka decision tree learner J48

    :param params: parameters in textual form to pass to the J48 Weka class (e.g. "-C 0.25 -M 2")
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.J48')()
    model.setOptions(common.parse_options(params))

    return WekaClassifier(common.serialize_weka_object(model))
Example #52
0
def j48(params=None):
    '''Weka decision tree learner J48

    :param params: parameters in textual form to pass to the J48 Weka class (e.g. "-C 0.25 -M 2")
    :return: a WekaClassifier object
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    model = jp.JClass('weka.classifiers.trees.J48')()
    model.setOptions(common.parse_options(params))

    return WekaClassifier(common.serialize_weka_object(model))
Example #53
0
def weka_local_build_classifier(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    instances = common.deserialize_weka_object(input_dict['instances'])
    classifier = common.deserialize_weka_object(input_dict['learner'])

    if instances.classIndex() == -1:
        instances.setClassIndex(instances.numAttributes() - 1)
        # raise ValueError('Class not set!')

    classifier.buildClassifier(instances)
    sclassifier = common.serialize_weka_object(classifier)

    return {'classifier': sclassifier}
Example #54
0
def weka_local_build_classifier(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    instances = common.deserialize_weka_object(input_dict['instances'])
    classifier = common.deserialize_weka_object(input_dict['learner'])

    if instances.classIndex() == -1:
        instances.setClassIndex(instances.numAttributes() - 1)
        # raise ValueError('Class not set!')

    classifier.buildClassifier(instances)
    sclassifier = common.serialize_weka_object(classifier)

    return {'classifier': sclassifier}
Example #55
0
    def build_classifier(self, data):
        """Builds a classifier

        :param data: bunch
        """
        if not jp.isThreadAttachedToJVM():
            jp.attachThreadToJVM()

        instances = ut.convert_bunch_to_weka_instances(data)

        classifier = common.deserialize_weka_object(self.sclassifier)

        if instances.classIndex() == -1:
            instances.setClassIndex(instances.numAttributes() - 1)
            # raise ValueError('Class not set!')

        classifier.buildClassifier(instances)
        self.sclassifier = common.serialize_weka_object(classifier)
Example #56
0
    def build_classifier(self, data):
        """Builds a classifier

        :param data: bunch
        """
        if not jp.isThreadAttachedToJVM():
            jp.attachThreadToJVM()

        instances = ut.convert_bunch_to_weka_instances(data)

        classifier = common.deserialize_weka_object(self.sclassifier)

        if instances.classIndex() == -1:
            instances.setClassIndex(instances.numAttributes() - 1)
            # raise ValueError('Class not set!')

        classifier.buildClassifier(instances)
        self.sclassifier = common.serialize_weka_object(classifier)
Example #57
0
def weka_local_apply_mapped_classifier_get_instances(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    MAPPING_REPORT_START = 'Attribute mappings:'

    classifier = common.deserialize_weka_object(input_dict['classifier'])
    original_training_instances = common.deserialize_weka_object(
        input_dict['original_training_instances'])
    instances = common.deserialize_weka_object(input_dict['instances'])

    # serialize classifier with original instances to a file once again for the Mapped classifier
    tfile = common.TemporaryFile(flags='wb+')
    s = jp.JClass('weka.core.SerializationHelper')
    s.writeAll(tfile.name, [classifier, original_training_instances])

    # construct a MappedClassifier
    mappedClassifier = jp.JClass(
        'weka.classifiers.misc.InputMappedClassifier')()
    mappedClassifier.setIgnoreCaseForNames(True)
    mappedClassifier.setTrim(True)
    #mappedClassifier.setSuppressMappingReport(True)
    #mc.setModelHeader(original_training_instances)
    mappedClassifier.setModelPath(tfile.name)

    # use the mapped classifier on new data
    classIndex = instances.classIndex()
    if classIndex == -1:
        raise ValueError('Class not set!')
    classAttribute = instances.classAttribute()
    for instance in instances:
        label = int(mappedClassifier.classifyInstance(instance))
        instance.setClassValue(classAttribute.value(label))

    report = mappedClassifier.toString()
    if MAPPING_REPORT_START in report:
        report = report[report.index(MAPPING_REPORT_START):]

    return {
        'mapping_report': report,
        'instances': common.serialize_weka_object(instances)
    }
Example #58
0
def weka_local_apply_classifier_and_get_instances(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    # print("Instances: %s" % type(input_dict['instances']))
    instances = common.deserialize_weka_object(input_dict['instances'])

    if instances.classIndex() == -1:
        instances.setClassIndex(instances.numAttributes() - 1)  # last attribute is class

    classifier_serialized = input_dict['classifier']
    try:
        classifier = common.deserialize_weka_object(classifier_serialized)
        classAttribute = instances.classAttribute()
        for instance in instances:
            label_ind = int(classifier.classifyInstance(instance))
            instance.setClassValue(classAttribute.value(label_ind))

        return {'instances': common.serialize_weka_object(instances)}
    except:
        raise Exception("Classifier not built. Please use the Build Classifier widget first.")
Example #59
0
def weka_local_apply_mapped_classifier_get_instances(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    MAPPING_REPORT_START = 'Attribute mappings:'

    classifier = common.deserialize_weka_object(input_dict['classifier'])
    original_training_instances = common.deserialize_weka_object(input_dict['original_training_instances'])
    instances = common.deserialize_weka_object(input_dict['instances'])

    # serialize classifier with original instances to a file once again for the Mapped classifier
    tfile = common.TemporaryFile(flags='wb+')
    s = jp.JClass('weka.core.SerializationHelper')
    s.writeAll(tfile.name, [classifier, original_training_instances])

    # construct a MappedClassifier
    mappedClassifier = jp.JClass('weka.classifiers.misc.InputMappedClassifier')()
    mappedClassifier.setIgnoreCaseForNames(True)
    mappedClassifier.setTrim(True)
    #mappedClassifier.setSuppressMappingReport(True)
    #mc.setModelHeader(original_training_instances)
    mappedClassifier.setModelPath(tfile.name)

    # use the mapped classifier on new data
    classIndex = instances.classIndex()
    if classIndex == -1:
        raise ValueError('Class not set!')
    classAttribute = instances.classAttribute()
    for instance in instances:
        label = int(mappedClassifier.classifyInstance(instance))
        instance.setClassValue(classAttribute.value(label))

    report = mappedClassifier.toString()
    if MAPPING_REPORT_START in report:
        report = report[report.index(MAPPING_REPORT_START):]

    return {'mapping_report':report, 'instances':common.serialize_weka_object(instances)}
Example #60
0
def weka_local_apply_classifier_and_get_instances(input_dict):
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    # print("Instances: %s" % type(input_dict['instances']))
    instances = common.deserialize_weka_object(input_dict['instances'])

    if instances.classIndex() == -1:
        instances.setClassIndex(instances.numAttributes() -
                                1)  # last attribute is class

    classifier_serialized = input_dict['classifier']
    try:
        classifier = common.deserialize_weka_object(classifier_serialized)
        classAttribute = instances.classAttribute()
        for instance in instances:
            label_ind = int(classifier.classifyInstance(instance))
            instance.setClassValue(classAttribute.value(label_ind))

        return {'instances': common.serialize_weka_object(instances)}
    except:
        raise Exception(
            "Classifier not built. Please use the Build Classifier widget first."
        )