楼主: Scalachen
1115 0

Simple Static KD-Trees using Julia [推广有奖]

  • 0关注
  • 0粉丝

已卖:147份资源

本科生

56%

还不是VIP/贵宾

-

TA的文库  其他...

Haskell NewOccidental

Splunk NewOccidental

Apache Storm NewOccidental

威望
0
论坛币
5149 个
通用积分
0
学术水平
9 点
热心指数
11 点
信用等级
9 点
经验
1156 点
帖子
24
精华
1
在线时间
0 小时
注册时间
2015-3-29
最后登录
2017-8-22

楼主
Scalachen 发表于 2015-3-31 21:16:58 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. # Simple static kd-trees.


  2. abstract KDNode


  3. immutable KDTree{T <: FloatingPoint}
  4.         # A matrix of n, m-dimensional observations
  5.         xs::AbstractMatrix{T}

  6.         # Permutaiton of the data used by the structure (to avoid sorting or
  7.         # otherwise modifying xs)
  8.         perm::Vector{Int}

  9.         root::KDNode

  10.         verts::Set{Vector{T}}

  11.         # Top-level bounding box
  12.         bounds::Matrix{T}
  13. end


  14. # Construct a kd-tree
  15. #
  16. # Args:
  17. #   xs: Data organized in the tree.
  18. #   leaf_size_factor: Stop spliting if a node contains
  19. #       fewer than leaf_size_factor * n elements.
  20. #   leaf_diameter_factor: Stop spliting if a node's bounding
  21. #       hypercube has diameter less that leaf_diameter_factor
  22. #       times the diameter of the root node's bounding hypercube.
  23. #
  24. # Returns:
  25. #   A KDTree object
  26. #
  27. function KDTree{T <: FloatingPoint}(xs::AbstractMatrix{T},
  28.                                         leaf_size_factor=0.05,
  29.                                         leaf_diameter_factor=0.0)
  30.         n, m = size(xs)
  31.         perm = collect(1:n)

  32.         bounds = Array(T, (2, m))
  33.         for j in 1:m
  34.                 col = xs[:,j]
  35.                 bounds[1, j] = minimum(col)
  36.                 bounds[2, j] = maximum(col)
  37.         end
  38.         diam = diameter(bounds)

  39.         leaf_size_cutoff = iceil(leaf_size_factor * n)
  40.         leaf_diameter_cutoff = leaf_diameter_factor * diam
  41.         verts = Set{Vector{T}}()

  42.         # Add verticies defined by the bounds
  43.         for vert in product([bounds[:,j] for j in 1:m]...)
  44.                 push!(verts, T[vert...])
  45.         end

  46.         root = build_kdtree(xs, sub(perm, 1:n), bounds,
  47.                                 leaf_size_cutoff, leaf_diameter_cutoff, verts)

  48.         KDTree(xs, collect(1:n), root, verts, bounds)
  49. end


  50. immutable KDLeafNode <: KDNode
  51. end


  52. immutable KDInternalNode{T <: FloatingPoint} <: KDNode
  53.         j::Int # dimension on which the data is split
  54.         med::T # median value where the split occours
  55.         leftnode::KDNode
  56.         rightnode::KDNode
  57. end



  58. # Compute the diamater of a hypercube defined by bounds
  59. #
  60. # Args:
  61. #   bounds: A 2 by n matrix where bounds[1,i] gives the lower bound in
  62. #           the ith dimension and bonuds[2,j] the upper.
  63. #
  64. # Returns:
  65. #   Computed diameter
  66. #
  67. function diameter(bounds::Matrix)
  68.         euclidean(vec(bounds[1,:]), vec(bounds[2,:]))
  69. end


  70. # Recursively build a kd-tree
  71. #
  72. # Args:
  73. #   xs: Data being orginized.
  74. #   perm: Permutation of the data, used to avoid
  75. #       directly sorting or modifying xs.
  76. #   bounds: Bounding hypercube of the node.
  77. #   leaf_size_cutoff: stop spliting on nodes with more than
  78. #       this many values.
  79. #   leaf_diameter_cutoff: stop splitting on nodes with less
  80. #        than this diameter.
  81. #   verts: current set of vertexes
  82. #
  83. # Modifies:
  84. #   perm, verts
  85. #
  86. # Returns:
  87. #   Either a KDLeafNode or a KDInternalNode
  88. #
  89. function build_kdtree{T}(xs::AbstractMatrix{T},
  90.                              perm::SubArray,
  91.                              bounds::Matrix{T},
  92.                              leaf_size_cutoff::Int,
  93.                              leaf_diameter_cutoff::T,
  94.                              verts::Set{Vector{T}})
  95.         n, m = size(xs)

  96.         if length(perm) <= leaf_size_cutoff || diameter(bounds) <= leaf_diameter_cutoff
  97.                 return KDLeafNode()
  98.         end

  99.         # split on the dimension with the largest spread
  100.         j = 1
  101.         maxspread = 0
  102.         for k in 1:m
  103.                 xmin = Inf
  104.                 xmax = -Inf
  105.                 for i in perm
  106.                         xmin = min(xmin, xs[i, k])
  107.                         xmax = max(xmax, xs[i, k])
  108.                 end
  109.                 if xmax - xmin > maxspread
  110.                         maxspread = xmax - xmin
  111.                         j = k
  112.                 end
  113.         end

  114.         # find the median and partition
  115.         if isodd(length(perm))
  116.                 mid = div(length(perm), 2)
  117.                 select!(perm, mid, by=i -> xs[i, j])
  118.                 med = xs[perm[mid], j]
  119.                 mid1 = mid
  120.                 mid2 = mid + 1
  121.         else
  122.                 mid1 = div(length(perm), 2)
  123.                 mid2 = mid1 + 1
  124.                 select!(perm, mid1, by=i -> xs[i, j])
  125.                 select!(perm, mid2, by=i -> xs[i, j])
  126.                 med = (xs[perm[mid1], j] + xs[perm[mid2], j]) / 2
  127.         end

  128.         leftbounds = copy(bounds)
  129.         leftbounds[2, j] = med
  130.         leftnode = build_kdtree(xs, sub(perm, 1:mid1), bounds,
  131.                                     leaf_size_cutoff, leaf_diameter_cutoff, verts)

  132.         rightbounds = copy(bounds)
  133.         rightbounds[1, j] = med
  134.         rightnode = build_kdtree(xs, sub(perm, mid2:length(perm)), bounds,
  135.                                      leaf_size_cutoff, leaf_diameter_cutoff, verts)

  136.         coords = Array(Array, m)
  137.         for i in 1:m
  138.                 if i == j
  139.                         coords[i] = [med]
  140.                 else
  141.                         coords[i] = bounds[:, i]
  142.                 end
  143.         end

  144.         for vert in product(coords...)
  145.                 push!(verts, T[vert...])
  146.         end

  147.         KDInternalNode{T}(j, med, leftnode, rightnode)
  148. end


  149. # Given a bounding hypecube, return its verticies
  150. function bounds_verts(bounds::Matrix)
  151.         collect(product([bounds[:, i] for i in 1:size(bounds, 2)]...))
  152. end


  153. # Traverse the tree to the bottom and return the verticies of
  154. # the leaf node's bounding hypercube.
  155. function traverse{T}(kdtree::KDTree{T}, xs::AbstractVector{T})
  156.         m = size(kdtree.bounds, 2)

  157.         if length(xs) != m
  158.                 error("$(m)-dimensional kd-tree searched with a length $(length(x)) vector.")
  159.         end

  160.         for j in 1:m
  161.                 if xs[j] < kdtree.bounds[1, j] || xs[j] > kdtree.bounds[2, j]
  162.                         error(
  163.                                 """
  164.                                 Loess cannot perform extrapolation. Predict can only be applied
  165.                                 to points within the bounding hypercube of the data used to train
  166.                                 the model.
  167.                                 """)
  168.                 end
  169.         end

  170.         bounds = copy(kdtree.bounds)
  171.         node = kdtree.root
  172.         while !isa(node, KDLeafNode)
  173.                 if xs[node.j] <= node.med
  174.                         bounds[2, node.j] = node.med
  175.                         node = node.leftnode
  176.                 else
  177.                         bounds[1, node.j] = node.med
  178.                         node = node.rightnode
  179.                 end
  180.         end

  181.         bounds_verts(bounds)
  182. end
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:static simple Trees Julia Using

本帖被以下文库推荐

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-2 08:02