Exemplo n.º 1
0
    def load_tree(path_to_file, path_to_param=None, bias=None):
        if path_to_param is not None:
            with open(path_to_param, "r") as fin:
                real_nr_features = int(fin.readline())
                for i in range(6):  # bypassing uncessary features
                    fin.readline()
                bias = float(fin.readline)
        if bias is None:
            bias = 1.0  # the bias term is default to 1.0 in the parabel package
        '''Load a single tree model obtained from Parabel Package'''
        with open(path_to_file, "r") as fin:
            nr_features = int(
                fin.readline()) - 1 if bias <= 0 else 0  # remove the bias term
            nr_labels = int(fin.readline())
            nr_nodes = int(fin.readline())
            max_depth = int(sp.log2(nr_nodes + 1))
            Clist, Wlist = [], []
            for depth in range(max_depth):
                nr_nodes_with_depth = 2**depth
                if depth != max_depth - 1:
                    C = smat_util.coo_appender((2**(depth + 1), 2**depth))
                    W = smat_util.coo_appender((nr_features, 2**(depth + 1)))
                else:
                    C = smat_util.coo_appender((nr_labels, 2**depth))
                    W = smat_util.coo_appender((nr_features, nr_labels))

                child_offset = 2**(depth + 1) - 1
                for nid in range(nr_nodes_with_depth):
                    is_leaf = int(fin.readline().strip())
                    left, right = [
                        int(x) - child_offset
                        for x in fin.readline().strip().split()
                    ]
                    cur_depth = int(fin.readline().strip())
                    assert cur_depth == depth
                    tmp = fin.readline().strip().split()
                    labels = [int(y) for y in tmp[1:]]
                    nr_childs = int(fin.readline().strip().split()[0])
                    if is_leaf != 1:
                        labels = [left, right]
                    for y in labels:
                        C.append(y, nid, 1.0)
                        for iv in fin.readline().strip().split():
                            iv = iv.split(':')
                            col = y
                            row = int(iv[0])
                            if row >= nr_features:
                                continue
                            v = float(iv[1])
                            W.append(row, col, v)
                Clist += [C.tocsr()]
                Wlist += [W.tocsr()]
        return HierarchicalMLModel(
            [MLModel(w, c) for w, c in zip(Wlist, Clist)], bias)
Exemplo n.º 2
0
    def load_tree(path_to_file):
        with open(path_to_file, "r") as fin:
            nr_features = int(
                fin.readline()) - 2  # remove the zero and bias term
            nr_labels = int(fin.readline())
            nr_nodes = int(fin.readline())
            max_depth = int(sp.log2(nr_nodes + 1))
            Clist, Wlist = [], []
            for depth in range(max_depth):
                nr_nodes_with_depth = 2**depth
                if depth != max_depth - 1:
                    C = smat_util.coo_appender((2**(depth + 1), 2**depth))
                    W = smat_util.coo_appender((nr_features, 2**(depth + 1)))
                else:
                    C = smat_util.coo_appender((nr_labels, 2**depth))
                    W = smat_util.coo_appender((nr_features, nr_labels))

                child_offset = 2**(depth + 1) - 1
                for nid in range(nr_nodes_with_depth):
                    is_leaf = int(fin.readline().strip())
                    left, right = [
                        int(x) - child_offset
                        for x in fin.readline().strip().split()
                    ]
                    cur_depth = int(fin.readline().strip())
                    assert cur_depth == depth
                    tmp = fin.readline().strip().split()
                    labels = [int(y) for y in tmp[1:]]
                    nr_childs = int(fin.readline().strip().split()[0])
                    if is_leaf != 1:
                        labels = [left, right]
                    for y in labels:
                        C.append(y, nid, 1.0)
                        for iv in fin.readline().strip().split():
                            iv = iv.split(':')
                            col = y
                            row = int(iv[0]) - 1
                            if row >= nr_features:
                                continue
                            v = float(iv[1])
                            W.append(row, col, v)
                Clist += [C.tocsr()]
                Wlist += [W.tocsr()]
        return HierarchicalMLModel(
            [MLModel(w, c) for w, c in zip(Wlist, Clist)])
Exemplo n.º 3
0
 def load_prediction(path_to_file, only_topk=None):
     with open(path_to_file, 'r') as fin:
         nr_insts, nr_labels = [
             int(x) for x in fin.readline().strip().split()
         ]
         coo = smat_util.coo_appender((nr_insts, nr_labels))
         for i in range(nr_insts):
             for iv in fin.readline().strip().split():
                 iv = iv.split(':')
                 j = int(iv[0])
                 v = float(iv[1])
                 coo.append(i, j, v)
     return smat_util.sorted_csr(coo.tocsr(), only_topk=only_topk)