def overlapResolve(self):
        """
        Resolve overlapping hyperboxes with bounders contained in self.V and self.W
        """
        yX = self.V.shape[0]
        # Contraction process does not cause overlappling regions => No need to check from the first hyperbox for each hyperbox
        for i in np.arange(yX - 1):
            j = i + 1
            while j < yX:
                if self.classId[i] != self.classId[j]:
                    caseDim = hyperboxOverlapTest(self.V, self.W, i, j)
                    if len(caseDim) > 0:
                        self.V, self.W = hyperboxContraction(
                            self.V, self.W, caseDim, j, i)

                j = j + 1

        return (self.V, self.W)
    def fit(self, X_l, X_u, patClassId):
        """
        Training the classifier

         Xl             Input data lower bounds (rows = objects, columns = features)
         Xu             Input data upper bounds (rows = objects, columns = features)
         patClassId     Input data class labels (crisp). patClassId[i] = 0 corresponds to an unlabeled item

        """
        print('--Online Learning--')

        if self.isNorm == True:
            X_l, X_u = self.dataPreprocessing(X_l, X_u)
        #X_l = X_l.astype(np.float32)
        #X_u = X_u.astype(np.float32)
        time_start = time.perf_counter()

        yX, xX = X_l.shape
        teta = self.teta

        mark = np.array([
            '*', 'o', 'x', '+', '.', ',', 'v', '^', '<', '>', '1', '2', '3',
            '4', '8', 's', 'p', 'P', 'h', 'H', 'X', 'D', '|', '_'
        ])
        mark_col = np.array(['r', 'g', 'b', 'y', 'c', 'm', 'k'])

        listLines = list()
        listInputSamplePoints = list()

        if self.isDraw:
            drawing_canvas = self.initializeCanvasGraph(
                "GFMM - Online learning", xX)

            if self.V.size > 0:
                # draw existed hyperboxes
                color_ = np.array(['k'] * len(self.classId), dtype=object)
                for c in range(len(self.classId)):
                    if self.classId[c] < len(mark_col):
                        color_[c] = mark_col[self.classId[c]]

                hyperboxes = drawbox(self.V[:, 0:np.minimum(xX, 3)],
                                     self.W[:, 0:np.minimum(xX, 3)],
                                     drawing_canvas, color_)
                listLines.extend(hyperboxes)
                self.delay()

        self.misclass = 1

        while self.misclass > 0 and teta >= self.tMin:
            # for each input sample
            for i in range(yX):
                classOfX = patClassId[i]
                # draw input samples
                if self.isDraw:
                    if i == 0 and len(listInputSamplePoints) > 0:
                        # reset input point drawing
                        for point in listInputSamplePoints:
                            point.remove()
                        listInputSamplePoints.clear()

                    color_ = 'k'
                    if classOfX < len(mark_col):
                        color_ = mark_col[classOfX]

                    if (X_l[i, :] == X_u[i, :]).all():
                        marker_ = 'd'
                        if classOfX < len(mark):
                            marker_ = mark[classOfX]

                        if xX == 2:
                            inputPoint = drawing_canvas.plot(X_l[i, 0],
                                                             X_l[i, 1],
                                                             color=color_,
                                                             marker=marker_)
                        else:
                            inputPoint = drawing_canvas.plot([X_l[i, 0]],
                                                             [X_l[i, 1]],
                                                             [X_l[i, 2]],
                                                             color=color_,
                                                             marker=marker_)

                        #listInputSamplePoints.append(inputPoint)
                    else:
                        inputPoint = drawbox(
                            np.asmatrix(X_l[i, 0:np.minimum(xX, 3)]),
                            np.asmatrix(X_u[i, 0:np.minimum(xX, 3)]),
                            drawing_canvas, color_)

                    listInputSamplePoints.append(inputPoint[0])
                    self.delay()

                if self.V.size == 0:  # no model provided - starting from scratch
                    self.V = np.array([X_l[0]])
                    self.W = np.array([X_u[0]])
                    self.classId = np.array([patClassId[0]])

                    if self.isDraw == True:
                        # draw hyperbox
                        box_color = 'k'
                        if patClassId[0] < len(mark_col):
                            box_color = mark_col[patClassId[0]]

                        hyperbox = drawbox(
                            np.asmatrix(self.V[0, 0:np.minimum(xX, 3)]),
                            np.asmatrix(self.W[0, 0:np.minimum(xX, 3)]),
                            drawing_canvas, box_color)
                        listLines.append(hyperbox[0])
                        self.delay()

                else:
                    id_lb_sameX = np.logical_or(
                        self.classId == classOfX,
                        self.classId == UNLABELED_CLASS)

                    if id_lb_sameX.any() == True:
                        V_sameX = self.V[id_lb_sameX]
                        W_sameX = self.W[id_lb_sameX]
                        lb_sameX = self.classId[id_lb_sameX]
                        id_range = np.arange(len(self.classId))
                        id_processing = id_range[id_lb_sameX]

                        b = memberG(X_l[i], X_u[i],
                                    np.minimum(V_sameX, W_sameX),
                                    np.maximum(V_sameX, W_sameX), self.gamma)
                        index = np.argsort(b)[::-1]
                        bSort = b[index]

                        if bSort[0] != 1 or (classOfX != lb_sameX[index[0]]
                                             and classOfX != UNLABELED_CLASS):
                            adjust = False
                            for j in id_processing[index]:
                                # test violation of max hyperbox size and class labels
                                if (classOfX == self.classId[j]
                                        or self.classId[j] == UNLABELED_CLASS
                                        or classOfX == UNLABELED_CLASS) and (
                                            (np.maximum(self.W[j], X_u[i]) -
                                             np.minimum(self.V[j], X_l[i])) <=
                                            teta).all() == True:
                                    # adjust the j-th hyperbox
                                    self.V[j] = np.minimum(self.V[j], X_l[i])
                                    self.W[j] = np.maximum(self.W[j], X_u[i])
                                    indOfWinner = j
                                    adjust = True
                                    if classOfX != UNLABELED_CLASS and self.classId[
                                            j] == UNLABELED_CLASS:
                                        self.classId[j] = classOfX

                                    if self.isDraw:
                                        # Handle drawing graph
                                        box_color = 'k'
                                        if self.classId[j] < len(mark_col):
                                            box_color = mark_col[
                                                self.classId[j]]

                                        try:
                                            listLines[j].remove()
                                        except:
                                            pass

                                        hyperbox = drawbox(
                                            np.asmatrix(
                                                self.V[j,
                                                       0:np.minimum(xX, 3)]),
                                            np.asmatrix(
                                                self.W[j,
                                                       0:np.minimum(xX, 3)]),
                                            drawing_canvas, box_color)
                                        listLines[j] = hyperbox[0]
                                        self.delay()

                                    break

                            # if i-th sample did not fit into any existing box, create a new one
                            if not adjust:
                                self.V = np.concatenate(
                                    (self.V, X_l[i].reshape(1, -1)), axis=0)
                                self.W = np.concatenate(
                                    (self.W, X_u[i].reshape(1, -1)), axis=0)
                                self.classId = np.concatenate(
                                    (self.classId, [classOfX]))

                                if self.isDraw:
                                    # handle drawing graph
                                    box_color = 'k'
                                    if self.classId[-1] < len(mark_col):
                                        box_color = mark_col[self.classId[-1]]

                                    hyperbox = drawbox(
                                        np.asmatrix(X_l[i,
                                                        0:np.minimum(xX, 3)]),
                                        np.asmatrix(X_u[i,
                                                        0:np.minimum(xX, 3)]),
                                        drawing_canvas, box_color)
                                    listLines.append(hyperbox[0])
                                    self.delay()

                            elif self.V.shape[0] > 1:
                                for ii in range(self.V.shape[0]):
                                    if ii != indOfWinner and (
                                            self.classId[ii] !=
                                            self.classId[indOfWinner]
                                            or self.classId[indOfWinner]
                                            == UNLABELED_CLASS):
                                        caseDim = hyperboxOverlapTest(
                                            self.V, self.W, indOfWinner,
                                            ii)  # overlap test

                                        if caseDim.size > 0:
                                            self.V, self.W = hyperboxContraction(
                                                self.V, self.W, caseDim, ii,
                                                indOfWinner)
                                            if self.isDraw:
                                                # Handle graph drawing
                                                boxii_color = boxwin_color = 'k'
                                                if self.classId[ii] < len(
                                                        mark_col):
                                                    boxii_color = mark_col[
                                                        self.classId[ii]]

                                                if self.classId[
                                                        indOfWinner] < len(
                                                            mark_col):
                                                    boxwin_color = mark_col[
                                                        self.
                                                        classId[indOfWinner]]

                                                try:
                                                    listLines[ii].remove()
                                                    listLines[
                                                        indOfWinner].remove()
                                                except:
                                                    pass

                                                hyperboxes = drawbox(
                                                    self.V[
                                                        [ii, indOfWinner],
                                                        0:np.minimum(xX, 3)],
                                                    self.W[
                                                        [ii, indOfWinner],
                                                        0:np.minimum(xX, 3)],
                                                    drawing_canvas, [
                                                        boxii_color,
                                                        boxwin_color
                                                    ])
                                                listLines[ii] = hyperboxes[0]
                                                listLines[
                                                    indOfWinner] = hyperboxes[
                                                        1]
                                                self.delay()

                    else:
                        self.V = np.concatenate(
                            (self.V, X_l[i].reshape(1, -1)), axis=0)
                        self.W = np.concatenate(
                            (self.W, X_u[i].reshape(1, -1)), axis=0)
                        self.classId = np.concatenate(
                            (self.classId, [classOfX]))

                        if self.isDraw:
                            # handle drawing graph
                            box_color = 'k'
                            if self.classId[-1] < len(mark_col):
                                box_color = mark_col[self.classId[-1]]

                            hyperbox = drawbox(
                                np.asmatrix(X_l[i, 0:np.minimum(xX, 3)]),
                                np.asmatrix(X_u[i, 0:np.minimum(xX, 3)]),
                                drawing_canvas, box_color)
                            listLines.append(hyperbox[0])
                            self.delay()

            teta = teta * 0.9
            if teta >= self.tMin:
                result = predict(self.V, self.W, self.classId, X_l, X_u,
                                 patClassId, self.gamma, self.oper)
                self.misclass = result.summis

        # Draw last result


#        if self.isDraw == True:
#            # Handle drawing graph
#            drawing_canvas.cla()
#            color_ = np.empty(len(self.classId), dtype = object)
#            for c in range(len(self.classId)):
#                color_[c] = mark_col[self.classId[c]]
#
#            drawbox(self.V[:, 0:np.minimum(xX, 3)], self.W[:, 0:np.minimum(xX, 3)], drawing_canvas, color_)
#            self.delay()
#
#        if self.isDraw:
#            plt.show()

        time_end = time.perf_counter()
        self.elapsed_training_time = time_end - time_start

        return self
Exemple #3
0
    def fit(self, Xh, patClassId):
        """
        Training the classifier
        
         Xh             Input data (rows = objects, columns = features)
         patClassId     Input data class labels (crisp). patClassId[i] = 0 corresponds to an unlabeled item
        
        """
        print('--Online Learning for Simpson' 's FMNN--')

        if self.isNorm == True:
            Xh = self.dataPreprocessing(Xh)

        time_start = time.perf_counter()

        yX, xX = Xh.shape

        mark = np.array([
            '*', 'o', 'x', '+', '.', ',', 'v', '^', '<', '>', '1', '2', '3',
            '4', '8', 's', 'p', 'P', 'h', 'H', 'X', 'D', '|', '_'
        ])
        mark_col = np.array(['r', 'g', 'b', 'y', 'c', 'm', 'k'])

        listLines = list()

        if self.isDraw:
            drawing_canvas = self.initializeCanvasGraph(
                "FMNN - Simpson's fuzzy min-max neural network", xX)

            if self.V.size > 0:
                # draw existed hyperboxes
                color_ = np.array(['k'] * len(self.classId), dtype=object)
                for c in range(len(self.classId)):
                    if self.classId[c] < len(mark_col):
                        color_[c] = mark_col[self.classId[c]]

                hyperboxes = drawbox(self.V[:, 0:np.minimum(xX, 3)],
                                     self.W[:, 0:np.minimum(xX, 3)],
                                     drawing_canvas, color_)
                listLines.extend(hyperboxes)
                self.delay()

        # for each input sample
        for i in range(yX):
            classOfX = patClassId[i]
            # draw input samples
            if self.isDraw:

                color_ = 'k'
                if classOfX < len(mark_col):
                    color_ = mark_col[classOfX]

                marker_ = 'd'
                if classOfX < len(mark):
                    marker_ = mark[classOfX]

                if xX == 2:
                    drawing_canvas.plot(Xh[i, 0],
                                        Xh[i, 1],
                                        color=color_,
                                        marker=marker_)
                else:
                    drawing_canvas.plot([Xh[i, 0]], [Xh[i, 1]], [Xh[i, 2]],
                                        color=color_,
                                        marker=marker_)

                self.delay()

            if self.V.size == 0:  # no model provided - starting from scratch
                self.V = np.array([Xh[0]])
                self.W = np.array([Xh[0]])
                self.classId = np.array([patClassId[0]])

                if self.isDraw == True:
                    # draw hyperbox
                    box_color = 'k'
                    if patClassId[0] < len(mark_col):
                        box_color = mark_col[patClassId[0]]

                    hyperbox = drawbox(
                        np.asmatrix(self.V[0, 0:np.minimum(xX, 3)]),
                        np.asmatrix(self.W[0, 0:np.minimum(xX, 3)]),
                        drawing_canvas, box_color)
                    listLines.append(hyperbox[0])
                    self.delay()

            else:
                idSameClassOfX = np.nonzero(self.classId == classOfX)[0]
                # Find all hyperboxes same class with indexOfX
                V1 = self.V[idSameClassOfX]

                if len(V1) > 0:
                    W1 = self.W[idSameClassOfX]

                    b = simpsonMembership(Xh[i], V1, W1, self.gamma)

                    max_mem_id = np.argmax(b)

                    # store the index of the winner hyperbox in the list of all hyperboxes of all classes
                    j = idSameClassOfX[max_mem_id]

                    if b[max_mem_id] != 1:
                        adjust = False

                        # test violation of max hyperbox size and class labels
                        if (np.maximum(self.W[j], Xh[i]) - np.minimum(
                                self.V[j], Xh[i])).sum() <= self.teta * xX:
                            # adjust the j-th hyperbox
                            self.V[j] = np.minimum(self.V[j], Xh[i])
                            self.W[j] = np.maximum(self.W[j], Xh[i])
                            indOfWinner = j
                            adjust = True

                            if self.isDraw:
                                # Handle drawing graph
                                box_color = 'k'
                                if self.classId[j] < len(mark_col):
                                    box_color = mark_col[self.classId[j]]

                                try:
                                    listLines[j].remove()
                                except:
                                    pass

                                hyperbox = drawbox(
                                    np.asmatrix(self.V[j,
                                                       0:np.minimum(xX, 3)]),
                                    np.asmatrix(self.W[j,
                                                       0:np.minimum(xX, 3)]),
                                    drawing_canvas, box_color)
                                listLines[j] = hyperbox[0]
                                self.delay()

                        # if i-th sample did not fit into any existing box, create a new one
                        if not adjust:
                            self.V = np.vstack((self.V, Xh[i]))
                            self.W = np.vstack((self.W, Xh[i]))
                            self.classId = np.append(self.classId, classOfX)

                            if self.isDraw:
                                # handle drawing graph
                                box_color = 'k'
                                if self.classId[-1] < len(mark_col):
                                    box_color = mark_col[self.classId[-1]]

                                hyperbox = drawbox(
                                    np.asmatrix(Xh[i, 0:np.minimum(xX, 3)]),
                                    np.asmatrix(Xh[i, 0:np.minimum(xX, 3)]),
                                    drawing_canvas, box_color)
                                listLines.append(hyperbox[0])
                                self.delay()

                        elif self.V.shape[0] > 1:
                            for ii in range(self.V.shape[0]):
                                if ii != indOfWinner:
                                    caseDim = hyperboxOverlapTest(
                                        self.V, self.W, indOfWinner,
                                        ii)  # overlap test

                                    if caseDim.size > 0 and self.classId[
                                            ii] != self.classId[indOfWinner]:
                                        self.V, self.W = hyperboxContraction(
                                            self.V, self.W, caseDim, ii,
                                            indOfWinner)
                                        if self.isDraw:
                                            # Handle graph drawing
                                            boxii_color = boxwin_color = 'k'
                                            if self.classId[ii] < len(
                                                    mark_col):
                                                boxii_color = mark_col[
                                                    self.classId[ii]]

                                            if self.classId[indOfWinner] < len(
                                                    mark_col):
                                                boxwin_color = mark_col[
                                                    self.classId[indOfWinner]]

                                            try:
                                                listLines[ii].remove()
                                                listLines[indOfWinner].remove()
                                            except:
                                                pass

                                            hyperboxes = drawbox(
                                                self.V[[ii, indOfWinner],
                                                       0:np.minimum(xX, 3)],
                                                self.W[[ii, indOfWinner],
                                                       0:np.minimum(xX, 3)],
                                                drawing_canvas,
                                                [boxii_color, boxwin_color])
                                            listLines[ii] = hyperboxes[0]
                                            listLines[
                                                indOfWinner] = hyperboxes[1]
                                            self.delay()

                else:
                    # create a new hyperbox
                    self.V = np.vstack((self.V, Xh[i]))
                    self.W = np.vstack((self.W, Xh[i]))
                    self.classId = np.append(self.classId, classOfX)

                    if self.isDraw:
                        # handle drawing graph
                        box_color = 'k'
                        if self.classId[-1] < len(mark_col):
                            box_color = mark_col[self.classId[-1]]

                        hyperbox = drawbox(
                            np.asmatrix(Xh[i, 0:np.minimum(xX, 3)]),
                            np.asmatrix(Xh[i, 0:np.minimum(xX, 3)]),
                            drawing_canvas, box_color)
                        listLines.append(hyperbox[0])
                        self.delay()

        time_end = time.perf_counter()
        self.elapsed_training_time = time_end - time_start

        return self