Machine Learning Foundations(NTU) 第一次作业
PLA
DATA: https://d396qusza40orc.cloudfront.net/ntumlone%2Fhw1%2Fhw1_15_train.dat
Each line of the data set contains one (xn,yn) with xn∈R4. T
he first 4 numbers of the line contains the components of xn orderly, the last number is yn.
Please initialize your algorithm with w=0 and take sign(0) as −1
Question 15:
Implement a version of PLA by visiting examples in the naive cycle using the
order of examples in the data set. Run the algorithm on the data set.
What is the number of updates before the algorithm halts?Question 16:
Implement a version of PLA by visiting examples in fixed, pre-determined random
cycles throughout the algorithm. Run the algorithm on the data set. Please repeat
your experiment for 2000 times, each with a different random seed. What is the average
number of updates before the algorithm halts?Question 17:
Implement a version of PLA by visiting examples in fixed, pre-determined random cycles
throughout the algorithm, while changing the update rule to be:
wt+1 = wt + alpha * yn(t)xn(t)
with alpha =0.5. Note that your PLA in the previous Question corresponds to alpha=1.
Please repeat your experiment for 2000 times, each with a different random seed.
What is the average number of updates before the algorithm halts?
import urllib2
import numpy as np
import random
# url = 'https://d396qusza40orc.cloudfront.net/ntumlone%2Fhw1%2Fhw1_15_train.dat'
# f = urllib2.urlopen(url)
# with open("hw1_15_train.dat", "wb") as code:
# code.write(f.read())
def train_PLA():
code = open("hw1_15_train.dat", "r")
lines = code.readlines()
xn = np.zeros((len(lines), 5)).astype(np.float)
yn = np.zeros((len(lines),)).astype(np.int)
learn_rate = 0.5
for i in range(0, len(lines)):