예제 #1
0
    def Process(self, sourcex, sourcey, targetx, targety, subsize):
        # fixed size windows for source stream and target stream
        #kpca = KernelPCA(n_components=3, kernel = 'rbf',gamma = 15)
        #print("sourcex.shape", sourcex.shape)
        #x_kpca = kpca.fit_transform(sourcex)
        #print("x_kpca.shape", x_kpca.shape)
        #sourcex = np.matrix(x_kpca)
        #y_kpca = kpca.fit_transform(targetx)
        #targetx = np.matrix(y_kpca)
        src_size, _ = sourcex.shape
        tgt_size, _ = targetx.shape
        #true_label = []

        ### get the initial model by using the first source and target windows
        alpha = 0.5
        b = targetx.T.shape[1]
        fold = 5
        sigma_list = Classification.sigma_list(np.array(targetx.T),
                                               np.array(sourcex.T))
        lambda_list = Classification.lambda_list()
        srcx_array = np.array(sourcex.T)
        trgx_array = np.array(targetx.T)
        #(thetah_old, w, sce_old, sigma_old) = Classification.R_ULSIF(trgx_array, srcx_array, alpha, sigma_list, lambda_list, b, fold)

        self.Ensemble.generateNewModelRULSIF(targetx, sourcex, sourcey, alpha,
                                             sigma_list, lambda_list, b, fold,
                                             subsize)
        # print "update model", src_size, source.shape
        truelablecount = 0.0
        totalcount = 0.0
        for i in range(0, len(targety)):
            #print("targetx[i]", targetx[i])
            instanceresult = self.Ensemble.evaluateEnsembleRULSIF(targetx[i])
            #print("instanceresult", instanceresult)
            #print("instanceresult[0]", instanceresult[0])
            #print("truelabel[i]", targety[i])
            if instanceresult[0] == targety[i]:
                truelablecount += 1.0
            totalcount += 1.0
            if i % 100 == 0:
                print("truelabelcount", truelablecount)
                print("totalcount", totalcount)
                print("tmp acc", truelablecount / totalcount)
        with open('accuracynormgroup3datafilegame0726.csv', 'a+') as f:
            writer = csv.writer(f)
            writer.writerow([
                len(targety), truelablecount, totalcount,
                truelablecount / totalcount
            ])
예제 #2
0
    def Process(self, sourcex: np.matrix, sourcey: List[int],
                targetx: np.matrix, targety: List[int], subsize: int) -> None:
        # fixed size windows for source stream and target stream
        # kpca = KernelPCA(n_components=3, kernel = 'rbf',gamma = 15)
        # x_kpca = kpca.fit_transform(sourcex)
        # sourcex = np.matrix(x_kpca)
        # y_kpca = kpca.fit_transform(targetx)
        # targetx = np.matrix(y_kpca)

        src_size, _ = sourcex.shape
        tgt_size, _ = targetx.shape

        # get the initial model by using the first source and target windows
        alpha = 0.5
        b = targetx.T.shape[1]
        fold = 5
        sigma_list = Classification.sigma_list(np.array(targetx.T),
                                               np.array(sourcex.T))
        lambda_list = Classification.lambda_list()
        # srcx_array = np.array(sourcex.T)
        # trgx_array = np.array(targetx.T)
        # (thetah_old, w, sce_old, sigma_old) = Classification.R_ULSIF(trgx_array, srcx_array, alpha, sigma_list, lambda_list, b, fold)

        # todo
        self.Ensemble.generateNewModelRULSIF(targetx, sourcex, sourcey, alpha,
                                             sigma_list, lambda_list, b, fold,
                                             subsize)

        # test model
        trueLableCount = 0.0
        totalCount = 0.0
        for i in range(0, len(targety)):
            instanceResult = self.Ensemble.evaluateEnsembleRULSIF(targetx[i])
            if instanceResult[0] == targety[i]:
                trueLableCount += 1.0
            totalCount += 1.0
            if i % 100 == 0:
                print("test size", i)
                print("true label count", trueLableCount)
                print("total count", totalCount)
                print("tmp acc", trueLableCount / totalCount)
        # write result to file
        with open('output/accuracy_norm15group1player.csv', 'a+') as f:
            writer = csv.writer(f)
            writer.writerow([
                len(targety), trueLableCount, totalCount,
                trueLableCount / totalCount
            ])
예제 #3
0
    def readdata(
            sourcex_matrix=None,
            sourcey_matrix=None,
            targetx_matrix=None,
            targety_matrix=None,
            src_path=SRC_FILE_PATH,
            tgt_path=TGT_FILE_PATH,
            src_size=None,
            tgt_size=None
    ) -> Tuple[np.matrix, List[int], np.matrix, List[int]]:
        """ 
        input is: source dataset with y, here we assume it is a list of list, the name is source, target dataset with yhat, 
        here we assume it is a list of list, the name is target 
        """
        if sourcex_matrix is None:
            sourcex_matrix_, sourcey_matrix = Classification.read_csv(
                src_path)  # matrix_ is source data
        else:
            sourcex_matrix_ = sourcex_matrix
            sourcey_matrix_ = sourcey_matrix

        if targetx_matrix is None:
            targetx_matrix_, targety_matrix_ = Classification.read_csv(
                tgt_path)
        else:
            targetx_matrix_ = targetx_matrix
            targety_matrix_ = targety_matrix

        # get list of all labels
        labelList = []
        for i in range(0, len(targety_matrix_)):
            if targety_matrix_[i] not in labelList:
                labelList.append(targety_matrix_[i])
        print("label list len:", len(labelList))

        # get list of indices of all source y labels
        sourcey_label = []
        for i in range(0, len(sourcey_matrix)):
            sourcey_label.append(labelList.index(sourcey_matrix[i]))

        # get list of indices of all target y labels
        targety_label = []
        for i in range(0, len(targety_matrix_)):
            targety_label.append(labelList.index(targety_matrix_[i]))

        return sourcex_matrix_, sourcey_label, targetx_matrix_, targety_label
예제 #4
0
    def readdata(sourcex_matrix=None,
                 sourcey_matrix=None,
                 targetx_matrix=None,
                 targety_matrix=None,
                 src_path=SRC_FILE_PATH,
                 tgt_path=TGT_FILE_PATH,
                 src_size=None,
                 tgt_size=None):
        """ 
        input is: source dataset with y, here we assume it is a list of list, the name is source, target dataset with yhat, 
        here we assume it is a list of list, the name is target 
        """
        if sourcex_matrix is None:
            sourcex_matrix_, sourcey_matrix = Classification.read_csv(
                src_path, None)  # matrix_ is source data
        else:
            sourcex_matrix_ = sourcex_matrix
            sourcey_matrix_ = sourcey_matrix
        matrix_ = sourcex_matrix_[:src_size, :]

        if targetx_matrix is None:
            targetx_, targety_ = Classification.read_csv(tgt_path, size=None)
        else:
            targetx_ = targetx_matrix
            targety_ = targety_matrix
        labellist = []
        for i in range(0, len(targety_)):
            if targety_[i] not in labellist:
                labellist.append(targety_[i])
        print("labellistlen", len(labellist))
        sourcey_label = []
        for i in range(0, len(sourcey_matrix)):
            sourcey_label.append(labellist.index(sourcey_matrix[i]))

        for i in range(0, len(targety_)):
            if targety_[i] not in labellist:
                labellist.append(targety_[i])
        targety_label = []
        for i in range(0, len(targety_)):
            targety_label.append(labellist.index(targety_[i]))
        return sourcex_matrix_, sourcey_label, targetx_, targety_label
예제 #5
0
    def generateNewModelRULSIF(self, trgx_matrix, srcx_matrix, srcy_matrix,
                               alpha, sigma_list, lambda_list, b, fold,
                               subsize):
        model = Model()

        if len(srcx_matrix) == 0 or len(trgx_matrix) == 0:
            raise Exception(
                'Source or Target stream should have some elements')

        # Create new model
        print('Target model creation')
        model.model = Classification.get_model(trgx_matrix, srcx_matrix,
                                               srcy_matrix, alpha, sigma_list,
                                               lambda_list, b, fold, subsize)

        # compute source and target weight
        print('Computing model weights')
        model.weight = model.computeModelWeightRULSIF(trgx_matrix)

        # update ensemble
        index = self.__addModelRULSIF(model, trgx_matrix)
        if index != -1:
            print('Ensemble updated at ' + str(index))