matlab实现pla和pocket,林轩田机器学习基石课程 - Pocket PLA算法 python实现

作业1:

Q1. Run the pocket algorithm with a total of 50 updates on D, and verify the performance of w pocket using the test set.

Please repeat your experiment for 2000 times, each with a different random seed.

What is the average error rate on the test set? Plot a histogram to show error rate versus frequency.

# calculate error count

def calError(self, X, Y, W):

score = np.dot(X, W)

Y_pred = np.ones_like(Y)

Y_pred[score < 0] = -1

err_cnt = np.sum(Y_pred != Y)

return err_cnt

def pocket_pla_1(self, X_train, Y_train, X_test, Y_test):

Iteration = 2000 # number of iteration

Update = 50

Errors = [] # list store error rate every iteration

for iter in range(Iteration):

np.random.seed(iter) # set random seed, different by iteration

permutation = np.random.permutation(X_train.shape[0]) # random select index

X_train = X_train[permutation] # random order X_train

Y_train = Y_train[permutation] # random order Y_train, as the same as X_train

# look through the 50 iterations

W = np.zeros(X_train.shape[1]) # weights initialization

min_err = self.calError(X_train, Y_train, W) # set initial W can make minimal error

for i in range(Update):

score = np.dot(X_train[i, :], W) # score

if score * Y_train[i] <= 0: # classification error

tmp = W + np.dot(X_train[i, :].T, Y_train[i]) # new tmp, wait to decide replace W

tmp_err = self.calError(X_train, Y_train, tmp) # calculate new error

if tmp_err < min_err:

W = tmp # update W

min_err = tmp_err # update min_err

# get W to test data

Y_pred_test = np.dot(X_test, W) # calculate score

Y_pred_test[Y_pred_test > 0] = 1 # positive

Y_pred_test[Y_pred_test < 0] = -1 # negative

error = np.mean(Y_pred_test != Y_test)

Errors.append(error) # store error to list

# mean of errors

error_mean = np.mean(Errors)

return error_mean

作业2

Q2. Modify your algorithm to return w50w50 (the PLA vector after 50 updates) instead of w (the pocket vector) after 50 updates. Run the modified algorithm on D, and verify the performance using the test set. Please repeat your experiment for 2000 times, each with a different random seed. What is the average error rate on the test set? Plot a histogram to show error rate versus frequency. Compare your result to the previous problem and briefly discuss your findings.

def pocket_pla_2(self, X_train, Y_train, X_test, Y_test):

Iteration = 2000 # number of iteration

Update = 50

Errors = [] # list store error rate every iteration

for iter in range(Iteration):

np.random.seed(iter) # set random seed, different by iteration

permutation = np.random.permutation(X_train.shape[0]) # random select index

X_train = X_train[permutation] # random order X_train

Y_train = Y_train[permutation] # random order Y_train, as the same as X_train

# look through the 50 iterations

W = np.zeros(X_train.shape[1]) # weights initialization

for i in range(Update):

score = np.dot(X_train[i, :], W) # score

if score * Y_train[i] <= 0: # classification error

W = W + np.dot(X_train[i, :].T, Y_train[i])

# get W to test data

Y_pred_test = np.dot(X_test, W) # calculate score

Y_pred_test[Y_pred_test > 0] = 1 # positive

Y_pred_test[Y_pred_test < 0] = -1 # negative

error = np.mean(Y_pred_test != Y_test)

Errors.append(error) # store error to list

# mean of error

error_mean = np.mean(Errors)

return error_mean

作业3

Q3. Modify your algorithm in Problem 1 to run for 100 updates instead of 50, and verify the performance of w pocket using the test set.

Please repeat your experiment for 2000 times, each with a different random seed. What is the average error rate on the test set? Plot a histogram to show error rate versus frequency. Compare your result to Problem 18 and briefly discuss your findings.

def pocket_pla_3(self, X_train, Y_train, X_test, Y_test):

Iteration = 2000 # number of iteration

Update = 100

Errors = [] # list store error rate every iteration

for iter in range(Iteration):

np.random.seed(iter) # set random seed, different by iteration

permutation = np.random.permutation(X_train.shape[0]) # random select index

X_train = X_train[permutation] # random order X_train

Y_train = Y_train[permutation] # random order Y_train, as the same as X_train

# look through the 50 iterations

W = np.zeros(X_train.shape[1]) # weights initialization

min_err = self.calError(X_train, Y_train, W) # set initial W can make minimal error

for i in range(Update):

score = np.dot(X_train[i, :], W) # score

if score * Y_train[i] <= 0: # classification error

tmp = W + np.dot(X_train[i, :].T, Y_train[i]) # new tmp, wait to decide replace W

tmp_err = self.calError(X_train, Y_train, tmp) # calculate new error

if tmp_err < min_err:

W = tmp # update W

min_err = tmp_err # update min_err

# get W to test data

Y_pred_test = np.dot(X_test, W) # calculate score

Y_pred_test[Y_pred_test > 0] = 1 # positive

Y_pred_test[Y_pred_test < 0] = -1 # negative

error = np.mean(Y_pred_test != Y_test)

Errors.append(error) # store error to list

# mean of errors

error_mean = np.mean(Errors)

return error_mean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值