Julia機器學習--KNN算法BallTree 結構分類統計,並使用圓繪製圖表

NearestNeighbors是Julia中一個效率比較高的KNN分類統計代碼庫,它提供了BallTree,KDTree等多種數據結構。

這裏使用BallTree結構,並繪製圖表。這裏仍然使用鳶尾花數據

代碼示例

using RDatasets
using DataFrames
using CSV
using NearestNeighbors
using Colors
using PyPlot
using PyCall
import PyPlot:plot
import NearestNeighbors.HyperSphere
@pyimport matplotlib.patches as patch
# y = load("D:/leaning/Julia/pkg-other/Rdatasets/csv/datasets/iris.csv") |> DataFrame

iris = dataset("datasets", "iris"); # load the data
# iris = DataFrame(CSV.File(joinpath(dirname(pathof(DataFrames)),"D:/leaning/Julia/pkg-other/Rdatasets/csv/datasets/iris.csv")));
show(iris)
iris[:, 1:4]
features = collect(Matrix(iris[:, 1:4])'); # features to use for clustering

tree = BallTree(features, Euclidean(); leafsize = 50)


# 跳過非葉子節點
offset = tree.tree_data.n_internal_nodes + 1
nleafs = tree.tree_data.n_leafs

# 葉子節點的範圍
index_range = offset: offset + nleafs - 1

# 生成顏色圖譜
cols = distinguishable_colors(length(index_range), RGB(0,0,0))

# 創建圖片
cfig = figure()
ax = cfig[:add_subplot](1,1,1)
ax[:set_aspect]("equal")
axis((2.5,9.0,1.0,5.0))

# 座標上添加一個圓
function add_sphere(ax, hs::HyperSphere, col)
    ell = patch.Circle(hs.center, radius = hs.r, facecolor="none", edgecolor=col)
    ax[:add_artist](ell)
end


for (i, idx) = enumerate(index_range)
    col = cols[i]
    # 獲取決策樹中的葉子節點
    range = NearestNeighbors.get_leaf_range(tree.tree_data, idx)
    d = tree.data[range]
    for idex in 1:length(d)
        point = collect(d[idex])
        # 先畫點
        plot(vec(point[1,:]), vec(point[2,:]), "*", color = (col.r, col.g, col.b))
    end
    # 設置圓
    sphere = tree.hyper_spheres[idx]
    add_sphere(ax, sphere, (col.r, col.g, col.b))
end

title("Leaf nodes with their corresponding points")
cfig[:savefig]("iris.png")

繪製的圖表

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章