Beispiel #1
0
plt.show()
'''

# TODO: Test perceptron_predict function, defined in perceptron.py
yPred = perceptron.perceptron_predict(w, XTrain[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])
# 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
Beispiel #3
0
#     yhat = perceptron.perceptron_predict(w0, XTest[i])
#     if yhat != yTest[i]:
#         err += 1
# print float(err)/len(yTest)

#print w0

# TODO: Test RBF_kernel function, defined in perceptron.py
#sigma = 0.1
#print perceptron.RBF_kernel(XTrain[0], XTrain[1], sigma)

# TODO: Test kernel_perceptron_predict function, defined in perceptron.py
# x = XTrain[0]
# sigma = 0.1
# a = np.zeros((n,1))
#print perceptron.kernel_perceptron_predict(a, XTrain, yTrain, x, sigma)

# TODO: Test kernel_perceptron_train function, defined in perceptron.py
a0 = [0 for i in range(0, len(XTrain))]
sigma = 100
a0 = perceptron.kernel_perceptron_train(a0, XTrain, yTrain, 2, sigma)
err = 0
for i in range(0, len(yTest)):
    yhat = perceptron.kernel_perceptron_predict(a0, XTrain, yTrain, XTest[i],
                                                sigma)
    if yhat != yTest[i]:
        err += 1
print float(err) / len(yTest)

# TODO: Run experiments outlined in HW4 PDF
Beispiel #4
0
#get the number of features
d = XTrain.shape[1]
n = XTrain.shape[0]
m = XTest.shape[0]

#experiment 1, original perceptron
w0 = np.zeros((d, 1))
w = perceptron.perceptron_train(w0, XTrain, yTrain, 10)
rate1 = perceptron.perceptron_test(w, XTest, yTest)
print(rate1)
#result: error rate: 0.03833

#experiment 2, kernel perceptron
sigmaList = [0.01, 0.1, 1, 10, 100, 1000]

for sigma in sigmaList:

    error_case = 0
    a0 = np.zeros((n, 1))
    a = perceptron.kernel_perceptron_train(a0, XTrain, yTrain, 2, sigma)

    for i in range(0, m):
        yHat = perceptron.kernel_perceptron_predict(a, XTrain, yTrain,
                                                    XTest[i, :], sigma)
        if yHat != yTest[i, 0]:
            error_case += 1

    error_rate = float(error_case) / m
    print("The error rate when sigma= ", sigma, " is: ", error_rate)
#result: error rate:[0.49,0.49,0.075,0.05833,0.08833,0.51]
#when sigma=10, we have the lowest test error
#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()

# TODO: Test perceptron_predict function, defined in perceptron.py

# TODO: Test perceptron_train function, defined in perceptron.py