コード例 #1
0
import numpy as np
import matplotlib.pyplot as plt
import perceptron as p
import nearest_neighbors as knn
import clustering as km

# KNN TESTING
X_train = np.array( [[1,5], [2,6], [2,7], [3,7], [3,8], [4,8], [5,1], [5,9], [6,2], [7,2], [7,3], [8,3], [8,4], [9,5]] )
Y_train = np.array( [[-1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [1]] )

X_test = np.array( [[1,1], [2,1], [0,10], [10,10], [5,5], [3,10], [9,4], [6,2], [2,2], [8,7]] )
Y_test = np.array( [[1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1]] )

acc_K1 = knn.KNN_test(X_train, Y_train, X_test, Y_test, 1)
acc_K2 = knn.KNN_test(X_train, Y_train, X_test, Y_test, 3)
acc_K3 = knn.KNN_test(X_train, Y_train, X_test, Y_test, 5)
print("Accuracy K=1:",acc_K1)
print("Accuracy K=3:",acc_K2)
print("Accuracy K=5:",acc_K3)
print()

best_K = knn.choose_K(X_train, Y_train, X_test, Y_test)
print("Best K:",best_K,"\n")
print()

# K-MEANS TESTING

print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ First Part ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
X = np.array( [[0], [1], [2], [7], [8], [9], [12], [14], [15]] )
K = 2
C = km.K_Means(X, K)
コード例 #2
0
import numpy as np
import nearest_neighbors as nn

x = np.array([2, 2, 3])
y = np.array([4, 4, 5])
val = nn.distance(x, y)

X_train = np.array([[1, 5], [2, 6], [2, 7], [3, 7], [3, 8], [4, 8], [5, 1], [5, 9], [6, 2], [7, 2], [7, 3], [8, 3], [8, 4], [9, 5]])
Y_train = np.array([[-1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [1]])

X_test = np.array([[1, 1], [2, 1], [0, 10], [10, 10], [5, 5], [3, 10], [9, 4], [6, 2], [2, 2], [8, 7]])
Y_test = np.array([[1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1]])

acc = nn.KNN_test(X_train, Y_train, X_test, Y_test, 2)
k = nn.choose_K(X_train, Y_train, X_test, Y_test)
コード例 #3
0
                  [5, 1], [1, 9], [10, 3], [4, 1], [6, 6], [2, 2]])

#test
perX1 = np.array([[0, 1], [1, 0], [5, 4], [1, 1], [3, 3], [2, 4], [1, 6]])
perY1 = np.array([[1], [1], [-1], [1], [-1], [-1], [-1]])
#writeup
perX2 = np.array([[-2, 1], [1, 1], [1.5, -0.5], [-2, -1], [-1, -1.5], [2, -2]])
perY2 = np.array([[1], [1], [1], [-1], [-1], [-1]])

#test
#print('SELFTEST')
#print('ACC',nn.KNN_test(nnTrainX1, nnTrainY1, nnTrainX1, nnTrainY1, 1))

#writeup
print("K Nearest Neighbor")
nnAcc1 = nn.KNN_test(nnTrainX1, nnTrainY1, nnTestX1, nnTestY1, 1)
print("\tK=1", "Accuracy", nnAcc1)
nnAcc2 = nn.KNN_test(nnTrainX1, nnTrainY1, nnTestX1, nnTestY1, 3)
print("\tK=3", "Accuracy", nnAcc2)
nnAcc3 = nn.KNN_test(nnTrainX1, nnTrainY1, nnTestX1, nnTestY1, 5)
print("\tK=5", "Accuracy", nnAcc3)
#writeup
nnK = nn.choose_K(nnTrainX1, nnTrainY1, nnTestX1, nnTestY1)
print("\tChoose K:", nnK)
print("\tAccuracy with chosen K:",
      nn.KNN_test(nnTrainX1, nnTrainY1, nnTestX1, nnTestY1, nnK))
print()

print("Clustering")
#test
C1 = clu.K_Means(cluX1, 3)
コード例 #4
0
ファイル: KNN_test.py プロジェクト: YifengQ/Machine-Learning
import nearest_neighbors as NN
import numpy as np

print_stuff = True

if print_stuff:
    print("KNN tests:")
NN_X_train = np.array([[1, 5], [2, 6], [2, 7], [3, 7], [3, 8], [4, 8], [5, 1],
                       [5, 9], [6, 2], [7, 2], [7, 3], [8, 3], [8, 4], [9, 5]])
NN_Y_train = np.array([[-1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1],
                       [1], [-1], [1], [1]])

NN_test_X = np.array([[1, 1], [2, 1], [0, 10], [10, 10], [5, 5], [3, 10],
                      [9, 4], [6, 2], [2, 2], [8, 7]])
NN_test_Y = np.array([[1], [-1], [1], [-1], [1], [-1], [1], [-1], [1], [-1]])
self_test = NN.KNN_test(NN_X_train, NN_Y_train, NN_X_train, NN_Y_train, 1)
choose_k_self = NN.choose_K(NN_X_train, NN_Y_train, NN_X_train, NN_Y_train)
if print_stuff:
    print("KNN test on self =", self_test)
    print("KNN choose k on self =", choose_k_self)

choose_test = NN.choose_K(NN_X_train, NN_Y_train, NN_test_X, NN_test_Y)
print()
if print_stuff:
    print("choose_k on test ", choose_test)

i = 9
test_test = NN.KNN_test(NN_X_train, NN_Y_train, NN_test_X, NN_test_Y, i)
if print_stuff:
    print("KNN k=", i, "test on test accuracy =", test_test)
print()
コード例 #5
0
ファイル: test_script.py プロジェクト: jjapp/CS622P1
    for item in x_str:
        temp = [float(x) for x in item.split(',')]
        X.append(temp)
    if len(y_str) > 0:
        for item in y_str:
            temp = int(item)
            Y.append(temp)
    X = np.array(X)
    Y = np.array(Y)
    return X, Y


X, Y = load_data("data_1.txt")
max_depth = 3
DT = dt.DT_train_binary(X, Y, max_depth)
test_acc = dt.DT_test_binary(X, Y, DT)
print("DT:", test_acc)

X, Y = load_data("data_4.txt")
acc = nn.KNN_test(X, Y, X, Y, 1)
print("KNN:", acc)

X, Y = load_data("data_5.txt")
mu = np.array([[1], [5]])
mu = kmeans.K_Means(X, 2, mu)
print("KMeans:", mu)

X, Y = load_data("data_6.txt")
mu = kmeans.K_Means(X, 2, [])
print("KMeans:", mu)