# -*- coding: utf-8 -*-
"""
Created on Wed Jul 1 20:28:57 2020
@author: cheetah023
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sci
import random as ra
#函数定义
def sigmoid(X):
return 1 /(1 + np.exp(-X))
def predict(theta1, theta2, X):
m = X.shape[0]
ones = np.ones([m,1])
X = np.column_stack([ones,X])
a2 = sigmoid(np.dot(X,theta1.T))
a2 = np.column_stack([ones,a2])
a3 = sigmoid(np.dot(a2,theta2.T))
p = np.argmax(a3,axis = 1) + 1
p = np.reshape(p,[m,1])
return p
#Part 1: Loading and Visualizing Data
data1 = sci.loadmat('ex3data1.mat')
#print(data) #data是个字典类型
X = data1['X']
y = data1['y']
#print('X',X.shape)
#print('y',y.shape)
#Part 2: Loading Pameters
data2 = sci.loadmat('ex3weights.mat')
#print(data2.keys())
theta1 = data2['Theta1']
theta2 = data2['Theta2']
#print('theta1',theta1.shape)
#print('theta2',theta2.shape)
#Part 3: Implement Predict
p = predict(theta1, theta2, X)
#temp里面的值是True(=1)和False(=0)
temp = (p==y)
prob = np.mean(temp)
print('Training Set Accuracy:',prob)