1 2 3 4 5 6 7 8 9 10 Sepallength Sepalwidth Petallength Petalwidth Species

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

2020/12/31 🎈 KMeans.jl ⚡ Pluto.

jl ⚡

⋅ using Pkg

⋅ Pkg.add("Clustering")

⋅ Pkg.add("RDatasets")

⋅ using Clustering, RDatasets, Plots

SepalLength SepalWidth PetalLength PetalWidth Species


1 5.1 3.5 1.4 0.2 CategoricalValue{String,UInt

2 4.9 3.0 1.4 0.2 CategoricalValue{String,UInt

3 4.7 3.2 1.3 0.2 CategoricalValue{String,UInt

4 4.6 3.1 1.5 0.2 CategoricalValue{String,UInt

5 5.0 3.6 1.4 0.2 CategoricalValue{String,UInt

6 5.4 3.9 1.7 0.4 CategoricalValue{String,UInt

7 4.6 3.4 1.4 0.3 CategoricalValue{String,UInt

8 5.0 3.4 1.5 0.2 CategoricalValue{String,UInt

9 4.4 2.9 1.4 0.2 CategoricalValue{String,UInt

10 4.9 3.1 1.5 0.1 CategoricalValue{String,UInt

more

150 5.9 3.0 5.1 1.8 CategoricalValue{String,UInt

⋅ iris = dataset("datasets", "iris")

features = 
4×150 Array{Float64,2}:
5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 … 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.7 3.2 3.3 3.0 2.5 3.0 3.4 3.0
1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 5.1 5.9 5.7 5.2 5.0 5.2 5.4 5.1
0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 1.9 2.3 2.5 2.3 1.9 2.0 2.3 1.8
⋅ features = collect(Matrix(iris[:,1:4])')

res = 
KmeansResult{Array{Float64,2},Float64,Int64}(4×3 Array{Float64,2}: , Int64[2, 2, 2,
5.90161 5.006 6.85
2.74839 3.428 3.07368
4.39355 1.462 5.74211
1.43387 0.246 2.07105

⋅ res = kmeans(features, 3)

localhost:1234/edit?id=a1590a70-4b0c-11eb-378a-67415e11ed15 1/4
2020/12/31 🎈 KMeans.jl ⚡ Pluto.jl ⚡

⋅ scatter(iris.PetalLength, iris.PetalWidth,marker_z = res.assignments,


color=:lightrainbow, legend = false)

X = 5×1000 Array{Float64,2}:
0.425544 0.61718 0.573201 … 0.763786 0.708567 0.948852 0.115165
0.591705 0.987675 0.0810505 0.237321 0.182992 0.710342 0.957146
0.00442796 0.963646 0.656056 0.745468 0.137074 0.682515 0.132817
0.159276 0.0442401 0.736157 0.0188291 0.855469 0.950143 0.755111
0.958077 0.96841 0.53596 0.145513 0.670037 0.812596 0.841391
⋅ X = rand(5, 1000)

R = 
KmeansResult{Array{Float64,2},Float64,Int64}(5×15 Array{Float64,2}:
0.776648 0.741381 0.721454 0.19869 …
0.302897 0.690129 0.317147 0.661161
0.672359 0.243211 0.793183 0.589928
0.752583 0.739094 0.321394 0.59443
0.690158 0.723916 0.246351 0.204999

⋅ R = kmeans(X, 15 ;maxiter = 300, display=:iter)

true
⋅ nclusters(R) == 15

M = 5×15 Array{Float64,2}:
0.776648 0.741381 0.721454 0.19869 … 0.212292 0.78157 0.253372 0.274885
0.302897 0.690129 0.317147 0.661161 0.345947 0.249006 0.214296 0.759596
0.672359 0.243211 0.793183 0.589928 0.725917 0.223315 0.226449 0.215397
0.752583 0.739094 0.321394 0.59443 0.223952 0.608264 0.687379 0.192026
0.690158 0.723916 0.246351 0.204999 0.760524 0.237421 0.640363 0.478572
⋅ M = R.centers

kmeans! (generic function with 1 method)


⋅ function kmeans!(x, k; maxiters = 100, tol = 1e-5)
localhost:1234/edit?id=a1590a70-4b0c-11eb-378a-67415e11ed15 2/4
2020/12/31 🎈 KMeans.jl ⚡ Pluto.jl ⚡
⋅ x = collect(eachrow(x))
⋅ N = length(x)
⋅ n = length(x[1])
⋅ dist = zeros(N) #khoang cach den tam minh chon gan nhat
⋅ reps = [zeros(n) for i=1:k] # chua cac tam chon

⋅ assignment = [rand(1:k) for i in 1:N]
⋅ JPre = Inf
⋅ for iter = 1:maxiters
⋅ #diem lam tam cua cum j la tring binh cua cac diem trong cum j
⋅ for j = 1:k
⋅ group = [i for i =1:N if assignment[i] == j]
⋅ reps = sum(x[group])/length(group)
⋅ end
⋅ #voi moi diem, tinh khoang cach giua diem do va tam cum gan nhat, gan vao
nhom do
⋅ for i = 1:N
⋅           (distances[i], assignment[i]) = findmin([norm(x[i] - reps[j]) for j =
1:k])
⋅        end
⋅ #tinh ham muc tieu
⋅ J = norm(dist)^2/N
⋅ println("Iteration ", iter, ": Jclust = ", J, ".")

⋅ #Thuat toan dung neu J khong giam
⋅        if iter > 1 && abs(J - Jprevious) < tol * J
⋅            return assignment, reps
⋅        end

⋅        Jprevious = J
⋅    end
⋅ end

MethodError: no method matching zero(::Type{SubArray{Float64,1,Array{Float64,2},Tup


Closest candidates are:
zero(!Matched::Type{Missing}) at missing.jl:103
zero(!Matched::Type{Dates.Time}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdl
zero(!Matched::Type{Dates.DateTime}) at D:\buildbot\worker\package_win64\build\usr\share\julia\
...

1. reduce_empty(::typeof(+),
::Type{SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64
2. reduce_empty(::typeof(Base.add_sum),
::Type{SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64
3. mapreduce_empty(::typeof(identity), ::Function, ::Type{T} where
T) @ reduce.jl:343
4. reduce_empty(::Base.MappingRF{typeof(identity),typeof(Base.add_sum)},
::Type{SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64
5. reduce_empty_iter @ reduce.jl:355 [inlined]
6. mapreduce_empty_iter(::Function, ::Function,
::Array{SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int6
::Base.HasEltype) @ reduce.jl:351
7. _mapreduce(::typeof(identity), ::typeof(Base.add_sum), ::IndexLinear,
::Array{SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int6
8. _mapreduce_dim @ reducedim.jl:318 [inlined]
9. #mapreduce#620 @ reducedim.jl:310 [inlined]
10. mapreduce @ reducedim.jl:310 [inlined]
11. _sum @ reducedim.jl:727 [inlined]
12. _sum @ reducedim.jl:726 [inlined]
13. #sum#627 @ reducedim.jl:722 [inlined]
14. sum(::Array{SubArray{Float64,1,Array{Float64,2},Tuple{Int64,Base.Slice{Base.OneTo{
15. #kmeans!#1(::Int64, ::Float64, ::typeof(Main.workspace88.kmeans!),
::Array{Float64,2}, ::Int64) @ Other: 14
16. top-level scope @ Local: 1 [inlined]

localhost:1234/edit?id=a1590a70-4b0c-11eb-378a-67415e11ed15 3/4
2020/12/31 🎈 KMeans.jl ⚡ Pluto.jl ⚡
⋅ result, fin = kmeans!(X, 15; maxiters = 300, tol = 1e-5)

localhost:1234/edit?id=a1590a70-4b0c-11eb-378a-67415e11ed15 4/4

You might also like