using PyPlot using NtToolBox A = readdlm("iris_dataset.csv", ','); A = A[randperm(size(A,1)),:]; X = A[:,1:end-1] y = Int.(A[:,end]) y = y-minimum(y)+1; n,p = size(X) k = maximum(y); Xm = X -> X-repeat(mean(X,1), outer=(size(X,1), 1)) Cov = X -> Xm(X)'*Xm(X); imshow(Cov(X), extent=[0, 1, 0, 1], cmap = get_cmap("jet")); U,D,V = svd(Xm(X),thin=true); Z = Xm(X) * V; length(D) figure(figsize = (5,3)) plot(D, ".-", linewidth= 2, markersize= 20); figure(figsize=(7,3)) col = [ [1 0 0]; [0 1 0]; [0 0 1]; [0 0 0]; [0 1 1]; [1 0 1]; [1 1 0]; [1 .5 .5]; [.5 1 .5]; [.5 .5 1] ]'; ms = 25; lgd = []; for i=1:min(k,size(col,2)) I = find(y.==i) plot(Z[I,1], Z[I,2], ".", markersize= ms, c=col[:,Int(i)], markersize=ms, label=i); append!(lgd,i); end axis("tight"); axis("equal"); box("on"); legend(); #figure(figsize=(10,5)) for i=1:k I = find(y.==i) plot3D(Z[I,1], Z[I,2], Z[I,3], ".", c=col[:,Int(i)], markersize=ms, label=Int(i)) end gca()[:view_init](50, 250) axis("equal"); axis("tight"); gca()[:grid](false); legend(loc="best"); n0 = Int(round(.5*n)); n1 = n-n0 X0 = X[1:n0,:]; y0 = y[1:n0] X1 = X[n0+1:end,:]; y1 = y[n0+1:end]; distmat = (X,Z) -> broadcast(+,sum(X'.*X',1)',sum(Z'.*Z',1))-2*(X*Z'); i = 1; x = X1[i,:]'; # could be any point D = distmat(X0,x); I = sortperm(D[:]) ys = Int.(y[I]); function custom_hist(h) return [sum(h .== i) for i in 1:k] end R = 5 h = custom_hist(ys[1:R,:]) h = h / R c = indmax(h); print("c(x)=",c," [true class=",Int(y1[i]), "]\n"); figure(figsize=(8,6)) Rlist = round([.05 .1 .5 1]*n0); # [5 50 100] clf() for i=1:length(Rlist) R = Int(Rlist[i]); h = custom_hist(ys[1:R,:]) h = h / R; subplot(length(Rlist),1,i); bar(1:k,h); axis([0.5 k+.5 0 1]'); end tight_layout() include("NtSolutions/ml_1_pca_nn/exo1.jl"); # Insert your code here. include("NtSolutions/ml_1_pca_nn/exo2.jl"); # Insert your code here. if k>=4 ksvg = k; Xsvg = X; ysvg = y k = 3 I = find(y.<=k) X = X(I,:); y = y(I) n = length(I) end U,D,V = svd(Xm(X),thin=true) Z = Xm(X) * V; I = randperm(n); I = I[1:k] C = X[I,:]; D = distmat(X,C) yb = mapslices(indmin,D, 2); for i=1:k I = find(yb.==i); plot(Z[I,1], Z[I,2], ".", c=col[:,i], ms=12)#, 'Color', col(:,i), 'MarkerSize', 25); end CV = (C-repeat(mean(X,1), outer=(k,1)))*V; for i=1:k plot(CV[i,1], CV[i,2], "o", c= col[:,i]*0.9,ms=20)#, markedgecolor ='k'); end axis("tight"); axis("equal"); axis("off"); for l=1:k C[l,:] = mean(X[find(yb.==l),:], 1) end include("NtSolutions/ml_1_pca_nn/exo3.jl"); # Insert your code here. for l=1:k I = find(yb.==l) h = custom_hist(y[I]); h = h/sum(h) subplot(k,1,l) bar(1:k,h) axis([0.5, k+.5, 0, 1]) # set(gca, 'FontSize', 10) end tight_layout() include("NtSolutions/ml_1_pca_nn/exo4.jl"); # Insert your code here.