(Theta1,Theta2,X)
| 240 | |
| 241 | # 预测 |
| 242 | def predict(Theta1,Theta2,X): |
| 243 | m = X.shape[0] |
| 244 | num_labels = Theta2.shape[0] |
| 245 | #p = np.zeros((m,1)) |
| 246 | '''正向传播,预测结果''' |
| 247 | X = np.hstack((np.ones((m,1)),X)) |
| 248 | h1 = sigmoid(np.dot(X,np.transpose(Theta1))) |
| 249 | h1 = np.hstack((np.ones((m,1)),h1)) |
| 250 | h2 = sigmoid(np.dot(h1,np.transpose(Theta2))) |
| 251 | |
| 252 | ''' |
| 253 | 返回h中每一行最大值所在的列号 |
| 254 | - np.max(h, axis=1)返回h中每一行的最大值(是某个数字的最大概率) |
| 255 | - 最后where找到的最大概率所在的列号(列号即是对应的数字) |
| 256 | ''' |
| 257 | #np.savetxt("h2.csv",h2,delimiter=',') |
| 258 | p = np.array(np.where(h2[0,:] == np.max(h2, axis=1)[0])) |
| 259 | for i in np.arange(1, m): |
| 260 | t = np.array(np.where(h2[i,:] == np.max(h2, axis=1)[i])) |
| 261 | p = np.vstack((p,t)) |
| 262 | return p |
| 263 | |
| 264 | if __name__ == "__main__": |
| 265 | checkGradient() |
no test coverage detected