#Demonstration of semi-supervised learning import numpy as np import graphlearning as gl import matplotlib.pyplot as plt import sklearn.datasets as datasets #data n = 500 X, L = datasets.make_moons(n_samples=n, noise=0.1) #Build graph k = 10 I, J, D = gl.knnsearch(X, k) W = gl.weight_matrix(I, J, D, k) #Randomly choose labels m = 5 #5 labels per class ind = gl.randomize_labels(L, m) #indices of labeled points #Semi-supervised learning l = gl.graph_ssl(W, ind, L[ind], method='laplace') #Compute accuracy acc = gl.accuracy(l, L, m) print("Accuracy=%f" % acc) #Plot result (red points are labels) plt.scatter(X[:, 0], X[:, 1], c=l) plt.scatter(X[ind, 0], X[ind, 1], c='r') plt.show()
k = 130 I, J, D = construct_graph(cleandata, k) print(I) print(J) print(D) W = gl.weight_matrix(I, J, D, k) #Randomly choose labels m = 140 #5 labels per class ind = gl.randomize_labels(label, m) #indices of labeled points #Semi-supervised learning l = gl.graph_ssl(W, ind, label[ind], method='poisson') #Compute accuracy acc = gl.accuracy(l, label, len(ind)) print("Accuracy=%f" % acc) ''' #xtrain,ytrain is training data, xtest contains label of bones, be careful xtrain,xtest,ytrain,ytest=split(cleandata,split_percentage) print(xtrain[0]) print(xtest[0]) #clf is the trained classifier clf=train_classifier(xtrain,ytrain) #prediction is a list in form of [bone label, prediction1, prediction2 ...] prediction=curve_prediction(xtest,clf) print(prediction) #Result is a list in form of [bone label, prediction] Result=voting(prediction) #print(Result)
print(cleandata[0]) print(label[500:1000]) #Build graph k = 500 I, J, D = gl.knnsearch(cleandata, k) W = gl.weight_matrix(I, J, D, k) #Randomly choose labels m = 5 #5 labels per class ind = gl.randomize_labels(label, m) #indices of labeled points #Semi-supervised learning l = gl.graph_ssl(W, ind, label[ind], method='Laplace') #Compute accuracy acc = gl.accuracy(l, label, m) print("Accuracy=%f" % acc) ''' #xtrain,ytrain is training data, xtest contains label of bones, be careful xtrain,xtest,ytrain,ytest=split(cleandata,split_percentage) print(xtrain[0]) print(xtest[0]) #clf is the trained classifier clf=train_classifier(xtrain,ytrain) #prediction is a list in form of [bone label, prediction1, prediction2 ...] prediction=curve_prediction(xtest,clf) print(prediction) #Result is a list in form of [bone label, prediction] Result=voting(prediction) #print(Result)
knn_mask = utils.create_directed_KNN_mask(D=D.copy(), knn_param=k, D_type='distance') sigma = np.mean(D[:, knn_mask[:, -1]]) / 2 G = np.exp(-(D**2) / (sigma**2)) W_knn = knn_graph(G, knn_mask, k, 1e-10, directed=True) W_nnk = nnk_graph(G, knn_mask, k, 1e-10, directed=True) # Randomly choose labels m = 2 # 5 labels per class ind = gl.randomize_labels(L, m) # indices of labeled points # Semi-supervised learning l_knn = gl.graph_ssl(W_knn, ind, L[ind], method='poissonmbo', symmetrize=False) knn_acc = gl.accuracy(l_knn, L, m) l_knn[ind] = 2 l_nnk = gl.graph_ssl(W_nnk, ind, L[ind], method='poissonmbo', symmetrize=False) nnk_acc = gl.accuracy(l_nnk, L, m) l_nnk[ind] = 2 # Plot result (red points are labels) fig = plt.figure(figsize=(12, 6)) ax = plt.subplot(1, 2, 1) utils.plot_graph(W_knn, X, vertex_color=l_knn, ax=ax) ax.set_title(f"KNN graph, Acc:{knn_acc}") ax = plt.subplot(1, 2, 2) utils.plot_graph(W_nnk, X, vertex_color=l_nnk, ax=ax) ax.set_title(f"NNK graph, Acc:{nnk_acc}")