Exemple #1
0
    def __split(self):
        """split current node"""
        max_gain, attr_index, partition_value = self.__information_gain()

        self.leaf = False
        self.attr_index = attr_index
        self.partition_value = partition_value

        tn_le = DTreeGain(DTreeSample(), self.param)
        tn_le.sample.m = self.sample.m
        tn_gt = DTreeGain(DTreeSample(), self.param)
        tn_gt.sample.m = self.sample.m

        for i in range(0, self.sample.n):
            x = self.sample.X[i]
            y = self.sample.Y[i]
            attr_value = x[attr_index]
            if attr_value <= partition_value:
                tn_le.sample.add_xy(x, y)
            else:
                tn_gt.sample.add_xy(x, y)

        left_n = len(tn_le.sample)
        right_n = len(tn_gt.sample)
        print >> sys.stderr, '[split]level=%d, max_gain=%f, attr_index=%d, partition_value=%f, left/right=%d/%d' % (
            self.level, max_gain, attr_index, partition_value, left_n, right_n)

        if left_n == 0:
            tn_le = None
        if right_n == 0:
            tn_gt = None
        return tn_le, tn_gt
Exemple #2
0
    def __split(self):
        """split current node"""
        max_gain, attr_index, partition_value, y_value_left, y_value_right = self.__gain()

        self.leaf = False
        self.attr_index = attr_index
        self.partition_value = partition_value
        self.value = None

        tn_le = DTreeLoss(DTreeSample(), self.param)
        tn_le.sample.m = self.sample.m
        tn_le.level = self.level + 1
        tn_le.value = y_value_left
        tn_gt = DTreeLoss(DTreeSample(), self.param)
        tn_gt.sample.m = self.sample.m
        tn_gt.level = self.level + 1
        tn_gt.value = y_value_right

        for i in range(0, self.sample.n):
            x = self.sample.X[i]
            y = self.sample.Y[i]
            y_residual = self.sample.Y_residual[i]
            attr_value = x[attr_index]
            if attr_value <= partition_value:
                tn_le.sample.add_xyr(x, y, y_residual)
            else:
                tn_gt.sample.add_xyr(x, y, y_residual)
        tn_le.__residual_2_response()
        tn_gt.__residual_2_response()

        left_n = len(tn_le.sample)
        right_n = len(tn_gt.sample)
        print >>sys.stderr, '[split]level=%d, max_gain=%f, attr_index=%d, partition_value=%f, left/right=%d/%d' % (self.level, max_gain, attr_index, partition_value, left_n, right_n)

        if left_n == 0:
            tn_le = None
        if right_n == 0:
            tn_gt = None
        return tn_le, tn_gt
Exemple #3
0
# -*- coding: utf-8 -*-
#
# 预测房地产价格
#
# author: yafei([email protected])
#
import sys
import codecs
import locale
from dtree_loss import DTreeLoss
from dtree_parameter import DTreeParameter
from dtree_sample import DTreeSample

if __name__ == '__main__':
    param = DTreeParameter()
    sample = DTreeSample()
    sample.load('real-estate.txt')
    dt = DTreeLoss(sample, param)
    dt.train(None)

    feature_map = {
        0: u'结构',
        1: u'装修',
        2: u'周边',
        3: u'地段',
        4: u'绿化',
        5: u'交通',
        6: u'户均车位',
    }
    # 为了输出中文
    locale.setlocale(locale.LC_ALL, '')