예제 #1
0
import numpy as np
import decision_trees as dt

#Training Set 1:
X_train = np.array([ [0,1], [0,0], [1,0], [0,0], [1,1] ])
Y_train = np.array([ [1], [0], [0], [0], [1] ])
#Validation Set 1:
X_val = np.array([ [0,0], [0,1], [1,0], [1,1] ])
Y_val = np.array([ [0], [1], [0], [1] ])
#Testing Set 1:
X = np.array([ [0,0], [0,1], [1,0], [1,1] ])
Y = np.array([ [1], [1], [0], [1] ])

DT = dt.DT_train_binary(X_train, Y_train, 2)
#DT.print_tree()
test_acc = dt.DT_test_binary(X, Y, DT)
print(test_acc)

DT = dt.DT_train_binary_best(X_train, Y_train, X_val, Y_val)
#DT.print_tree()
test_acc = dt.DT_test_binary(X, Y, DT)
print(test_acc)

print()

#Training Set 2:
X_train = np.array([ [0,1,0,0], [0,0,0,1], [1,0,0,0], [0,0,1,1], [1,1,0,1], [1,1,0,0], [1,0,0,1], [0,1,0,1], [0,1,0,0] ])
Y_train = np.array([ [0], [1], [0], [0], [1], [0], [1], [1], [1] ])
#Validation Set 1:
X_val = np.array([ [1,0,0,0], [0,0,1,1], [1,1,0,1], [1,1,0,0], [1,0,0,1], [0,1,0,0] ])
Y_val = np.array([ [0], [0], [1], [0], [1], [1] ])
예제 #2
0
RealX = np.array([[4.8, 3.4, 1.9, 0.2], [5, 3, 1.6, 1.2], [5, 3.4, 1.6, 0.2],
                  [5.2, 3.5, 1.5, 0.2], [5.2, 3.4, 1.4, 0.2],
                  [4.7, 3.2, 1.6, 0.2], [4.8, 3.1, 1.6, 0.2],
                  [5.4, 3.4, 1.5, 0.4], [7, 3.2, 4.7,
                                         1.4], [6.4, 3.2, 4.7, 1.5],
                  [6.9, 3.1, 4.9, 1.5], [5.5, 2.3, 4, 1.3],
                  [6.5, 2.8, 4.6, 1.5], [5.7, 2.8, 4.5, 1.3],
                  [6.3, 3.3, 4.7, 1.6], [4.9, 2.4, 3.3, 1]])
RealY = np.array([[1], [1], [1], [1], [1], [1], [1], [1], [0], [0], [0], [0],
                  [0], [0], [0], [0]])

print()
print()
print('WRITE UP 1')
max_depth_1 = -1
DT_train_1 = dt.DT_train_binary(trainingX1, trainingY1, max_depth_1)
DT_train_1.toString()
train_acc_1 = dt.DT_test_binary(testingX1, testingY1, DT_train_1)
print('TRAIN1', train_acc_1)

DT_best_1 = dt.DT_train_binary_best(trainingX1, trainingY1, valX1, valY1)
DT_best_1.toString()
best_acc_1 = dt.DT_test_binary(testingX1, testingY1, DT_best_1)
print('BEST1', best_acc_1)

DT_train_2 = dt.DT_train_binary(trainingX2, trainingY2, max_depth_1)
DT_train_2.toString()
train_acc_2 = dt.DT_test_binary(testingX2, testingY2, DT_train_2)
print('TRAIN2', train_acc_2)

DT_best_2 = dt.DT_train_binary_best(trainingX2, trainingY2, valX2, valY2)
예제 #3
0
    Y = []
    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)
예제 #4
0
import numpy as np
import decision_trees as dt

# Default max depth value
max_depth = 2
# Training Set 1
X_1 = np.array([[0, 1], [0, 0], [1, 0], [0, 0], [1, 1]])
Y_1 = np.array([[1], [0], [0], [0], [1]])
# Validation Set 1
X_val_1 = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
Y_val_1 = np.array([[0], [1], [0], [1]])
# Test Set 1
X_test_1 = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
Y_test_1 = np.array([[1], [1], [0], [1]])

tree = dt.DT_train_binary(X_1, Y_1, max_depth)
test = dt.DT_test_binary(X_test_1, Y_test_1, tree)

tree_best = dt.DT_train_binary_best(X_1, Y_1, X_val_1, Y_val_1)
test_best = dt.DT_test_binary(X_test_1, Y_test_1, tree_best)

print("Training Set 1:")
print("Binary Test (max_depth = 2): ", test)
print("Binary Test (Best Depth): ", test_best)
print("Decision Tree (max_depth = 2): ", tree)
print("Decision Tree (Best Depth): ", tree_best)

# Training Set 2
X_2 = np.array([[0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1],
                [1, 1, 0, 1], [1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 0, 1],
                [0, 1, 0, 0]])
예제 #5
0
	YT = np.array([[1], [1], [0], [0], [1], [0], [1], [1], [1]])

	max_depth = -1
	#DT = dt.DT_train_binary_best(X,Y,XV,YV)
	#print(DT)
	#DT = dt.DT_train_binary(X,Y,max_depth)
	#test_acc = dt.DT_test_binary(XT,YT,DT)
	#print(test_acc)

	dinnerX = np.array([[1, 1, 1, 0, 1, 1, 0], [0, 0, 1, 1, 0, 1, 1], [0, 1, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 1, 1, 1], [1, 1, 0, 1, 1, 0, 1], [1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 1, 0, 1, 1]])
	dinnerY = np.array([[1], [1], [0], [1], [0], [1], [1], [0]])

	dinnerTX = np.array([[0, 1, 1, 1, 0, 1, 0], [0, 1, 1, 1, 0, 0, 1], [1, 0, 0, 1, 1, 0, 0]])
	dinnerTY = np.array([[0], [1], [0]])

	DT1 = dt.DT_train_binary(dinnerX[:5],dinnerY[:5],5)
	#print(dt.DT_test_binary(dinnerTX,dinnerTY,DT1))
	DT2 = dt.DT_train_binary(dinnerX[-5:],dinnerY[-5:],5)
	#print(dt.DT_test_binary(dinnerTX,dinnerTY,DT2))
	DT3 = dt.DT_train_binary(dinnerX[1:6],dinnerY[1:6],5)
	#print(dt.DT_test_binary(dinnerTX,dinnerTY,DT3))

	real = np.array([[4.8, 3.4, 1.9, 0.2], [5, 3, 1.6, 1.2], [5, 3.4, 1.6, 0.2], [5.2, 3.5, 1.5, 0.2], [5.2, 3.4, 1.4, 0.2], [4.7, 3.2, 1.6, 0.2], [4.8, 3.1, 1.6, 0.2], [5.4, 3.4, 1.5, 0.4], [7, 3.2, 4.7, 1.4], [6.4, 3.2, 4.7, 1.5], [6.9, 3.1, 4.9, 1.5], [5.5, 2.3, 4, 1.3], [6.5, 2.8, 4.6, 1.5], [5.7, 2.8, 4.5, 1.3], [6.3, 3.3, 4.7, 1.6], [4.9, 2.4, 3.3, 1]])
	realL = np.array([[1], [1], [1], [1], [1], [1], [1], [1], [0], [0], [0], [0], [0], [0], [0], [0]])
	DT = dt.DT_train_real(real,realL,-1)

	exit(1)



예제 #6
0
import decision_trees as dt
import numpy as np

# Test Data
# X = np.array([[0,1], [0,0], [1,0], [0,0], [1,1]]) # Training Set 1
# Y = np.array([[1], [0], [0], [0], [1]]) # Training labels 1

X = np.array([[1, 1, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 0, 0, 1],
              [0, 0, 1, 1], [0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 1, 0],
              [1, 1, 1, 0], [0, 0, 1, 1]])
Y = np.array([[0], [1], [1], [0], [0], [1], [0], [0], [1], [0]])

DT = dt.DT_train_binary(X, Y, 1)
test_acc = dt.DT_test_binary(X, Y, DT)
print("ACC:", test_acc)
예제 #7
0
first_five = [training_one[i] for i in range(5)]
label_first = [label_one[i] for i in range(5)]
middle_five = [training_one[i] for i in range(2, 7)]
label_middle = [label_one[i] for i in range(2, 7)]
last_five = [training_one[i] for i in range(3, 8)]
label_last = [label_one[i] for i in range(3, 8)]
"""
first_tree = dt.DT_train_binary(first_five, label_first, 5)
acc = dt.DT_test_binary(first_five, label_first, first_tree)
#print(acc)

middle_tree = dt.DT_train_binary(middle_five, label_middle, 5)
acc = dt.DT_test_binary(middle_five, label_middle, middle_tree)
#print(acc)
"""
last_tree = dt.DT_train_binary(last_five, label_last, 5)
acc = dt.DT_test_binary(last_five, label_last, last_tree)
#print(acc)
"""
result = dt.DT_make_prediction(sample_1, first_tree)
print("result of sample 1 on first tree: " + str(result))
result = dt.DT_make_prediction(sample_2, first_tree)
print("result of sample 2 on first tree: " + str(result))
result = dt.DT_make_prediction(sample_3, first_tree)
print("result of sample 3 on first tree: " + str(result))
"""
"""
result = dt.DT_make_prediction(sample_1, middle_tree)
print("result of sample 1 on middle tree: " + str(result))
result = dt.DT_make_prediction(sample_2, middle_tree)
print("result of sample 2 on middle tree: " + str(result))