import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
data = sio.loadmat('ex3data1.mat')
data
print(type(data))
data.keys()
raw_X = data['X']
raw_y = data['y']
print(raw_X.shape,raw_y.shape)
def plot_an_image(X): #显示一张图片
pick_one = np.random.randint(5000) #随机选取一个
image = X[pick_one,:]
fig,ax = plt.subplots(figsize=(1,1))
ax.imshow(image.reshape(20,20).T,cmap = 'gray_r')
plt.xticks([]) ##去掉图片显示的刻度
plt.yticks([])
plot_an_image(raw_X)
def plot_100_image(X):
sample_index = np.random.choice(len(X),100) #随机选取100张图片
images = X[sample_index,:]
print(images.shape)
fig,ax = plt.subplots(ncols=10,nrows=10,figsize=(8,8),sharex=True,sharey=True) #10×10网格,共享x,y属性
for r in range(10):
for c in range(10):
ax[r,c].imshow(images[10 * r + c].reshape(20,20).T,cmap='gray_r')
plt.xticks([])
plt.yticks([])
plt.show
plot_100_image(raw_X)
def sigmoid(z):
return 1/(1+np.exp(-z))
def costFunction(theta,X,y,lamda):
A = sigmoid(X@theta)
first = y*np.log(A)
second = (1-y) * np.log(1-A)
# reg = np.sum(np.power(theta[1:],2)) * (lamda / (2 * len(X))) #这行代码也可以算内积
reg = theta[1:] @ theta[1:] * (lamda / (2 * len(X))) #算内积
return -np.sum(first + second ) / len(X) + reg
def gradient_reg(theta,X,y,lamda):
reg = theta[1:] * (lamda / len(X))
reg = np.insert(reg,0,values=0,axis=0)
first = (X.T@(sigmoid(X@theta) - y)) / len(X)
return first + reg
X = np.insert(raw_X,0,values=1,axis=1)
X.shape
y = raw_y.flatten()
y.shape
from scipy.optimize import minimize
def one_vs_all(X,y,lamda,K):
n = X.shape[1] #1代表X的列数,如果是0,则代表X的行数
theta_all = np.zeros((K,n))
for i in range(1,K+1):
theta_i = np.zeros(n,)
res = minimize(fun =costFunction,
x0 = theta_i,
args = (X, y == i,lamda),
method = 'TNC',
jac =gradient_reg )
theta_all[i-1,:] = res.x
return theta_all
lamda = 1
K =10
theta_final = one_vs_all(X,y,lamda,K)
theta_final
def predict(X,theta_final):
h = sigmoid(X@theta_final.T) #(5000,401) (10,401) =>(5000,10)
h_argmax = np.argmax(h,axis=1)
return h_argmax + 1
y_pred = predict(X,theta_final)
acc = np.mean(y_pred == y)
acc