예제 #1
0
    def fit(self, X_train, Y_train):
        self.X_train = X_train
        self.Y_train = Y_train
        self.dim = (len(self.X_train[0]) + 1, len(list(set(self.Y_train))))
        self.w = np.ones(self.dim[0] * self.dim[1])
        options = {"c1": 0.5, "c2": 0.1, "w": 0.9}

        X_net, X_opt, Y_net, Y_opt = train_test_split(X_train,
                                                      Y_train,
                                                      test_size=0.20,
                                                      stratify=Y_train)

        G = nBuilding.quipusBuildKnn(X_net,
                                     Y_net,
                                     self.knn,
                                     self.ePercentile,
                                     labels=True,
                                     inside=True)

        probabilities = []

        for index, instance in enumerate(X_opt):
            nBuilding.quipusInsert(G, instance, inside=True)
            tmpResults = predict.quipusPrediction(G, self.bnn, self.alpha)
            probabilities.append(tmpResults)
        initial = np.zeros(self.dim[0])
        initial[0] = 1
        initPos = initial
        for i in range(self.dim[1] - 1):
            initPos = np.append(initPos, initial)
        min_bound = -np.ones(self.dim[0] * self.dim[1])
        max_bound = np.ones(self.dim[0] * self.dim[1])
        bounds = (min_bound, max_bound)
        nParticles = 10
        initPosParticles = []
        for _ in range(nParticles):
            initPosParticles.append(initPos)
        initPosParticles = np.array(initPosParticles)
        optimizer = ps.single.GlobalBestPSO(
            n_particles=nParticles,
            dimensions=self.dim[0] * self.dim[1],
            options=options,
            bounds=bounds,
            init_pos=initPosParticles,
        )
        cost, pos = optimizer.optimize(
            optimization,
            iters=500,
            n_processes=None,
            probabilidades=probabilities,
            Y=Y_opt,
            dim=self.dim,
        )
        self.w = np.reshape(pos, (self.dim))
        self.graphs = nBuilding.quipusBuildKnn(X_train,
                                               Y_train,
                                               self.knn,
                                               self.ePercentile,
                                               labels=True,
                                               inside=True)
예제 #2
0
    def predict(self, X_test, Y_test=[]):
        result = []
        results = []
        self.classes = self.graphs[0].graph["classNames"]
        for index, instance in enumerate(X_test):
            # indexNode = self.graphs[0].graph["lnNet"] + index
            if len(Y_test) == 0:
                nBuilding.quipusInsert(self.graphs,
                                       instance,
                                       inside=True,
                                       accepted=self.accepted)
            else:
                nBuilding.quipusInsert(
                    self.graphs,
                    instance,
                    Y_test[index],
                    inside=True,
                    accepted=self.accepted,
                )
            # draw.drawGraph(g,"New Dark Node Inserted")
            tmpResults = predict.quipusPrediction(self.graphs,
                                                  self.bnn,
                                                  self.alpha,
                                                  accepted=self.accepted)
            test = np.transpose(tmpResults) * self.w

            test = [np.sum(e) for e in test]
            test2 = test / np.sum(test)
            results.append(test2)
            result.append(list(set(self.Y_train))[np.argmax(test2)])

        if len(Y_test) != 0:
            print("RESULT:", np.array(result))
            print("Y_TEST:", np.array(Y_test))
            acc = 0
            err = []
            err.append(self.graphs[0].graph["classNames"])
            for index, element in enumerate(result):
                if element == Y_test[index]:
                    acc += 1
                else:
                    err.append([
                        element,
                        Y_test[index],
                        results[index][self.classes.index(element)],
                        results[index][self.classes.index(Y_test[index])],
                    ])
            acc /= len(X_test)

            print("ERRORS: ", err)
            print("Accuracy ", round(acc, 4), "%")
        # print(test2)
        # print(tmpResults)
        return result
예제 #3
0
    def fit(self, X_train, Y_train):
        self.X_train = X_train
        self.Y_train = Y_train
        self.dim = (len(self.X_train[0]), len(list(set(self.Y_train))))
        self.w = np.ones(self.dim[0] * self.dim[1])
        G = nBuilding.quipusBuildKnn(X_train,
                                     Y_train,
                                     self.knn,
                                     self.ePercentile,
                                     labels=True)
        options = {"c1": 0.5, "c2": 0.3, "w": 0.9}
        probabilities = []

        for index, instance in enumerate(X_train):
            # indexNode = self.graphs[0].graph["lnNet"] + index
            if len(Y_train) == 0:
                nBuilding.quipusInsert(G, instance)
            else:
                nBuilding.quipusInsert(G, instance, Y_train[index])
            # draw.drawGraph(g,"New Dark Node Inserted")
            tmpResults = predict.quipusPrediction(G, self.bnn, self.alpha)
            probabilities.append(tmpResults)
            # test = tmpResults * self.w
            # test = np.transpose(test)
            # test = [np.sum(e) for e in test]
            # test2 = test / np.sum(test)
            # print(test2)
            # print(tmpResults)
        # print(probabilities)
        max_bound = np.ones(self.dim[0] * self.dim[1]) * self.dim[1]
        # min_bound = np.zeros(self.dim[0] * self.dim[1])
        min_bound = -max_bound
        bounds = (min_bound, max_bound)
        optimizer = ps.single.GlobalBestPSO(
            n_particles=50,
            dimensions=self.dim[0] * self.dim[1],
            options=options,
            bounds=bounds,
        )
        cost, pos = optimizer.optimize(
            optimizacion,
            iters=20,
            probabilidades=probabilities,
            Y=Y_train,
            dim=self.dim,
        )
        self.w = np.reshape(pos, (self.dim))
        self.graphs = G
예제 #4
0
    def fit(self, X_train, Y_train):
        self.X_train = X_train
        self.Y_train = Y_train
        self.dim = (len(self.X_train[0]) + 1, len(list(set(self.Y_train))))
        self.w = np.ones(self.dim[0] * self.dim[1])
        options = {"c1": 0.5, "c2": 0.1, "w": 0.9}

        X_net, X_opt, Y_net, Y_opt = train_test_split(X_train,
                                                      Y_train,
                                                      test_size=0.20,
                                                      stratify=Y_train)

        G = nBuilding.quipusBuildKnn(X_net,
                                     Y_net,
                                     self.knn,
                                     self.ePercentile,
                                     labels=False,
                                     inside=True)
        minQ = G[0].graph["mod"] - 0.1
        accepted = []
        nDimAccepted = 0
        for g in G:
            # print(g.graph["mod"])
            if g.graph["mod"] >= minQ:
                accepted.append(True)
                nDimAccepted += 1
            else:
                accepted.append(False)

        accepted.pop(0)
        probabilities = []

        for index, instance in enumerate(X_opt):
            nBuilding.quipusInsert(G, instance, inside=True, accepted=accepted)
            tmpResults = predict.quipusPrediction(G,
                                                  self.bnn,
                                                  self.alpha,
                                                  accepted=accepted)
            probabilities.append(tmpResults)
        # initial = np.zeros(self.dim[0])
        # initial[0] = 1
        # initPos = initial

        # for i in range(self.dim[1] - 1):
        #     initPos = np.append(initPos, initial)
        probabilities = np.array(probabilities)
        self.dim = probabilities.shape[1]
        self.w = np.ones(self.dim)
        if (not self.dim == 1):
            min_bound = np.zeros(self.dim)
            max_bound = np.ones(self.dim)

            bounds = (min_bound, max_bound)
            nParticles = 10

            # initPosParticles = []
            # for _ in range(nParticles):
            #     initPosParticles.append(initPos)
            # initPosParticles = np.array(initPosParticles)

            optimizer = ps.single.GlobalBestPSO(
                n_particles=nParticles,
                dimensions=self.dim,
                options=options,
                bounds=bounds,
            )
            cost, pos = optimizer.optimize(
                optimizationQ3,
                iters=500,
                n_processes=None,
                probabilidades=probabilities,
                Y=Y_opt,
                dim=self.dim,
            )
            self.w = np.reshape(pos, (self.dim))
        self.accepted = accepted
        self.graphs = nBuilding.quipusBuildKnn(X_train,
                                               Y_train,
                                               self.knn,
                                               self.ePercentile,
                                               labels=True,
                                               inside=True)