機器學習作業(三)多類別分類與神經網絡——Python(numpy)實現


題目太長了!下載地址【傳送門

第1題

簡述:識別圖片上的數字。

  1 import numpy as np
  2 import scipy.io as scio
  3 import matplotlib.pyplot as plt
  4 import scipy.optimize as op
  5 
  6 #顯示圖片數據
  7 def displayData(X):
  8     m = np.size(X, 0)  #X的行數,即樣本數量
  9     n = np.size(X, 1)  #X的列數,即單個樣本大小
 10     example_width = int(np.round(np.sqrt(n)))  #單張圖片寬度
 11     example_height = int(np.floor(n / example_width))  #單張圖片高度
 12     display_rows = int(np.floor(np.sqrt(m)))  #顯示圖中,一行多少張圖
 13     display_cols = int(np.ceil(m / display_rows))  #顯示圖中,一列多少張圖片
 14     pad = 1  #圖片間的間隔
 15     display_array = - np.ones((pad + display_rows * (example_height + pad),
 16                             pad + display_cols * (example_width + pad)))  #初始化圖片矩陣
 17     curr_ex = 0  #當前的圖片計數
 18     #將每張小圖插入圖片數組中
 19     for j in range(0, display_rows):
 20         for i in range(0, display_cols):
 21             if curr_ex >= m:
 22                 break
 23             max_val = np.max(abs(X[curr_ex, :]))
 24             jstart = pad + j * (example_height + pad)
 25             istart = pad + i * (example_width + pad)
 26             display_array[jstart: (jstart + example_height), istart: (istart + example_width)] = \
 27                 np.array(X[curr_ex, :]).reshape(example_height, example_width) / max_val
 28             curr_ex = curr_ex + 1
 29         if curr_ex >= m:
 30             break
 31     display_array = display_array.T
 32     plt.imshow(display_array,cmap=plt.cm.gray)
 33     plt.axis('off')
 34     plt.show()
 35 
 36 #計算hθ(z)
 37 def sigmoid(z):
 38     g = 1.0 / (1.0 + np.exp(-z))
 39     return g
 40 
 41 #計算cost
 42 def lrCostFunction(theta, X, y, lamb):
 43     theta = np.array(theta).reshape((np.size(theta), 1))
 44     m = np.size(y)
 45     h = sigmoid(np.dot(X, theta))
 46     J = 1 / m * (-np.dot(y.T, np.log(h)) - np.dot((1 - y.T), np.log(1 - h)))
 47     theta2 = theta[1:, 0]
 48     Jadd = lamb / (2 * m) * np.sum(theta2 ** 2)
 49     J = J + Jadd
 50     return J.flatten()
 51 
 52 #計算梯度
 53 def gradient(theta, X, y, lamb):
 54     theta = np.array(theta).reshape((np.size(theta), 1))
 55     m = np.size(y)
 56     h = sigmoid(np.dot(X, theta))
 57     grad = 1/m*np.dot(X.T, h - y)
 58     theta[0,0] = 0
 59     gradadd = lamb/m*theta
 60     grad = grad + gradadd
 61     return grad.flatten()
 62 
 63 #θ計算
 64 def oneVsAll(X, y, num_labels, lamb):
 65     m = np.size(X, 0)
 66     n = np.size(X, 1)
 67     all_theta = np.zeros((num_labels, n+1))
 68     one = np.ones(m)
 69     X = np.insert(X, 0, values=one, axis=1)
 70     for c in range(0, num_labels):
 71         initial_theta = np.zeros(n+1)
 72         y_t = (y==c)
 73         result = op.minimize(fun=lrCostFunction, x0=initial_theta, args=(X, y_t, lamb), method='TNC', jac=gradient)
 74         all_theta[c, :] = result.x
 75     return all_theta
 76 
 77 #計算准確率
 78 def predictOneVsAll(all_theta, X):
 79     m = np.size(X, 0)
 80     num_labels = np.size(all_theta, 0)
 81     p = np.zeros((m, 1))  #用來保存每行的最大值
 82     g = np.zeros((np.size(X, 0), num_labels))  #用來保存每次分類后的結果(一共分類了10次,每次保存到一列上)
 83     one = np.ones(m)
 84     X = np.insert(X, 0, values=one, axis=1)
 85     for c in range(0, num_labels):
 86         theta = all_theta[c, :]
 87         g[:, c] = sigmoid(np.dot(X, theta.T))
 88     p = g.argmax(axis=1)
 89     # print(p)
 90     return p.flatten()
 91 
 92 #加載數據文件
 93 data = scio.loadmat('ex3data1.mat')
 94 X = data['X']
 95 y = data['y']
 96 y = y%10  #因為數據集是考慮了matlab從1開始,把0的結果保存為了10,這里進行取余,將10變回0
 97 m = np.size(X, 0)
 98 rand_indices = np.random.randint(0,m,100)
 99 sel = X[rand_indices, :]
100 displayData(sel)
101 
102 #計算θ
103 lamb = 0.1
104 num_labels = 10
105 all_theta = oneVsAll(X, y, num_labels, lamb)
106 # print(all_theta)
107 
108 #計算預測的准確性
109 pred = predictOneVsAll(all_theta, X)
110 # np.set_printoptions(threshold=np.inf)
111 #在計算這個上遇到了一個坑:
112 #pred輸出的是[[...]]的形式,需要flatten變成1維向量,y同樣用flatten變成1維向量
113 acc = np.mean(pred == y.flatten())*100
114 print('Training Set Accuracy:',acc,'%')

 

運行結果:

 

 

第2題

簡介:使用神經網絡實現數字識別(Θ已提供)

 1 import numpy as np
 2 import scipy.io as scio
 3 import matplotlib.pyplot as plt
 4 import scipy.optimize as op
 5 
 6 #顯示圖片數據
 7 def displayData(X):
 8     m = np.size(X, 0)  #X的行數,即樣本數量
 9     n = np.size(X, 1)  #X的列數,即單個樣本大小
10     example_width = int(np.round(np.sqrt(n)))  #單張圖片寬度
11     example_height = int(np.floor(n / example_width))  #單張圖片高度
12     display_rows = int(np.floor(np.sqrt(m)))  #顯示圖中,一行多少張圖
13     display_cols = int(np.ceil(m / display_rows))  #顯示圖中,一列多少張圖片
14     pad = 1  #圖片間的間隔
15     display_array = - np.ones((pad + display_rows * (example_height + pad),
16                             pad + display_cols * (example_width + pad)))  #初始化圖片矩陣
17     curr_ex = 0  #當前的圖片計數
18     #將每張小圖插入圖片數組中
19     for j in range(0, display_rows):
20         for i in range(0, display_cols):
21             if curr_ex >= m:
22                 break
23             max_val = np.max(abs(X[curr_ex, :]))
24             jstart = pad + j * (example_height + pad)
25             istart = pad + i * (example_width + pad)
26             display_array[jstart: (jstart + example_height), istart: (istart + example_width)] = \
27                 np.array(X[curr_ex, :]).reshape(example_height, example_width) / max_val
28             curr_ex = curr_ex + 1
29         if curr_ex >= m:
30             break
31     display_array = display_array.T
32     plt.imshow(display_array,cmap=plt.cm.gray)
33     plt.axis('off')
34     plt.show()
35 
36 #計算hθ(z)
37 def sigmoid(z):
38     g = 1.0 / (1.0 + np.exp(-z))
39     return g
40 
41 #實現神經網絡
42 def predict(theta1, theta2, X):
43     m = np.size(X,0)
44     p = np.zeros((np.size(X, 0), 1))
45     #第二層計算
46     one = np.ones(m)
47     X = np.insert(X, 0, values=one, axis=1)
48     a2 = sigmoid(np.dot(X, theta1.T))
49     #第三層計算
50     one = np.ones(np.size(a2,0))
51     a2 = np.insert(a2, 0, values=one, axis=1)
52     a3 = sigmoid(np.dot(a2, theta2.T))
53     p = a3.argmax(axis=1) + 1  #y的值為1-10,所以此處0-9要加1
54     return p.flatten()
55 
56 #讀取數據文件
57 data = scio.loadmat('ex3data1.mat')
58 X = data['X']
59 y = data['y']
60 m = np.size(X, 0)
61 rand_indices = np.random.randint(0,m,100)
62 sel = X[rand_indices, :]
63 # displayData(sel)
64 
65 theta = scio.loadmat('ex3weights.mat')
66 theta1 = theta['Theta1']
67 theta2 = theta['Theta2']
68 
69 #預測准確率
70 pred = predict(theta1, theta2, X)
71 acc = np.mean(pred == y.flatten())*100
72 print('Training Set Accuracy:',acc,'%')
73 
74 #識別單張圖片
75 for i in range(0, m):
76     it = np.random.randint(0, m, 1)
77     it = it[0]
78     displayData(X[it:it+1, :])
79     pred = predict(theta1, theta2, X[it:it+1, :])
80     print('Neural Network Prediction:', pred)
81     print('input q to exit:')
82     cin = input()
83     if cin == 'q':
84         break

神經網絡的矩陣表示分析:

運行結果:

 


免責聲明!

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



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