機器學習基石作業1


第一題很明顯:(1)質數有很明確的定義,所以辨別質數並不需要ML;(2)在課件中反復提到的信用卡發放問題;(3)計算重力加

速度有明確的方法,不需要ML;(4)在繁忙十字路口最優交通紅綠燈的周期,由於每端時間的車流量很難去預測,所以需要ML去自

己學習控制周期;(5)根據年齡推薦醫學檢驗,無法通過明確定義程序直接判斷,電腦需要自己ML得出值得推薦的結果。

 

 

下棋時data是一筆一筆喂給電腦,每一步沒有明確的label,題目中也提到了feedback,也就是獎懲,所以很明顯是reinforcement

learning

 

 

依題意書並沒有label,和課件中的例子articles=>topic,一樣,很明顯是unsupervised learning

 

 

依題意是binary classification,每個x有明確的label,所以是supervised learning

 

 

評估葯物的療效,由於每次實驗很貴,也就是標注每個x很貴,所以我們的data只有一部分有label,只有當機器不確定的情況,我們才

去做實驗獲得label,所以這是active learning

 

 

Off-Training-Set error即不在訓練集中的測試錯誤,所以我們求的是N+1~N+L的error,依題意只有當

1~N+L中的數是偶數的時候f ≠ g產生error,所以1~N+L中共有⌊(N+L)/2⌋個偶數,1~N中共有N/2

個偶數,所以N+1~N+L中有⌊(N+L)/2⌋-⌊N/2個偶數,所以Eots = 1/L * (⌊(N+L)/2⌋-⌊N/2)

 

 

這道題乍一看真是繞啊。。。仔細分析一下,有多少種能產生D in a noiseless setting的f,也就是說在D內這些f產生的值都是相同

的,這些f的不同都發生在N+1~N+L中,在N+1~N+L中每個值都有{-1,1}兩種取值,所以共有2ˆL種不同的情況,即能產生2ˆL

不同的f

 

 

前三個選項很明顯可以看出來不對,思考下第四個選項為什么對,Eots是不在訓練集中的測試錯誤,也就是A(D)和f在N+1~N+L上結

果的差異,通過上題我們知道,f已經取得了N+1~N+L上所有可能的結果,所以A1(D)和A2(D)在N+1~N+L上的取值,都是f中的一

種,所以A1(D)和A2(D)與所有f的Eots取期望,是一樣的。(我們可以這樣計算,對於每一個A,A(D)與f的差異都是從0到f分布的,

我們都可以通過如下方法計算:

0位差異:C(L,0)個,1位差異:C(L,1)個,2位差異:C(L,2),3位差異:C(L,3),······,L位差異:C(L,L)。所以不管什么A值都是一

樣的,期望也就是一樣的。)

 

 這道題就是比較簡單的概率計算:C(10,5) * 0.5ˆ10 ≈0.24

 

 

C(10,1) * 0.1 * 0.9ˆ9 ≈ 0.39

 

 

C(10,1) * 0.9 * 0.1ˆ9 + 0.1ˆ10 = 9.1*10-9

 

 

hoeffding不等式:P(|v - µ| > 0.8) ≤ 2*eˆ(-2 * 0.8ˆ2 * 10) ≈ 5.52*10-6

 

 

 

又是概率問題,出現5個橘色1的概率,橘色1出現在B和C類型骰子里,所以

(1/4 * 0 + 1/4 * 1 + 1/4 * 1 + 1/4 * 0)ˆ5 = 1/32 = 8/256

 

 

上一題的升級版,能出現的純橘色數字1,2,3,4,5,6,其中1,3分布在B和C類型的骰子上,4,6出現在A和D類型的骰子上,2出現在A,C

類型的骰子上,5出現在B,D類型的骰子上。針對這4中情況來計算:

 

(1) (1/4 * 0 + 1/4 * 1 + 1/4 * 1 + 1/4 * 0)ˆ5 = 1/32

 

(2) (1/4 * 1 + 1/4 * 0 + 1/4 * 0 + 1/4 * 1)ˆ5 = 1/32

 

(3) (1/4 * 1 + 1/4 * 0 + 1/4 * 1 + 1/4 * 0)ˆ5 = 1/32

 

(4) (1/4 * 0 + 1/4 * 1 + 1/4 * 0 + 1/4 * 1)ˆ5 = 1/32

 

由於(1)和(3)中有C重復,(1)和(4)中有B重復,(2)和(3)中有A重復,(2)和(4)中有D重復,需要去掉重復的情況

 

所以(1)+(3)+(2)+(4) - 1/4ˆ5 * 4 = 1/8 - 1/256 = 31/256

 

 

navi cycle的意思是,每次發生的錯誤時,循環不重新開始,而是接着進行。程序代碼如下

 1 import numpy
 2 
 3 class navi_cycle_PLA(object):
 4     def __init__(self, dimension, count):
 5         self.__dimension = dimension
 6         self.__count = count
 7 
 8     def train_Matrix(self, path):
 9         training_set = open(path)
10         X_train = numpy.zeros((self.__count, self.__dimension))
11         y_train = numpy.zeros((self.__count, 1))
12         x = []
13         x_count = 0
14         for line in training_set:
15             x.append(1)
16             for str in line.split(' '):
17                 if len(str.split('\t')) == 1:
18                     x.append(float(str))
19                 else:
20                     x.append(float(str.split('\t')[0]))
21                     y_train[x_count,0] = int(str.split('\t')[1].strip())
22             X_train[x_count,:] = x
23             x = []
24             x_count += 1
25         return X_train, y_train
26 
27     def interationCount(self, path):
28         count = 0
29         X_train, y_train = self.train_Matrix(path)
30         w = numpy.zeros((self.__dimension, 1))
31         while True:
32             flag = 0
33             for i in range(self.__count):
34                 if numpy.dot(X_train[i, :], w)[0] * y_train[i, 0] <= 0:
35                     w += y_train[i, 0] * X_train[i,:].reshape(5, 1)
36                     count += 1
37                     flag = 1
38             if flag == 0:
39                 break
40         return count
41 
42 my_Percetron = navi_cycle_PLA(5, 400)
43 print my_Percetron.interationCount("ntumlone_hw1_hw1_15_train.dat")

 

每次實驗前把X的順序打亂,重復2000次

 1 import numpy
 2 import random
 3 
 4 class random_PLA(object):
 5     def __init__(self, dimension, count):
 6         self.__dimension = dimension
 7         self.__count = count
 8 
 9     def random_Matrix(self, path):
10         training_set = open(path)
11         randomLst = []
12         x = []
13         x_count = 0
14         for line in training_set:
15             x.append(1)
16             for str in line.split(' '):
17                 if len(str.split('\t')) == 1:
18                     x.append(float(str))
19                 else:
20                     x.append(float(str.split('\t')[0]))
21                     x.append(int(str.split('\t')[1].strip()))
22             randomLst.append(x)
23             x = []
24             x_count += 1
25         return randomLst
26 
27     def train_Maxtrix(self, path):
28         X_train = numpy.zeros((self.__count, self.__dimension))
29         y_train = numpy.zeros((self.__count, 1))
30         randomLst = self.random_Matrix(path)
31         random.shuffle(randomLst)
32         for i in range(self.__count):
33             for j in range(self.__dimension):
34                 X_train[i, j] = randomLst[i][j]
35             y_train[i, 0] = randomLst[i][self.__dimension]
36         return X_train, y_train
37 
38 
39     def interationCount(self, path):
40         count = 0
41         X_train, y_train = self.train_Maxtrix(path)
42         w = numpy.zeros((self.__dimension, 1))
43         while True:
44             flag = 0
45             for i in range(self.__count):
46                 if numpy.dot(X_train[i, :], w)[0] * y_train[i, 0] <= 0:
47                     w += y_train[i, 0] * X_train[i,:].reshape(5, 1)
48                     count += 1
49                     flag = 1
50             if flag == 0:
51                 break
52         return count
53 
54 average_count = 0
55 for i in range(2000):
56     my_Percetron = random_PLA(5, 400)
57     average_count += my_Percetron.interationCount('ntumlone_hw1_hw1_15_train.dat')
58 print average_count / 2000.0

 

這次是在w每次更新的時候加上一個權重0.5

 1 import numpy
 2 import random
 3 
 4 class random_PLA(object):
 5     def __init__(self, dimension, count):
 6         self.__dimension = dimension
 7         self.__count = count
 8 
 9     def random_Matrix(self, path):
10         training_set = open(path)
11         randomLst = []
12         x = []
13         x_count = 0
14         for line in training_set:
15             x.append(1)
16             for str in line.split(' '):
17                 if len(str.split('\t')) == 1:
18                     x.append(float(str))
19                 else:
20                     x.append(float(str.split('\t')[0]))
21                     x.append(int(str.split('\t')[1].strip()))
22             randomLst.append(x)
23             x = []
24             x_count += 1
25         return randomLst
26 
27     def train_Maxtrix(self, path):
28         X_train = numpy.zeros((self.__count, self.__dimension))
29         y_train = numpy.zeros((self.__count, 1))
30         randomLst = self.random_Matrix(path)
31         random.shuffle(randomLst)
32         for i in range(self.__count):
33             for j in range(self.__dimension):
34                 X_train[i, j] = randomLst[i][j]
35             y_train[i, 0] = randomLst[i][self.__dimension]
36         return X_train, y_train
37 
38 
39     def interationCount(self, path):
40         count = 0
41         X_train, y_train = self.train_Maxtrix(path)
42         w = numpy.zeros((self.__dimension, 1))
43         while True:
44             flag = 0
45             for i in range(self.__count):
46                 if numpy.dot(X_train[i, :], w)[0] * y_train[i, 0] <= 0:
47                     w += 0.5 * y_train[i, 0] * X_train[i,:].reshape(5, 1)
48                     count += 1
49                     flag = 1
50             if flag == 0:
51                 break
52         return count
53 
54 average_count = 0
55 for i in range(2000):
56     my_Percetron = random_PLA(5, 400)
57     average_count += my_Percetron.interationCount('ntumlone_hw1_hw1_15_train.dat')
58 print average_count / 2000.0

 

 

從這題開始,考察的是Pocket算法了,話不多說,上代碼:

 1 import numpy
 2 import random
 3 import copy
 4 
 5 class Pocket(object):
 6     def __init__(self, dimension, train_count, test_count):
 7         self.__dimension = dimension
 8         self.__train_count = train_count
 9         self.__test_count = test_count
10 
11     def random_Matrix(self, path):
12         training_set = open(path)
13         randomLst = []
14         x = []
15         x_count = 0
16         for line in training_set:
17             x.append(1)
18             for str in line.split(' '):
19                 if len(str.split('\t')) == 1:
20                     x.append(float(str))
21                 else:
22                     x.append(float(str.split('\t')[0]))
23                     x.append(int(str.split('\t')[1].strip()))
24             randomLst.append(x)
25             x = []
26             x_count += 1
27         return randomLst
28 
29     def train_Maxtrix(self, path):
30         X_train = numpy.zeros((self.__train_count, self.__dimension))
31         y_train = numpy.zeros((self.__train_count, 1))
32         randomLst = self.random_Matrix(path)
33         random.shuffle(randomLst)
34         for i in range(self.__train_count):
35             for j in range(self.__dimension):
36                 X_train[i, j] = randomLst[i][j]
37             y_train[i, 0] = randomLst[i][self.__dimension]
38         return X_train, y_train
39 
40 
41     def interationW(self, path):
42         count = 0
43         X_train, y_train = self.train_Maxtrix(path)
44         w = numpy.zeros((self.__dimension, 1))
45         bestCount = self.__train_count
46         bestW = numpy.zeros((self.__dimension, 1))
47 
48         while True:
49             for i in range(self.__train_count):
50                 if numpy.dot(X_train[i, :], w)[0] * y_train[i, 0] <= 0:
51                     w += 0.5 * y_train[i, 0] * X_train[i,:].reshape(5, 1)
52                     count += 1
53                     num = 0
54                     for j in range(self.__train_count):
55                         if numpy.dot(X_train[j, :], w)[0] * y_train[j, 0] <= 0:
56                             num += 1
57                     if num < bestCount:
58                         bestCount = num
59                         bestW = copy.deepcopy(w)
60                     if count == 50:
61                         break
62             if count == 50:
63                 break
64         return bestW
65 
66     def test_Matrix(self, test_path):
67         X_test = numpy.zeros((self.__test_count, self.__dimension))
68         y_test = numpy.zeros((self.__test_count, 1))
69         test_set = open(test_path)
70         x = []
71         x_count = 0
72         for line in test_set:
73             x.append(1)
74             for str in line.split(' '):
75                 if len(str.split('\t')) == 1:
76                     x.append(float(str))
77                 else:
78                     x.append(float(str.split('\t')[0]))
79                     y_test[x_count, 0] = (int(str.split('\t')[1].strip()))
80             X_test[x_count, :] = x
81             x = []
82             x_count += 1
83         return X_test, y_test
84 
85     def testError(self, train_path, test_path):
86         w = self.interationW(train_path)
87         X_test, y_test = self.test_Matrix(test_path)
88         count = 0.0
89         for i in range(self.__test_count):
90             if numpy.dot(X_test[i, :], w)[0] * y_test[i, 0] <= 0:
91                 count += 1
92         return count / self.__test_count
93 
94 average_error_rate = 0
95 for i in range(2000):
96     my_Pocket = Pocket(5, 500, 500)
97     average_error_rate += my_Pocket.testError('ntumlone_hw1_hw1_18_train.dat', 'ntumlone_hw1_hw1_18_test.dat')
98 print average_error_rate / 2000.0

 

這道題意在,Pocket和PLA做對比,看樣看出Pocket算法在同等更新下好很多:

 

 1 import numpy
 2 import random
 3 
 4 class random_PLA(object):
 5     def __init__(self, dimension, train_count, test_count):
 6         self.__dimension = dimension
 7         self.__train_count = train_count
 8         self.__test_count = test_count
 9 
10     def random_Matrix(self, path):
11         training_set = open(path)
12         randomLst = []
13         x = []
14         x_count = 0
15         for line in training_set:
16             x.append(1)
17             for str in line.split(' '):
18                 if len(str.split('\t')) == 1:
19                     x.append(float(str))
20                 else:
21                     x.append(float(str.split('\t')[0]))
22                     x.append(int(str.split('\t')[1].strip()))
23             randomLst.append(x)
24             x = []
25             x_count += 1
26         return randomLst
27 
28     def train_Maxtrix(self, path):
29         X_train = numpy.zeros((self.__train_count, self.__dimension))
30         y_train = numpy.zeros((self.__train_count, 1))
31         randomLst = self.random_Matrix(path)
32         random.shuffle(randomLst)
33         for i in range(self.__train_count):
34             for j in range(self.__dimension):
35                 X_train[i, j] = randomLst[i][j]
36             y_train[i, 0] = randomLst[i][self.__dimension]
37         return X_train, y_train
38 
39 
40     def interationW(self, path):
41         count = 0
42         X_train, y_train = self.train_Maxtrix(path)
43         w = numpy.zeros((self.__dimension, 1))
44         while True:
45             for i in range(self.__train_count):
46                 if numpy.dot(X_train[i, :], w)[0] * y_train[i, 0] <= 0:
47                     w += 0.5 * y_train[i, 0] * X_train[i,:].reshape(5, 1)
48                     count += 1
49                 if count == 50:
50                     break
51             if count == 50:
52                 break
53         return w
54 
55     def test_Matrix(self, test_path):
56         X_test = numpy.zeros((self.__test_count, self.__dimension))
57         y_test = numpy.zeros((self.__test_count, 1))
58         test_set = open(test_path)
59         x = []
60         x_count = 0
61         for line in test_set:
62             x.append(1)
63             for str in line.split(' '):
64                 if len(str.split('\t')) == 1:
65                     x.append(float(str))
66                 else:
67                     x.append(float(str.split('\t')[0]))
68                     y_test[x_count, 0] = (int(str.split('\t')[1].strip()))
69             X_test[x_count, :] = x
70             x = []
71             x_count += 1
72         return X_test, y_test
73 
74     def testError(self, train_path, test_path):
75         w = self.interationW(train_path)
76         X_test, y_test = self.test_Matrix(test_path)
77         count = 0.0
78         for i in range(self.__test_count):
79             if numpy.dot(X_test[i, :], w)[0] * y_test[i, 0] <= 0:
80                 count += 1
81         return count / self.__test_count
82 
83 average_error_rate = 0
84 for i in range(2000):
85     my_PLA = random_PLA(5, 500, 500)
86     average_error_rate += my_PLA.testError('ntumlone_hw1_hw1_18_train.dat', 'ntumlone_hw1_hw1_18_test.dat')
87 print average_error_rate / 2000.0

 

和第18題一樣,只是把更新次數增大到100:

 1 import numpy
 2 import random
 3 import copy
 4 
 5 class Pocket(object):
 6     def __init__(self, dimension, train_count, test_count):
 7         self.__dimension = dimension
 8         self.__train_count = train_count
 9         self.__test_count = test_count
10 
11     def random_Matrix(self, path):
12         training_set = open(path)
13         randomLst = []
14         x = []
15         x_count = 0
16         for line in training_set:
17             x.append(1)
18             for str in line.split(' '):
19                 if len(str.split('\t')) == 1:
20                     x.append(float(str))
21                 else:
22                     x.append(float(str.split('\t')[0]))
23                     x.append(int(str.split('\t')[1].strip()))
24             randomLst.append(x)
25             x = []
26             x_count += 1
27         return randomLst
28 
29     def train_Maxtrix(self, path):
30         X_train = numpy.zeros((self.__train_count, self.__dimension))
31         y_train = numpy.zeros((self.__train_count, 1))
32         randomLst = self.random_Matrix(path)
33         random.shuffle(randomLst)
34         for i in range(self.__train_count):
35             for j in range(self.__dimension):
36                 X_train[i, j] = randomLst[i][j]
37             y_train[i, 0] = randomLst[i][self.__dimension]
38         return X_train, y_train
39 
40 
41     def interationW(self, path):
42         count = 0
43         X_train, y_train = self.train_Maxtrix(path)
44         w = numpy.zeros((self.__dimension, 1))
45         bestCount = self.__train_count
46         bestW = numpy.zeros((self.__dimension, 1))
47 
48         while True:
49             for i in range(self.__train_count):
50                 if numpy.dot(X_train[i, :], w)[0] * y_train[i, 0] <= 0:
51                     w += 0.5 * y_train[i, 0] * X_train[i,:].reshape(5, 1)
52                     count += 1
53                     num = 0
54                     for j in range(self.__train_count):
55                         if numpy.dot(X_train[j, :], w)[0] * y_train[j, 0] <= 0:
56                             num += 1
57                     if num < bestCount:
58                         bestCount = num
59                         bestW = copy.deepcopy(w)
60                     if count == 100:
61                         break
62             if count == 100:
63                 break
64         return bestW
65 
66     def test_Matrix(self, test_path):
67         X_test = numpy.zeros((self.__test_count, self.__dimension))
68         y_test = numpy.zeros((self.__test_count, 1))
69         test_set = open(test_path)
70         x = []
71         x_count = 0
72         for line in test_set:
73             x.append(1)
74             for str in line.split(' '):
75                 if len(str.split('\t')) == 1:
76                     x.append(float(str))
77                 else:
78                     x.append(float(str.split('\t')[0]))
79                     y_test[x_count, 0] = (int(str.split('\t')[1].strip()))
80             X_test[x_count, :] = x
81             x = []
82             x_count += 1
83         return X_test, y_test
84 
85     def testError(self, train_path, test_path):
86         w = self.interationW(train_path)
87         X_test, y_test = self.test_Matrix(test_path)
88         count = 0.0
89         for i in range(self.__test_count):
90             if numpy.dot(X_test[i, :], w)[0] * y_test[i, 0] <= 0:
91                 count += 1
92         return count / self.__test_count
93 
94 average_error_rate = 0
95 for i in range(2000):
96     my_Pocket = Pocket(5, 500, 500)
97     average_error_rate += my_Pocket.testError('ntumlone_hw1_hw1_18_train.dat', 'ntumlone_hw1_hw1_18_test.dat')
98 print average_error_rate / 2000.0

到此第一次作業就全部完成了~~20道題看似不多,但是工作量真是大啊,概念,計算,編程全部都涵蓋了,真是不容易。。。

后面再接再厲了~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM