当前位置: 首页>>代码示例>>Python>>正文


Python BST.range方法代码示例

本文整理汇总了Python中bst.BST.range方法的典型用法代码示例。如果您正苦于以下问题:Python BST.range方法的具体用法?Python BST.range怎么用?Python BST.range使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bst.BST的用法示例。


在下文中一共展示了BST.range方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: axis_aligned_hypotheses

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import range [as 别名]
def axis_aligned_hypotheses(dataset):
    """
    Given a dataset in R2, return an iterator over hypotheses that result in
    distinct classifications of those points.

    Classifiers are axis-aligned rectangles

    Args:
      dataset: The dataset to use to generate hypotheses
    """
    validRectangles = [[(float('inf'),float('inf')),(float('inf'),float('inf'))]]
    xTree = BST()
    yTree = BST()
    ydataset = [(point[1],point[0]) for point in dataset]
    [xTree.insert(point) for point in dataset]
    [yTree.insert(point) for point in ydataset]

    for i in range(1,len(dataset)+1):
        d = combinations(dataset, i)
        l = []
        for comb in d:
            min_x = min(comb, key=operator.itemgetter(0))[0]
            max_x = max(comb, key=operator.itemgetter(0))[0]
            min_y = min(comb, key=operator.itemgetter(1))[1]
            max_y = max(comb, key=operator.itemgetter(1))[1]
            xRange = []
            yRange = []
            # Generate list of points in each range (x,y)
            for r in xTree.range(min_x, max_x):
                xRange.append(r.key)
            for r in yTree.range(min_y, max_y):
                point = r.key
                yRange.append((point[1], point[0]))

            # Check for points in both ranges
            bothHave = []
            for point in xRange:
                if point in yRange and point not in bothHave:
                    bothHave.append(point)

            # Valid only if length of combination == length of points both have
            if len(comb) == len(bothHave):

                validRectangles.append([(min_x-0.1, min_y-0.1), (max_x+0.1, max_y+0.1)])
                drawRectangles.append(validRectangles[-1])
    
    for vr in validRectangles:
        yield AxisAlignedRectangle(vr[0][0], vr[0][1], vr[1][0], vr[1][1])        
开发者ID:rpcrimi,项目名称:Machine-Learning-Homework,代码行数:50,代码来源:rademacher.py

示例2: axis_aligned_hypotheses

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import range [as 别名]
def axis_aligned_hypotheses(dataset):
    """
    Given a dataset in R2, return an iterator over hypotheses that result in
    distinct classifications of those points.

    Classifiers are axis-aligned rectangles

    Args:
      dataset: The dataset to use to generate hypotheses
    """

    # TODO: complete this function

    """
    Brute Force Solution, this algorithm works, the complexity is about n^5, run 100 points for about 250 seconds

    """

    # all_hypotheses = defaultdict(int)
    # all_boundary = defaultdict(list)
    # dataset_size = len(dataset)
    # origin_strlist = ['0' for x in xrange(dataset_size)]
    # for idx in xrange(dataset_size):
    #     id_strlist = origin_strlist[:]
    #     id_strlist[idx] = '1'
    #     id_str = "".join(id_strlist)
    #     all_hypotheses[id_str] += 1
    #     all_boundary[id_str] = [dataset[idx][0], dataset[idx][1], dataset[idx][0], dataset[idx][1]]
    #
    # for comb in combinations(dataset, 2):
    #     inner_points = points_in_the_rectangle(dataset, comb[0], comb[1])
    #     id_strlist = origin_strlist[:]
    #     for point in inner_points:
    #         id_strlist[point] = '1'
    #     id_str = "".join(id_strlist)
    #     all_hypotheses[id_str] += 1
    #     all_boundary[id_str] = [min(comb[0][0], comb[1][0]), min(comb[0][1], comb[1][1]), \
    #                             max(comb[0][0], comb[1][0]), max(comb[0][1], comb[1][1])]
    #
    # for comb in combinations(dataset, 3):
    #     inner_points = points_in_the_rectangle(dataset, comb[0], comb[1], comb[2])
    #     id_strlist = origin_strlist[:]
    #     for point in inner_points:
    #         id_strlist[point] = '1'
    #     id_str = "".join(id_strlist)
    #     all_hypotheses[id_str] += 1
    #     all_boundary[id_str] = [min(comb[0][0], comb[1][0], comb[2][0]), min(comb[0][1], comb[1][1], comb[2][1]), \
    #                             max(comb[0][0], comb[1][0], comb[2][0]), max(comb[0][1], comb[1][1], comb[2][1])]
    #
    # for comb in combinations(dataset, 4):
    #     inner_points = points_in_the_rectangle(dataset, comb[0], comb[1], comb[2], comb[3])
    #     id_strlist = origin_strlist[:]
    #     for point in inner_points:
    #         id_strlist[point] = '1'
    #     id_str = "".join(id_strlist)
    #     all_hypotheses[id_str] += 1
    #     all_boundary[id_str] = [min(comb[0][0], comb[1][0], comb[2][0], comb[3][0]), \
    #                             min(comb[0][1], comb[1][1], comb[2][1], comb[3][1]), \
    #                             max(comb[0][0], comb[1][0], comb[2][0], comb[3][0]), \
    #                             max(comb[0][1], comb[1][1], comb[2][1], comb[3][1])]


    # for key in all_hypotheses:
    #     x1, y1, x2, y2 = all_boundary.get(key)
    #     yield  AxisAlignedRectangle(x1, y1, x2, y2)
    # yield AxisAlignedRectangle(1e17, 1e17, 1e17, 1e17)


    """
    This algorithm works efficiently and easy to understand, using bst to get ranges of y when scan along the x axis
    """

    all_boundary = defaultdict(int)
    sorted_dataset = sorted(dataset)
    dataset_size = len(dataset)
    for l_idx in xrange(dataset_size):
        l_point = sorted_dataset[l_idx]
        y_tree = BST()
        y_tree.insert(sorted_dataset[l_idx][1])
        for r_idx in xrange(l_idx, dataset_size):
            y_tree.insert(sorted_dataset[r_idx][1])
            r_point = sorted_dataset[r_idx]
            y_bot = min(l_point[1], r_point[1])
            y_top = max(l_point[1], r_point[1])
            x_left = l_point[0]
            x_right = r_point[0]
            upper_set = set(x.key for x in y_tree.range(y_top+1e-15, 1e300))
            lower_set = set(x.key for x in y_tree.range(-1e300, y_bot-1e-15))
            for upper_boundary in upper_set:
                if not all_boundary.get((x_left, y_bot, x_right, upper_boundary)):
                    yield AxisAlignedRectangle(x_left, y_bot, x_right, upper_boundary)
                all_boundary[(x_left, y_bot, x_right, upper_boundary)] += 1
                for lower_boundary in lower_set:
                    if not all_boundary.get((x_left, lower_boundary, x_right, upper_boundary)):
                        yield AxisAlignedRectangle(x_left, lower_boundary, x_right, upper_boundary)
                    all_boundary[(x_left, lower_boundary, x_right, upper_boundary)] += 1
            for lower_boundary in lower_set:
                if not all_boundary.get((x_left, lower_boundary, x_right, y_top)):
                    yield AxisAlignedRectangle(x_left, lower_boundary, x_right, y_top)
                all_boundary[(x_left, lower_boundary, x_right, y_top)] += 1
#.........这里部分代码省略.........
开发者ID:PeicheTsao,项目名称:machine-learning-course,代码行数:103,代码来源:rademacher.py

示例3: axis_aligned_hypotheses

# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import range [as 别名]
def axis_aligned_hypotheses(dataset):
    """
    Given a dataset in R2, return an iterator over hypotheses that result in
    distinct classifications of those points.

    Classifiers are axis-aligned rectangles

    Args:
      dataset: The dataset to use to generate hypotheses
    """

    # Based on VC-Dimension, we can only ever have 4 points
    # Inside rectangles is positive
    # 0 Positive - take x_min-1, y_min-1 make that a rectangle-point
    # 1 Positive - each point is its own rectangle
    # 2 Positive - each pair of points
    # 3 Positive - much be contained within the extreme pairs
    #  Need to make sure I'm not double-classifying somehow
    # 4 Positive - x_min-x_max, y_min-y_max
    
    # Min/Max variables
    x_min = 0
    y_min = 0
    x_max = 0
    y_max = 0

    # Create a BST and fill it with all of the points in the dataset
    bst = BST()
    map(bst.insert, dataset)
    # list to hold pair combos
    a = []

    # Dataset is a list of tuples of points
    for start_point in dataset:
        start_x = start_point[0]
        start_y = start_point[1]
        yield AxisAlignedRectangle(start_x, start_y, start_x, start_y)
        # Calculate min/maxes
        if start_x > x_max:
            x_max = start_x
        elif start_x < x_min:
            x_min = start_x
        if start_y > y_max:
            y_max = start_y
        elif start_y < y_min:
            y_min = start_y       
        for end_point in dataset:
            points_in_range = []
            end_x = end_point[0]
            end_y = end_point[1]
            # Manipulate points that only cause half negative
            if (end_x > start_x and end_y < start_y):
                start_y = end_y
            elif (end_x < start_x and end_y > start_y):
                start_x = end_x
            # Skip points that cause fully negative rectangles
            if (end_x <= start_x and end_y <= start_y):
                continue
            points_in_range = map(lambda x: x.key, list(bst.range(start_point, end_point)))
            if points_in_range in a:
                continue
            else:
                yield AxisAlignedRectangle(start_x, start_y, end_x, end_y)
                a.append(points_in_range)
    # Get the 4 point cases
    # All in
    yield AxisAlignedRectangle(x_min-1, y_min-1, x_max+1, y_max+1)
    # All out
    yield AxisAlignedRectangle(x_min-1, y_min-1,x_min-1, y_min-1)
开发者ID:jkeuhlen,项目名称:machinelearning,代码行数:71,代码来源:rademacher.py


注:本文中的bst.BST.range方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。