예제 #1
0
# TODO: Test kernel_perceptron_train function, defined in perceptron.py
# TODO: Run experiments outlined in HW4 PDF

#1
# a0 = np.zeros(XTrain.shape[1])
# a1 = pe.perceptron_train(a0,XTrain,yTrain,10)
# error_num = 0
# for i in range(0,XTest.shape[0]):
#     y_pre = pe.perceptron_predict(a1,XTest[i])
#     y_real = yTest[i]
#     if y_real != y_pre:
#         error_num += 1
# error_rate = error_num * 1.0 / XTest.shape[0]
# print error_rate

#2
for sigma in (0.01, 0.1, 1, 10, 100, 1000):
    a0 = np.zeros(XTrain.shape[0])
    a1 = pe.kernel_perceptron_train(a0, XTrain, yTrain, 2, sigma)
    error_num = 0
    for i in range(0, XTest.shape[0]):
        y1 = pe.kernel_perceptron_predict(a1, XTrain, yTrain, XTest[i], sigma)
        y2 = yTest[i]
        if y1 != y2:
            error_num += 1
    error_rate = error_num * 1.0 / XTest.shape[0]
    print sigma, error_rate

#3
예제 #2
0
# TODO: Test perceptron_train function, defined in perceptron.py
w = np.zeros(XTrain.shape[1])
w = perceptron.perceptron_train(w, XTrain, yTrain, 10)

# TODO: Test RBF_kernel function, defined in perceptron.py
K = perceptron.RBF_kernel(XTrain[0:5], XTrain[0:5], 10.0)

# TODO: Test kernel_perceptron_predict function, defined in perceptron.py
a = np.zeros(XTrain.shape[0])
yPred = perceptron.kernel_perceptron_predict(a, XTrain, yTrain, XTrain[0],
                                             10.0)

# TODO: Test kernel_perceptron_train function, defined in perceptron.py
a = np.zeros(XTrain.shape[0])
a0 = perceptron.kernel_perceptron_train(a, XTrain, yTrain, 5, 100)

# TODO: Run experiments outlined in HW4 PDF
w = np.zeros(XTrain.shape[1])
w = perceptron.perceptron_train(w, XTrain, yTrain, 10)
yPred = np.zeros(len(yTest))
for i in range(len(XTest)):
    yPred[i] = perceptron.perceptron_predict(w, XTest[i])
np.sum(np.abs(yTest - yPred)) / (2.0 * len(XTest))

for sigma in [0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]:
    a = np.zeros(XTrain.shape[0])
    a0 = perceptron.kernel_perceptron_train(a, XTrain, yTrain, 2, sigma)
    yPred = np.zeros(len(yTest))
    for i in range(len(XTest)):
        yPred[i] = perceptron.kernel_perceptron_predict(
예제 #3
0
weight = np.zeros(len(XTrain[0]))
#weight[0] = -5
#weight[1] = -2
#tess = np.tile(weight, (5, 1))
#print tess
alpha = np.zeros(len(XTrain))
#after_w = pp.perceptron_train(weight, XTrain, yTrain, 10)
#count = 0
#for i in range(len(yTest)):
#    ypred = pp.perceptron_predict(after_w, XTest[i])
#    if ypred != yTest[i]:
#         count = count + 1
#print count / float(len(yTest))
#pp.perceptron_train(after_w, XTest, yTest, 1)
#print pp.kernel_perceptron_predict(alpha, XTrain, yTrain, XTrain[0], 2)
alpha_after = pp.kernel_perceptron_train(alpha, XTrain, yTrain, 2, 0.1)
count = 0
for i in range(len(XTest)):
    ypred = pp.kernel_perceptron_predict(alpha_after, XTrain, yTrain, XTest[i],
                                         0.1)
    if ypred != yTest[i]:
        count = count + 1
print count / float(len(yTest))
pp.kernel_perceptron_train(alpha_after, XTest, yTest, 1, 1)
#pp.RBF_kernel(XTrain, XTrain, 2)

# Visualize the image
#idx = 0
#datapoint = XTrain[idx, 1:]
#plt.imshow(datapoint.reshape((28,28), order = 'F'), cmap='gray')
#plt.show()