老提示边界出错,该如何改正程序

# code
```using CSV
using DelimitedFiles, Random, LinearAlgebra

function distance(x::Array{T,1},y::Array{T,1}) where {T <: Number}
       dist=0
       for i in 1:length(x)
       dist += (x[i]-y[i])^2
       end
       dist = sqrt(dist)
       return dist
       end

function classify(distances::Array{Float64,1}, labels::Array{T,1},k::Int64) where {T<:Any}
    class=unique(labels)
    nc=length(class)
    indexes=Array{Int,1}(undef,length(distances))
    M=typemax(typeof(distances[1]))
    class_count=Array{Int,1}(undef,nc)
for i in 1:k
       indexes[i]=findmin(distances)[2]
       distances[indexes[i]]=M
       end
klabels=labels[indexes]
for i in 1:nc
       for j in 1:k
       if klabels[j]==class[i]
       class_count[i]+=1
       break
       end
       end
       end
index=findmax(class_count)[1]
return class[index]
end

function apply_KNN(X::Array{T1,2}, x::Array{T2,1},Y::Array{T1,2},k::Int64) where {T1<:Number,T2<:Any}
N=size(X,1)
n=size(Y,1)
D=Array{Float64,1}(undef,N)
Z=Array{typeof(x[1]),1}(undef,n)
for i in 1:n
       for j in 1:N
       D[j] = distance(X[j,:],Y[i,:])
end
Z[i]=classify(D,x,k)
end
return Z
end

data=readdlm("J:/j-data/magic04.csv")
I=map(Float64,data[:,1:(end-1)])    # 剔除最后一列后,映射为Float64类型,作为样本的X
O=data[:,end]                       # 最后一列作为Y

N=length(O)                         # 样本数
n=round(Int64,N/2)                  # 样本分成两部分,各部分多少
R=randperm(N)                       # 生成样本序列编号

indX=R[1:n]                         # 前半部分
X=I[indX,:]                         # 按照随机顺序,生成新的样本X序列
x=O[indX]                           # 生成新的Y序列

indY=R[(n+1):end]                   # 后半部分
Y=I[indY,:]
y=O[indY]

Iz=apply_KNN(X,x,Y,5)
println(sum(y.==z[1])/n)
println(z[1][1:5],z[2][1:5])

请高手解决此问题