import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import matplotlib
import scipy.optimize as opt
from sklearn.metrics import classification_report
def load_data(path,transpose=True):
data=sio.loadmat(path)
y=data.get('y')
y=y.reshape(y.shape[0])
X=data.get('X')
if transpose:
X=np.array([im.reshape((20,20)) for im in X])
X=np.array([im.reshape(400) for im in X])
return X,y
X,y=load_data('ex3data1.mat')
print(X.shape)
print(y.shape)
def plot_an_image(image):
fig,ax=plt.subplots(figsize=(1,1))
ax.matshow(image.reshape((20,20)),cmap=matplotlib.cm.binary)
plt.xticks([])
plt.yticks([])
pick_one=np.random.randint(0,5000)
plot_an_image(X[pick_one,:])
plt.show()
print('this should be {}'.format(y[pick_one]))
def plot_100_image(X):
sample_index=np.random.choice(len(X),100)
images=X[sample_index,:]
fig,ax=plt.subplots(ncols=10,nrows=10,figsize=(8,8 ),sharex=True,sharey=True)
for r in range(10):
for c in range(10):
ax[r,c].imshow(images[10*r+c].reshape((20,20)),cmap='gray_r')
plt.xticks([])
plt.yticks([])
plot_100_image(X)
plt.show()
raw_x,raw_y=load_data('ex3data1.mat')
def sigmod(z):
return 1/(1+np.exp(-z))
def regularized_cost(theta,X,y,punish=1):
A=sigmod(X@theta)
first=y*np.log(A)
second=(1-y)*np.log(1-A)
reg=theta[1:]@theta[1:]
return -np.sum(first+second)/len(X)+reg*(punish/(2*len(X)))
def gridentdecent(theta,X,y):
return (1/len(X))*(X.T@(sigmod(X@theta)-y))
def regularized_grident(theta,X,y,punish=1):
theta_n=theta[1:]
regularized_theta=theta_n*(punish/len(X))
regularized_theta=np.insert(regularized_theta,0,values=0,axis=0)
return gridentdecent(theta,X,y)+regularized_theta
def logical_regression(X,y,K,punish=1):
theta=np.zeros((K,X.shape[1]))
for i in range(1,K+1):
theta_i=np.zeros(X.shape[1],)
res=opt.minimize(fun=regularized_cost,x0=theta_i,args=(X,y==i,1),method='TNC',jac=regularized_grident,options={'disp': True})
theta[i-1,:]=res.x
return theta
def predict(x,theta):
prob=sigmod(x@theta.T)
prob_max=np.argmax(prob,axis=1)
print(prob_max.shape)
return prob_max+1
X=np.insert(raw_x,0,values=1,axis=1)
y=raw_y.flatten()
theta_final=logical_regression(X,y,10)
print(logical_regression(X,y,10))
y_pred=predict(X,theta_final)
print('Accuracy={}'.format(np.mean(y == y_pred)))
import scipy.io as sio
data=sio.loadmat('ex3weights.mat')
print(data.keys())
theta1=data['Theta1']
theta2=data['Theta2']
a1=X
z2=a1@theta1.T
a2=sigmod(z2)
a2=np.insert(a2,0,values=1,axis=1)
z3=a2@theta2.T
a3=sigmod(z3)
print(a3.shape)
y_pred=np.argmax(a3,axis=1)
y_pred=y_pred+1
acc=np.mean(y_pred==y)
print(acc)