Пример #1
0
def load_classifier(lang, tag):
    classifier = {}

    if lang == LANG_ID and tag == "nnp":
        objects = serialization.read_all(ID_MODEL_NNP)
    elif lang == LANG_ID and tag == "nn":
        objects = serialization.read_all(ID_MODEL_NN)
    elif lang == LANG_ID and tag == "cdp":
        objects = serialization.read_all(ID_MODEL_CDP)

    elif lang == LANG_EN and tag == "nnp":
        objects = serialization.read_all(EN_MODEL_NNP)
    elif lang == LANG_EN and tag == "jj":
        objects = serialization.read_all(EN_MODEL_JJ)
    elif lang == LANG_EN and tag == "nn":
        objects = serialization.read_all(EN_MODEL_NN)
    elif lang == LANG_EN and tag == "vbp":
        objects = serialization.read_all(EN_MODEL_VBP)
    elif lang == LANG_EN and tag == "cd":
        objects = serialization.read_all(EN_MODEL_CD)
    elif lang == LANG_EN and tag == "vb":
        objects = serialization.read_all(EN_MODEL_VB)

    classifier['classifier'] = Classifier(jobject=objects[0])
    classifier['filter'] = Filter(jobject=objects[1])
    return classifier
def PredecirUnaTemporada(path):
    jvm.start()
    insta = CrearInstanciaParaPredecir(path)
    atributos = ""
    file = open('ModelData/wekaHeader.arff', 'r')
    atributos = file.readlines()
    file.close()

    file = open('ModelData/predictionFiles/inst.arff', 'w')
    file.writelines(atributos)
    file.write("\n" + insta + '\n')
    file.close()

    objects = serialization.read_all("ModelData/77PercentModelPaisajes.model")
    classifier = Classifier(jobject=objects[0])

    loader = Loader()
    data = loader.load_file("ModelData/predictionFiles/inst.arff")
    data.class_is_last()

    clases = ["invierno", "verano", "otono", "primavera"]
    prediccion = ""
    for index, inst in enumerate(data):
        pred = classifier.classify_instance(inst)
        dist = classifier.distribution_for_instance(inst)
        prediccion = clases[int(pred)]
    jvm.stop()
    return prediccion
Пример #3
0
    def test_read_write_all(self):
        """
        Tests methods read_all and write_all.
        """
        fname = self.tempfile("readwrite.ser")
        self.delfile(fname)

        lin = []
        for i in range(4):
            lin.append(javabridge.make_instance("java/lang/Integer", "(I)V",
                                                i))
        serialization.write_all(fname, lin)
        self.assertTrue(os.path.exists(fname),
                        msg="Failed to write to " + fname + "?")

        lout = serialization.read_all(fname)
        self.assertIsNotNone(lout, msg="Failed to read from " + fname + "?")
        self.delfile(fname)
        self.assertEqual(len(lin), len(lout), msg="Number of elements differ")
        for i in range(len(lin)):
            iin = javabridge.call(lin[i], "intValue", "()I")
            iout = javabridge.call(lout[i], "intValue", "()I")
            self.assertEqual(iin,
                             iout,
                             msg="Input/output differ at #" + str(i))
Пример #4
0
    def predBtn_clicked(self):

        gender = self.gender_entry.get()
        age = int(self.age_entry.get())
        height = int(self.height_entry.get())
        weight = int(self.weight_entry.get())
        sociability = self.sociability_entry.get()
        stability = self.stability_entry.get()
        '''Create the model'''
        objects = serialization.read_all("J48.model")

        cls = Classifier(jobject=objects[0])
        data = Instances(jobject=objects[1])
        '''Create the test set to be classified'''
        gender_values = ["Man", "Woman"]
        sociability_values = ["Introvert", "Extrovert"]
        stability_values = ["Stable", "Unstable"]

        values = [
            gender_values.index(gender), age, height, weight,
            self.BMI(weight, height),
            stability_values.index(stability),
            sociability_values.index(sociability),
            Instance.missing_value()
        ]

        inst = Instance.create_instance(values)
        inst.dataset = data
        '''Classification'''
        prediction = int(cls.classify_instance(inst))
        self.controller.show_frame("Result").show(prediction)
        self.clear()
Пример #5
0
def load_model(filename):
    """ Load the model from cache.
    Args:
        filename(str): The target file name (without extension) to load. Example: LMT
    Returns:
        The classifier and data object if the target caching is saved, otherwise None.
    """

    # Path to the cashed model (example: caches/model/LMT.cache)
    path = os.path.join(os.path.join('caches', 'model'), filename + '.cache')

    print("Path to the cashed model to load:", path)

    if os.path.isfile(path):
        cached_model, cached_data_used_for_training = serialization.read_all(
            path)
        print("Loading cached classifier")
        trained_classifier = Classifier(jobject=cached_model)
        print("Loading cached data")
        training_data = Instances(jobject=cached_data_used_for_training)
        localizer_log.msg("Loaded model: {filename}".format(filename=filename))
        return [trained_classifier, training_data]

    localizer_log.msg("Failed to load cache of 'model'.")
    return None
Пример #6
0
def TestClassification(arff, modelInput, results):
    # 启动java虚拟机
    jvm.start()
    # 导入分析模型
    objects = serialization.read_all(modelInput)
    clsf = Classifier(jobject=objects[0])
    print(clsf)
    # 导入测试组
    loader = Loader(classname="weka.core.converters.ArffLoader")
    test = loader.load_file(arff)
    test.class_is_first()
    # 分析结果
    resultsFile = open(results, "w")
    resultsFile.write("序号\t原判断\t预测\t良性概率\t恶性概率\n")
    print("序号\t原判断\t预测\t良性概率\t恶性概率")
    for index, inst in enumerate(test):
        pred = clsf.classify_instance(inst)
        dist = clsf.distribution_for_instance(inst)
        sampleID = index + 1
        origin = inst.get_string_value(inst.class_index)
        prediction = inst.class_attribute.value(int(pred))
        sameAsOrigin = "yes" if pred != inst.get_value(
            inst.class_index) else "no"
        NRate = dist.tolist()[0]
        PRate = dist.tolist()[1]
        resultsFile.write(
            "%d\t%s\t%s\t%s\t%s" %
            (sampleID, origin, prediction, str(NRate), str(PRate)) + "\n")
        print("%d\t%s\t%s\t%s\t%s" %
              (sampleID, origin, prediction, str(NRate), str(PRate)))
    resultsFile.close()
    # 退出java虚拟机
    jvm.stop()
    print("检测完成")
def main():
    """
    Just runs some example code.
    """

    # load a dataset
    iris_file = helper.get_data_dir() + os.sep + "iris.arff"
    helper.print_info("Loading dataset: " + iris_file)
    loader = Loader("weka.core.converters.ArffLoader")
    iris_data = loader.load_file(iris_file)
    iris_data.class_is_last()

    # train classifier
    classifier = Classifier("weka.classifiers.trees.J48")
    classifier.build_classifier(iris_data)

    # save and read object
    helper.print_title("I/O: single object")
    outfile = tempfile.gettempdir() + os.sep + "j48.model"
    serialization.write(outfile, classifier)
    model = Classifier(jobject=serialization.read(outfile))
    print(model)

    # save classifier and dataset header (multiple objects)
    helper.print_title("I/O: single object")
    serialization.write_all(outfile, [classifier, Instances.template_instances(iris_data)])
    objects = serialization.read_all(outfile)
    for i, obj in enumerate(objects):
        helper.print_info("Object #" + str(i+1) + ":")
        if javabridge.get_env().is_instance_of(obj, javabridge.get_env().find_class("weka/core/Instances")):
            obj = Instances(jobject=obj)
        elif javabridge.get_env().is_instance_of(obj, javabridge.get_env().find_class("weka/classifiers/Classifier")):
            obj = Classifier(jobject=obj)
        print(obj)
Пример #8
0
def main():
    """
    Just runs some example code.
    """

    # load a dataset
    iris_file = helper.get_data_dir() + os.sep + "iris.arff"
    helper.print_info("Loading dataset: " + iris_file)
    loader = Loader("weka.core.converters.ArffLoader")
    iris_data = loader.load_file(iris_file)
    iris_data.class_is_last()

    # train classifier
    classifier = Classifier("weka.classifiers.trees.J48")
    classifier.build_classifier(iris_data)

    # save and read object
    helper.print_title("I/O: model (using serialization module)")
    outfile = tempfile.gettempdir() + os.sep + "j48.model"
    serialization.write(outfile, classifier)
    model = Classifier(jobject=serialization.read(outfile))
    print(model)

    # save classifier and dataset header (multiple objects)
    helper.print_title("I/O: model and header (using serialization module)")
    serialization.write_all(
        outfile,
        [classifier, Instances.template_instances(iris_data)])
    objects = serialization.read_all(outfile)
    for i, obj in enumerate(objects):
        helper.print_info("Object #" + str(i + 1) + ":")
        if javabridge.get_env().is_instance_of(
                obj,
                javabridge.get_env().find_class("weka/core/Instances")):
            obj = Instances(jobject=obj)
        elif javabridge.get_env().is_instance_of(
                obj,
                javabridge.get_env().find_class(
                    "weka/classifiers/Classifier")):
            obj = Classifier(jobject=obj)
        print(obj)

    # save and read object
    helper.print_title("I/O: just model (using Classifier class)")
    outfile = tempfile.gettempdir() + os.sep + "j48.model"
    classifier.serialize(outfile)
    model, _ = Classifier.deserialize(outfile)
    print(model)

    # save classifier and dataset header (multiple objects)
    helper.print_title("I/O: model and header (using Classifier class)")
    classifier.serialize(outfile, header=iris_data)
    model, header = Classifier.deserialize(outfile)
    print(model)
    if header is not None:
        print(header)
Пример #9
0
def predicaoCluster(matricula, curso, tipo_predicao):

    dados = retornarDadosCurso(curso)
    # selecionando as caracteristicas do aluno
    aluno = dados.loc[dados['MATRICULA'] == matricula][:]
    aluno.drop('MATRICULA', axis=1, inplace=True)
    aluno.drop('APROVADO', axis=1, inplace=True)
    aluno.drop('COD_DISCIPLINA', axis=1, inplace=True)
    aluno.drop('SIT_MATRICULA', axis=1, inplace=True)
    aluno = aluno.head(1)

    aluno.to_csv('aluno_temp.csv', index=False)

    from weka.clusterers import Clusterer
    import weka.core.jvm as jvm
    from weka.core.converters import Loader
    import weka.core.serialization as serialization

    jvm.start()

    if curso == 'si':
        if tipo_predicao == 'reprovacao':
            model = serialization.read_all("model/kmeans_si_reprovacao.model")
        elif tipo_predicao == 'evasao':
            model = serialization.read_all("model/kmeans_si_evasao.model")
    elif curso == 'eca':
        if tipo_predicao == 'reprovacao':
            model = serialization.read_all("model/kmeans_eca_reprovacao.model")
        elif tipo_predicao == 'evasao':
            model = serialization.read_all("model/kmeans_eca_evasao.model")
    cluster = Clusterer(jobject=model[0])

    loader = Loader(classname="weka.core.converters.CSVLoader")
    dado_aluno = loader.load_file("aluno_temp.csv")
    for aluno in dado_aluno:
        cluster_aluno_pertence = cluster.cluster_instance(aluno)

    #jvm.stop()

    caracteristica = retornarCaracteristicaCluster(curso, tipo_predicao,
                                                   cluster_aluno_pertence)

    return caracteristica
Пример #10
0
 def load_trained_model(self, t_mod):
     # using the serialization library for
     # opening the model
     objects = serialization.read_all(t_mod)
     # and after the using the classifier
     # for actually learn the classifier
     classifier = Classifier(jobject=objects[0])
     print "Model Classified"
     print classifier
     # returning both classifier and objects
     return objects, classifier
Пример #11
0
    def weka_predict(self):

        # grab WEKA model
        objects = serialization.read_all(self.weka_model)
        classifier = Classifier(jobject=objects[0])

        # load the dataset i.e. the .arff file generated for the supplied url
        loader = Loader(classname="weka.core.converters.ArffLoader")
        data = loader.load_file(self.dataset)
        data.class_is_last()

        # for each url tested predict whether "Phishy" or not by using the Random Tree model
        for item in data:
            self.prediction = classifier.classify_instance(item)
    def __init__(self, number, name=None):
        """
        Minimax bot which uses a simple feed-forward neural net to score board states

        :param number:  Board.X for player1 or Board.O for player2
        :param name: A descriptive name for the Bot
        """
        if name is None:
            name = "Continuous Neural Net Bot"
        MinimaxBot.__init__(self, number, name=name)
        self.player_type = 'continuous-nn minimax'

        objects = serialization.read_all("models/game/bots/weka_models/mlp-tuned-continuous.model")
        self.classifier = Classifier(jobject=objects[0])
Пример #13
0
def predict(attributes):
    jvm.start()
    file_path = print_to_file(attributes)
    # load the saved model
    objects = serialization.read_all("/Users/hosyvietanh/Desktop/data_mining/trained_model.model")
    classifier = Classifier(jobject=objects[0])
    loader = Loader(classname="weka.core.converters.ArffLoader")
    data = loader.load_file(file_path)
    data.class_is_last()
    for index, inst in enumerate(data):
        pred = classifier.classify_instance(inst)
        dist = classifier.distribution_for_instance(inst)
        return int(pred)
    jvm.stop()
    def __init__(self, number, name=None):
        """
        Minimax bot which uses a Model Tree to score board states

        :param number:  Board.X for player1 or Board.O for player2
        :param name: A descriptive name for the Bot
        """
        if name is None:
            name = "Model Tree Bot"
        MinimaxBot.__init__(self, number, name=name)
        self.player_type = 'modeltree minimax'

        objects = serialization.read_all(
            "models/game/bots/weka_models/model-tree.model")
        self.classifier = Classifier(jobject=objects[0])
Пример #15
0
    def __init__(self, number, name=None):
        """
        Minimax bot which uses a nominal neural network to score board states
        The neural net outputs one of ten classes.  The higher the class number, the better the board state for X

        :param number:  Board.X for player1 or Board.O for player2
        :param name: A descriptive name for the Bot
        """
        if name is None:
            name = "Nominal NeuralNet Bot"
        MinimaxBot.__init__(self, number, name=name)
        self.player_type = 'nominal-neuralnet minimax'

        objects = serialization.read_all("models/game/bots/weka_models/mlp-tuned-categorical.model")
        self.classifier = Classifier(jobject=objects[0])
Пример #16
0
    def deserialize(cls, ser_file):
        """
        Deserializes a filter from a file.

        :param ser_file: the file to deserialize from
        :type ser_file: str
        :return: model
        :rtype: Filter
        """

        objs = serialization.read_all(ser_file)
        if len(objs) == 1:
            return Filter(jobject=objs[0])
        else:
            raise Exception(
                "Excepted one object in the model file (%s), but encountered: %d" % (ser_file, len(objs)))
    def __init__(self, number, name=None):
        """
        Minimax bot which uses a decision tree to score board states
        The decision tree outputs one of ten classes.  The higher the class number, the better the board state for X

        :param number:  Board.X for player1 or Board.O for player2
        :param name: A descriptive name for the Bot
        """
        if name is None:
            name = "DTree Bot"
        MinimaxBot.__init__(self, number, name=name)
        self.player_type = 'dtree minimax'

        objects = serialization.read_all(
            "models/game/bots/weka_models/j48_default.model")
        self.classifier = Classifier(jobject=objects[0])
    def functionProcessamento(self, ca1_r, ca1_l, ca2_ca3_r, ca2_ca3_l, sub_r,
                              sub_l, sexo, id):
        jvm.start()
        path = os.path.dirname(os.path.abspath(__file__))
        # TODO: verificar qual o sexo do individuo para carregar o modelo corretamente
        modelo = path + "\\naive_bayes_feminino_novo.model"
        if (sexo == "Male"):
            print("É masculino")
            modelo = path + "\\naive_bayes_feminino_novo.model"
        objects = serialization.read_all(modelo)
        classifier = Classifier(jobject=objects[0])
        loader = Loader(classname="weka.core.converters.ArffLoader")
        arquivo = open(path + "\\novo_individuo.arff", "w")
        conteudo = list()
        conteudo.append("@relation alzheimer \n\n")
        conteudo.append("@attribute doente {SIM, NAO} \n")
        conteudo.append("@attribute ca1_right real \n")
        conteudo.append("@attribute ca1_left real \n")
        conteudo.append("@attribute ca2_ca3_right real\n")
        conteudo.append("@attribute ca2_ca3_left real \n")
        conteudo.append("@attribute subic_right real \n")
        conteudo.append("@attribute subic_left real \n\n")
        conteudo.append("@data \n")
        #aqui passar as variáveis
        conteudo.append("SIM," + str(ca1_r) + "," + str(ca1_l) + "," +
                        str(ca2_ca3_r) + "," + str(ca2_ca3_l) + "," +
                        str(sub_r) + "," + str(sub_l))
        print(conteudo)
        arquivo.writelines(conteudo)
        arquivo.close()

        data = loader.load_file(path + "\\novo_individuo.arff")
        data.class_is_last()
        for index, inst in enumerate(data):
            pred = classifier.classify_instance(inst)
            dist = classifier.distribution_for_instance(inst)
            pc_doenca = round(((pred) * 100), 2)
            pc_saudavel = round(((100 - pc_doenca)), 2)
            print(" Porcentagem de alzheimer=" + str(pc_doenca) +
                  "%, porcentagem saudavel=" + str(pc_saudavel) + "%")
            alzheimer = Alzheimer.objects.get(id=id)
            alzheimer.resultado_ad = pc_doenca
            alzheimer.resultado_cn = pc_saudavel
            alzheimer.status_seg = 2
            alzheimer.save()
        jvm.stop()
def index():
    if request.method == "GET":
        return render_template('bot.html')
    if request.method == "POST":
        # jvm.stop()
        jvm.start()
        f = open("instances.arff", "a")
        args = request.form.to_dict()
        weight_lb = float(args['weight']) * 2.20462
        bmi = (weight_lb / pow(float(args['height']), 2)) * 703
        hypertensive_status = args['hypertensive_status']
        heart_disease_status = args['heart_disease_status']
        if heart_disease_status == "Yes":
            heart_disease_status = '1'
        else:
            heart_disease_status = '0'
        if hypertensive_status == "Yes":
            hypertensive_status = '1'
        else:
            hypertensive_status = '0'

        st = "\n"+args['gender']+","+args['age']+","+hypertensive_status+","+heart_disease_status+","+args['marrital_status'] + \
            ","+args['work_type']+","+args['residence']+"," + \
            args['hypertension']+","+str(bmi)+",'"+args['smoking_status'].lower()+"',?"
        print(st)
        f.write(st)
        f.close()
        objects = serialization.read_all("J48.model")
        loader = Loader(classname="weka.core.converters.ArffLoader")
        csr = Classifier(jobject=objects[0])
        output_results = PredictionOutput(
            classname="weka.classifiers.evaluation.output.prediction.CSV")
        data1 = loader.load_file("instances.arff")
        data1.class_is_last()
        ev2 = Evaluation(data1)
        ev2.test_model(csr, data1, output_results)

        TESTDATA = StringIO("Instance,Actual,Predicted," +
                            output_results.buffer_content())
        df = pd.read_csv(TESTDATA)
        prediction = list(df.Predicted).pop().split(":")[1]
        print(prediction)
        # jvm.stop()
        response = {"status": "200", "prediction": prediction}
        return Response(json.dumps(response, indent=2),
                        mimetype="application/json")
Пример #20
0
    def deserialize(cls, ser_file):
        """
        Deserializes a clusterer from a file.

        :param ser_file: the model file to deserialize
        :type ser_file: str
        :return: model and, if available, the dataset header
        :rtype: tuple
        """

        objs = serialization.read_all(ser_file)
        if len(objs) == 1:
            return Clusterer(jobject=objs[0]), None
        elif len(objs) == 2:
            return Clusterer(jobject=objs[0]), Instances(jobject=objs[1])
        else:
            raise Exception(
                "Excepted one or two objects in the model file (%s), but encountered: %d"
                % (ser_file, len(objs)))
Пример #21
0
	def predict(self, modelName, x, arffName, debug=False):
		# Carga el arrf para conocer la estructura de las instancias
		loader = Loader(classname="weka.core.converters.ArffLoader")
		data = loader.load_file(arffName)


		# Se asume que la clase es el ultimo atributo
		data.class_is_last()

		# Carga del modelo generado en Weka
		objects = serialization.read_all(modelName)
		cls = Classifier(jobject=objects[0])
		if(debug):
			print("Loaded model...")
			print(cls)

		# Se crea la instancia correspondiente a la entrada y se clasifica
		if(debug): print("Input", x)

		# Anyade un valor tonto para la clase de la instancia
		if data.class_attribute.is_nominal:
			x.append('a')
		else:
			x.append(0)

		# Convierte los valores nominales a la posicion entera que ocupa dentro de sus lista
		#print data.num_attributes
		for i in range(0, data.num_attributes):
			attribute = data.attribute(i)
			if attribute.is_nominal:
				x[i] = attribute.index_of(x[i])
			'''print x[i]
		print '''''
		# Realiza la prediccion
		inst = Instance.create_instance(x)
		inst.dataset = data
		pred = cls.classify_instance(inst)
		if data.class_attribute.is_nominal:
			pred =  data.class_attribute.value(pred)
		if(debug): print("Prediction", pred)

		return pred
Пример #22
0
    def test_read_write_all(self):
        """
        Tests methods read_all and write_all.
        """
        fname = self.tempfile("readwrite.ser")
        self.delfile(fname)

        lin = []
        for i in range(4):
            lin.append(javabridge.make_instance("java/lang/Integer", "(I)V", i))
        serialization.write_all(fname, lin)
        self.assertTrue(os.path.exists(fname), msg="Failed to write to " + fname + "?")

        lout = serialization.read_all(fname)
        self.assertIsNotNone(lout, msg="Failed to read from " + fname + "?")
        self.delfile(fname)
        self.assertEqual(len(lin), len(lout), msg="Number of elements differ")
        for i in range(len(lin)):
            iin = javabridge.call(lin[i], "intValue", "()I")
            iout = javabridge.call(lout[i], "intValue", "()I")
            self.assertEqual(iin, iout, msg="Input/output differ at #" + str(i))
from iotdata import IOTData
from ShimmerBluetooth import ShimmerBluetooth

import weka.core.serialization as serialization
from weka.classifiers import Classifier
import weka.core.jvm as jvm
from weka.core.converters import Loader
from weka.classifiers import Evaluation
from weka.core.classes import Random

# devices = ["Ground_Truth_Treadmill1", "Ground_Truth_Treadmill2", "Ground_Truth_Treadmill3", "Ground_Truth_Treadmill5"]
# device = IOTData(devices, 'Ground_Truth_data\\03_27_16\\')
# devices = IOTData(None, comport = "COM5")

jvm.start()
objects = serialization.read_all("test.model")
classifier = Classifier(jobject=objects[0])


# sensors = [ShimmerBluetooth("COM9", 256)]
dataManager = IOTData(None, comport = 'COM30')
predictions = [0]*len(sensors)
stepCount = 0

try:
    while True:
        for i in range(len(sensors)):

            # aquire data
            # df = sensors[i].getFrame()
            df = dataManager.getNextWindow()
Пример #24
0
def main():

    workbook = xlsxwriter.Workbook('../data/demo.xlsx')
    worksheet = workbook.add_worksheet()
    # Widen the first column to make the text clearer.
    worksheet.set_column('A:A', 20)
    worksheet.set_column('B:B', 20)
    worksheet.set_column('C:C', 20)
    worksheet.set_column('D:D', 20)
    worksheet.set_column('E:E', 20)
    worksheet.set_column('F:F', 20)

    bold = workbook.add_format({'bold': True})

    worksheet.write('A1', 'Source IP', bold)
    worksheet.write('B1', 'TCP/UDP', bold)
    worksheet.write('C1', 'Service Port', bold)
    worksheet.write('D1', 'Description of Service', bold)
    worksheet.write('E1', 'Destination IP', bold)
    worksheet.write('F1', 'Action', bold)

    # load a dataset
    loader = Loader(classname="weka.core.converters.ArffLoader")

    test_data = loader.load_file("../Logs/arff/logs_training_test.arff")
    test_data.class_is_last()  # set class attribute
    #
    # classifier2 = Classifier(classname="weka.classifiers.trees.J48", options=["-C", "0.3"])
    # classifier2.build_classifier(test_data)

    #    # evaluate model on train/test split
    objects = serialization.read_all("out.model")
    classifier2 = Classifier(jobject=objects[0])
    evaluation = Evaluation(test_data)
    evl = evaluation.test_model(classifier2, test_data)
    # print(evl)
    # evaluation.evaluate_train_test_split(classifier2, test_data, 66.0, Random(1))

    # print(evaluation.predictions)
    # evaluation.evaluate_train_test_split(classifier2, test_data, 66.0, Random(1))
    #
    # print(list(enumerate(test_data)))
    i = 0
    for index, inst in enumerate(test_data):
        pred = classifier2.classify_instance(inst)
        dist = classifier2.distribution_for_instance(inst)
        data_line = str(inst).split(",")
        src_host = data_line[0]
        dst_host = data_line[1]
        dst_port = data_line[3]
        protocol = data_line[4]
        # print(int(pred))
        if int(pred) == 1:
            action = "permitted"
            # print(action)
            # print(src_host)
            worksheet.write(i + 1, 0, src_host)
            worksheet.write(i + 1, 1, protocol)
            worksheet.write(i + 1, 2, dst_port)
            worksheet.write(i + 1, 4, dst_host)
            worksheet.write(i + 1, 5, action)
            i = i + 1

        # print(str(inst).split(","))
        # print(len(inst))

        # print(str(pred))
        # print(str(index+1) + ": label index=" + str(pred) + ", class distribution=" + str(dist))

    print(evaluation.summary())
    workbook.close()
__author__ = 'umanoidTyphoon'
from build_weka_database import build
from classifier_model_analyzer import classify_dump
from weka.classifiers import Classifier

import util
import weka.core.jvm as jvm
import weka.core.serialization as serialization

# WINDOW_WIDTH = 10000
WINDOW_WIDTH = 25

jvm.start()

deserialized_objects = serialization.read_all("default.model")
classifier = Classifier(jobject=deserialized_objects[0])

print classifier

jvm.stop()

# build(WINDOW_WIDTH)
#
# conn = util.connect_to_db()
#
# cursor = conn.cursor()
#
# cursor.execute("""SELECT max(dump_id) from pe_dumps""")
# lastID = cursor.fetchone()[0]
#
# currID = lastID - WINDOW_WIDTH
Пример #26
0
	def loadClassifier(self, filename, path='/home/sbiastoch/Schreibtisch/classifiers/'):
		objects = serialization.read_all(path+filename)
		self.classifier = Classifier(jobject=objects[0])
Пример #27
0
warnings.simplefilter(action='ignore', category=FutureWarning)

from weka.core.converters import Loader
l = Loader("weka.core.converters.ArffLoader")
# d = l.load_file("C:\Users\Paolo\Documents\school stuff\4th Year\for Implementation\dataset_Updated.arff")
d = l.load_file("C:\Program Files\Weka-3-8\data\dataset_new.arff")
#print(d)

# In[2]:

import weka.core.jvm as jvm
import weka.core.serialization as serialization
from weka.classifiers import Classifier
data = d
data.class_is_last()
objects = serialization.read_all("NBTree_trained.model")
classifier = Classifier(jobject=objects[0])

# In[3]:

#classifier.classify_instance(inst=data.get_instance(index=4))

# In[4]:

from weka.core import dataset
from weka.core.dataset import Instance

# In[5]:

age, gender, mar_stat, ocd_hist, q2, q5, q10, q12, q13, q15, q17 = input(
    "Input list here : ").split(" ")
Пример #28
0
    def getInstance(self, gameState):

        headers = ""
        headers = headers + "@relation prueba\n\n"

        headers = headers + "@attribute score5 NUMERIC\n"
        headers = headers + "@attribute score2 NUMERIC\n"
        headers = headers + "@attribute score NUMERIC\n"

        headers = headers + "@attribute ghost1-living {True, False}\n"
        headers = headers + "@attribute ghost2-living {True, False}\n"
        headers = headers + "@attribute ghost3-living {True, False}\n"
        headers = headers + "@attribute ghost4-living {True, False}\n"

        headers = headers + "@attribute distance-ghost1 NUMERIC \n"
        headers = headers + "@attribute distance-ghost2 NUMERIC \n"
        headers = headers + "@attribute distance-ghost3 NUMERIC \n"
        headers = headers + "@attribute distance-ghost4 NUMERIC \n"

        headers = headers + "@attribute posX NUMERIC\n"
        headers = headers + "@attribute posY NUMERIC\n"

        headers = headers + "@attribute direction {North, South, East, West, Stop}\n"

        headers = headers + "@attribute wall-east {True, False}\n"
        headers = headers + "@attribute wall-south {True, False}\n"
        headers = headers + "@attribute wall-west {True, False}\n"
        headers = headers + "@attribute wall-north {True, False}\n"

        headers = headers + "@attribute move {North, South, East, West, Stop}\n\n"

        headers = headers + "@data\n\n\n"

        objects = serialization.read_all("data/out.model")
        cls = [
            classifiers.Classifier("weka.classifiers.trees.REPTree"),
            classifiers.Classifier(
                "weka.classifiers.functions.LinearRegression"),
            classifiers.Classifier("weka.classifiers.functions.SMOreg"),
        ]
        cls = Classifier()
        file = open('data/instances.arff', 'w+')
        file.write(headers)

        line = ""
        for i in gameState.livingGhosts[
                1:]:  #discard the first value, as it is PacMan
            line = line + str(i) + ","

        for i in gameState.data.ghostDistances:
            if i is None:
                line = line + "0" + ","
            else:
                line = line + str(i) + ","


        line = line + str(gameState.data.agentStates[0].getPosition()[0]) + "," +\
        str(gameState.data.agentStates[0].getPosition()[1])+ "," +\
        str(gameState.data.agentStates[0].getDirection()) + "," +\
        str(gameState.hasWall(gameState.getPacmanPosition()[0] - 1, gameState.getPacmanPosition()[1])) + "," +\
        str(gameState.hasWall(gameState.getPacmanPosition()[0], gameState.getPacmanPosition()[1] - 1)) + "," +\
        str(gameState.hasWall(gameState.getPacmanPosition()[0] + 1, gameState.getPacmanPosition()[1])) + "," +\
        str(gameState.hasWall(gameState.getPacmanPosition()[0], gameState.getPacmanPosition()[1] + 1)) + ",?"

        line = str(int(BustersAgent.getScore5(self, gameState))) + ","+\
        str(int(BustersAgent.getScore2(self, gameState))) + "," +\
        str(gameState.data.score) + "," + line

        file.write(line)
        file.close()

        loader = Loader(classname="weka.core.converters.ArffLoader")
        data = loader.load_file("data/instances.arff")
        data.class_is_last()  # set class attribute
        for index, inst in enumerate(data):
            pred = cls.classify_instance(inst)

        return pred
from weka.classifiers import PredictionOutput
from weka.classifiers import Evaluation
from weka.core.classes import Random
import pandas as pd


# In[2]:


loader = Loader(classname="weka.core.converters.ArffLoader")

data_dir = "D:\\Rahul\\HealthCare ChatBot\\healthcare-dataset-stroke-data\\"



objects = serialization.read_all("J48.model")
csr = Classifier(jobject=objects[0])
#print(classifier)


# In[3]:


f= open("instances.arff","a")

#enter attributes

st= "\nFemale,54,1,0,Yes,Private,Urban,72.93,35.7,'never smoked',?"
f.write(st)
f.close()
Пример #30
0
def python_wrapper(mImage, prefix, file_name, pre_prefix, dir, permanent_dir,
                   model):
    # Initialization of weka machine learning library
    weka_machine_learning = WML.WekaMachineLearning()
    # tokenization of images
    token = re.split('RGB_|.png', mImage)
    ir_directory = token[0] + 'IR_' + token[1] + '.pgm'
    mat_directory = token[0] + 'Mat_' + token[1]

    # get mat and ir image
    image = segmentor.getImage(ir_directory)
    mat = segmentor.readMatFile(mat_directory)

    # image processing
    edges = segmentor.edgeDetector(image)
    type = segmentor.getTypeOfFruit(image)
    segmentation = segmentor.segmentation(image, type)
    filter = segmentor.filterImageFromSegmentation(image, segmentation)
    output_seg = segmentor.imageMapping(filter, mat['IR'])

    ####################-Anomaly Detection via INFLO-###################

    # file prefix creation for the csv file to save
    prefix_csv = prefix + "\\" + file_name

    # if folder is not there then create it
    # and right the csv to the folder
    if not os.path.exists(prefix):
        os.mkdir(prefix)
        csv = segmentor.writeToCSV(output_seg, prefix_csv)
        print "file is written"

    #else simply write the csv to the folder
    else:
        csv = segmentor.writeToCSV(output_seg, prefix_csv)
        print "file is written"
    #call the INFLO.bat after segmenting the image
    #for anomaly detection
    run_batch_file("rapid_miner_pro_ifruitlfy.bat")
    ############################-Clustering-############################

    # image file directory is stored in ir_directory
    # mat file directory is stored in mat_directory
    # and need to get the INFLO file
    # directory for INFLO file is prefix_csv
    anomaly_file = prefix_csv + '.csv_INFLO.csv'
    # directory for the temperorary files is made so
    # some results can be stored and processed auto-
    # matically by the rapid miner 5, this folder is

    demo_printing_picture(permanent_dir, prefix, mImage, pre_prefix, dir,
                          file_name)
    print(
        "END OF ANOMALY DETECTION CLICK TRAIN AND SHOW RESULT FOR PROCESSING")
    write_temp_dir = permanent_dir + "\\"
    print prefix
    print file_name
    # Clean the junk of the output files
    if os.path.exists(permanent_dir + "//output.csv"):
        os.remove(permanent_dir + "//output.csv")
    features = iFruitFly_clustering.cluster_analysis.cluster_analysis(
        ir_directory, permanent_dir + "\\output_INFLO.csv", mat_directory,
        dir + "\\" + file_name, prefix, file_name, permanent_dir)
    if (features == None):
        print("Image cant be segmented due to poor calibiration")

    #other files are stored for the user in the junk
    else:
        print "printing images->>>>>>> ", prefix + file_name
        image_plotter(features, ir_directory, prefix + file_name)
    import csv
    # Weka Machine Learning Inclusion on 5/30/2017
    # adding one extra column
    with open(permanent_dir + "\\output.csv", 'r') as csvinput:
        with open(permanent_dir + "\\output_n.csv", 'w') as csvoutput:
            writer = csv.writer(csvoutput, lineterminator='\n')
            reader = csv.reader(csvinput)
            all = []
            row = next(reader)
            row.append('result')
            all.append(row)
            for row in reader:
                row.append(0)
                all.append(row)
            writer.writerows(all)
    #model = "J:\iFruitFly\Python Scripts\Model 1\\model.model"
    data_dir = permanent_dir + "\\output_n.csv"
    #data_dir_open = open(data_dir)
    #r = csv.reader(data_dir_open)

    jvm.start()
    loader = Loader(classname="weka.core.converters.CSVLoader")
    data = loader.load_file(data_dir)
    # using the serialization library for
    # opening the model
    objects = serialization.read_all(model)
    classifier = Classifier(jobject=objects[0])
    print "Model Classified"
    print classifier
    data.class_is_last()
    for index, inst in enumerate(data):
        pred = classifier.classify_instance(inst)
        dist = classifier.distribution_for_instance(inst)
        print pred
def predictionFromModel():
    import weka.core.serialization as serialization
    from weka.classifiers import Classifier
    from weka.classifiers import Evaluation

    predictionsPath = outputPrediction
    models_dir = inputModel
    modelsList = os.listdir(inputModel)
    data_dir = input
    folderList = os.listdir(inputModel)
    i = 0
    loader = Loader(classname="weka.core.converters.ArffLoader")
    from weka.core.classes import Random
    from weka.core.dataset import Instances

    data = loader.load_file(os.path.join(inputModel, "genderTest.arff"))
    data.class_is_last()
    modelName = "GenderModel.model"
    objects = serialization.read_all(os.path.join(inputModel, modelName))
    trainedModel = Classifier(jobject=objects[0])
    genderFile = open(os.path.join(outputPrediction, 'Gender_Predictions.csv'),
                      'w')
    with genderFile:
        j = -1
        fieldnames = ['Test_Author_Profile_Id', 'Gender']
        writer = csv.DictWriter(genderFile, fieldnames=fieldnames)
        writer.writeheader()
        for index, inst in enumerate(data):
            j = j + 1
            pred = trainedModel.classify_instance(inst)
            dist = trainedModel.distribution_for_instance(inst)
            print(
                str(index + 1) + ": label index=" + str(pred) +
                ", class distribution=" + str(dist))
            if (str(pred) == '0.0'):
                writer.writerow({
                    'Test_Author_Profile_Id': my_list[j],
                    'Gender': 'male'
                })
            if (str(pred) == '1.0'):
                writer.writerow({
                    'Test_Author_Profile_Id': my_list[j],
                    'Gender': 'female'
                })

    data = loader.load_file(os.path.join(inputModel, "ageTest.arff"))
    data.class_is_last()
    modelName = "AgeModel.model"
    objects = serialization.read_all(os.path.join(inputModel, modelName))
    trainedModel = Classifier(jobject=objects[0])
    ageFile = open(os.path.join(outputPrediction, 'Age_Predictions.csv'), 'w')

    with ageFile:
        j = -1
        fieldnames = ['Test_Author_Profile_Id', 'Age']
        writer = csv.DictWriter(ageFile, fieldnames=fieldnames)
        writer.writeheader()
        for index, inst in enumerate(data):
            j = j + 1
            pred = trainedModel.classify_instance(inst)
            dist = trainedModel.distribution_for_instance(inst)
            print(
                str(index + 1) + ": label index=" + str(pred) +
                ", class distribution=" + str(dist))
            if (str(pred) == '0.0'):
                writer.writerow({
                    'Test_Author_Profile_Id': my_list[j],
                    'Age': '15-19'
                })
            if (str(pred) == '1.0'):
                writer.writerow({
                    'Test_Author_Profile_Id': my_list[j],
                    'Age': '20-24'
                })
            if (str(pred) == '2.0'):
                writer.writerow({
                    'Test_Author_Profile_Id': my_list[j],
                    'Age': '25-xx'
                })
    os._exit(0)