Example #1
0
def apply_mapped_classifier_get_instances(weka_classifier, original_data,
                                          data):
    '''An advanced version of the Apply Classifier method.
    Addresses incompatible training and test data, and returns a dataset with predictions.

    :param weka_classifier: WekaClassifier object
    :param original_data: original training instances, bunch
    :param data: test instances, bunch
    :return: Dataset (Bunch) object with predictions and a textual report from the InputMappedClassifier class
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    try:
        classifier = common.deserialize_weka_object(
            weka_classifier.sclassifier)
    except:
        raise Exception(
            "Only WEKA classifiers/models supported. Please provide a valid WEKA learner."
        )

    original_training_instances = ut.convert_bunch_to_weka_instances(
        original_data)
    instances = ut.convert_bunch_to_weka_instances(data)

    # 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
    mapped_classifier = jp.JClass(
        'weka.classifiers.misc.InputMappedClassifier')()
    mapped_classifier.setIgnoreCaseForNames(True)
    mapped_classifier.setTrim(True)
    # mapped_classifier.setSuppressMappingReport(True)
    # mc.setModelHeader(original_training_instances)
    mapped_classifier.setModelPath(tfile.name)

    predictions = []
    try:
        for instance in instances:
            label = int(mapped_classifier.classifyInstance(instance))
            predictions.append(label)

        data["targetPredicted"] = predictions
    except:
        raise Exception(
            "Classifier not built. Please use the Build Classifier widget first."
        )

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

    return data, report
Example #2
0
def apply_mapped_classifier_get_instances(weka_classifier, original_data, data):
    '''An advanced version of the Apply Classifier method.
    Addresses incompatible training and test data, and returns a dataset with predictions.

    :param weka_classifier: WekaClassifier object
    :param original_data: original training instances, bunch
    :param data: test instances, bunch
    :return: Dataset (Bunch) object with predictions and a textual report from the InputMappedClassifier class
    '''
    if not jp.isThreadAttachedToJVM():
        jp.attachThreadToJVM()

    try:
        classifier = common.deserialize_weka_object(weka_classifier.sclassifier)
    except:
        raise Exception("Only WEKA classifiers/models supported. Please provide a valid WEKA learner.")

    original_training_instances = ut.convert_bunch_to_weka_instances(original_data)
    instances = ut.convert_bunch_to_weka_instances(data)

    # 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
    mapped_classifier = jp.JClass('weka.classifiers.misc.InputMappedClassifier')()
    mapped_classifier.setIgnoreCaseForNames(True)
    mapped_classifier.setTrim(True)
    # mapped_classifier.setSuppressMappingReport(True)
    # mc.setModelHeader(original_training_instances)
    mapped_classifier.setModelPath(tfile.name)

    predictions = []
    try:
        for instance in instances:
            label = int(mapped_classifier.classifyInstance(instance))
            predictions.append(label)

        data["targetPredicted"] = predictions
    except:
        raise Exception("Classifier not built. Please use the Build Classifier widget first.")

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

    return data, report
Example #3
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 #4
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)
def wekaLocalExportDatasetToARFF(request,input_dict,output_dict,widget):
    import utilities as ut
    from cf_core import helpers

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

    bunch = input_dict['instances']
    output_dict = {}

    instances = ut.convert_bunch_to_weka_instances(bunch)

    destination = helpers.get_media_root()+'/'+str(request.user.id)+'/'+str(widget.id)+'.arff'

    f = open(destination, 'w')
    s = instances.toString()
    f.write(s)
    f.close()

    filename = str(request.user.id)+'/'+str(widget.id)+'.arff'
    output_dict['filename'] = filename

    return render(request, 'visualizations/string_to_file.html',{'widget':widget,'input_dict':input_dict,'output_dict':output_dict})
Example #6
0
    def apply_classifier(self, data):
        """Applies a classifier on a dataset, and gets predictions

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

        instances = ut.convert_bunch_to_weka_instances(data)

        classifier = common.deserialize_weka_object(self.sclassifier)

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

        predictions = []
        for instance in instances:
            label = int(classifier.classifyInstance(instance))
            predictions.append(label)

        data["targetPredicted"] = predictions
        return data
Example #7
0
    def apply_classifier(self, data):
        """Applies a classifier on a dataset, and gets predictions

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

        instances = ut.convert_bunch_to_weka_instances(data)

        classifier = common.deserialize_weka_object(self.sclassifier)

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

        predictions = []
        for instance in instances:
            label = int(classifier.classifyInstance(instance))
            predictions.append(label)

        data["targetPredicted"] = predictions
        return data