Julia机器学习--KNN算法KDTree结构搜索欧式距离最近的数据,并绘制图表

NearestNeighbors是Julia中一个效率比较高的KNN分类统计代码库,它提供了BallTree,KDTree等多种数据结构。

这里使用KDTree结构搜索欧式距离最近的数据, 并绘制图表。这里仍然使用鸢尾花数据

代码示例

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

iris = dataset("datasets", "iris"); # load the data

features = collect(Matrix(iris[:, 1:4])'); # features to use for clustering
#要搜索的点
point = features[:,1]
#其他的点
features = features[:,2:end]
#创建树
kdtree = KDTree(features)
#搜索多少个点
k =3

idxs, dists = knn(kdtree, point, k, true)
#搜索到的点的行索引
idxs
# 3-element Array{Int64,1}:
#  17
#   4
#  39
features[:,17]
features[:,4]
features[:,39]
#搜索到的点欧式距离
dists
# 3-element Array{Float64,1}:
#  0.09999999999999998
#  0.1414213562373093
#  0.14142135623730964
data = hcat(features[:,17],features[:,4],features[:,39])
data = data'

other = hcat(features[:,3:16],features[:,1:3],features[:,18:38],features[:,40:end])
other=other'

# 生成颜色图谱,并绘制搜索到的点图
cols = distinguishable_colors(4, RGB(0,0,0))
# 创建图片
cfig = figure()
ax = cfig[:add_subplot](1,1,1)
ax[:set_aspect]("equal")
axis((0.0,9.0,0.0,3.0))
data
for  row in eachrow(other)
    plot(row[3], row[4], "*",color = (cols[2].r, cols[2].g, cols[2].b))
end
for  row in eachrow(data)
    plot(row[3], row[4], "*",color = (cols[3].r, cols[3].g, cols[3].b))
end
plot(point[3], point[4], "*",color = (cols[4].r, cols[4].g, cols[4].b))

title("iris")
cfig[:savefig]("iris.png")

绘制图表

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

October-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值