导入数据集
导入ex3data1.mat,并显示100张图片
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 loda_data(path,transpose=True):
data=sio.loadmat(path)
y=data.get('y') #(5000,1)
y=y.reshape(y.shape[0]) #make sure it is column vector(make it back to column vector)
X=data.get('X') #(5000,400)
if transpose:
X=np.array([im.reshape((20,20)).T for im in X])
X=np.array([im.reshape(400) for im in X])
return X,y
raw_X,raw_y=loda_data(r'D:\MachineLearning\ex3\ex3data1.mat')
#print(raw_X.shape,raw_y.shape) (5000,400)(5000,)
def plot_100_image(X):
size=int(np.sqrt(X.shape[1]))
sample_idx=np.random.choice(np.arange(X.shape[0]),100)#100*400
sample_images=X[sample_idx,:]
fig,ax_array=plt.subplots(nrows=10,ncols=10,sharey=True,sharex=True,figsize=(8,8))
for r in range(10):
for c in range(10):
ax_array[r,c].matshow(sample_images[10*r+c].reshape((size,size)),cmap=matplotlib.cm.binary)
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plot_100_image(raw_X)
plt.show()
成功导入的显示结果如下图所示
准备数据
正则化cost和gradient
#regularized the cost function
def cost(theta,X,y):
return np.mean(-y*np.log(sigmoid(X@theta))-(1-y)*np.log(1-sigmoid(X@theta)))
def regularized_cost(theta,X,y,l=1):
#you dont penalize theta_0
theta_j1_to_n=theta[1:]
regularized_term=(l/(2*len(X)))*np.power(theta_j1_to_n,2).sum()
return cost(theta,X,y)+regularized_term
#regularized the gradient
def regularized_gradient(theta,X,y,l=1):
#still, leave theta_0 alone
theta_j1_to_n=theta[1:]
regularized_theta=(l/len(X))*theta_j1_to_n
#by doing this , no offset is on theta_0
regularized_term=np.concatenate([np.array([0]),regularized_theta])#concatenate用于多数组的拼接,默认axis=0
#axis应该从轴来考虑,为1时是横轴从左到右,为0时从上到下
return gradient(theta,X,y)+regularized_term
def sigmoid(z):
return 1/(1+np.exp(-z))
def gradient(theta,X,y):
return (1/len(X))*X.T@(sigmoid(X@theta)-y)
def logistic_regression(X,y,l=1):
theta=np.zeros(X.shape[l])
res=opt.minimize(fun=regularized_cost,x0=theta,args=(X,y,l),method='TNC',jac=regularized_gradient,options={'disp':True})
final_theta=res.x
return final_theta
def predict(x,theta):
prob=sigmoid(x@theta)
return (prob>=0.5).astype(int)
t0=logistic_regression(X,y[0])
#print(t0.shape)
y_pred=predict(X,t0)
#print('Accuracy={}'.format(np.mean(y[0]==y_pred)))
#train k model
k_theta=np.array([logistic_regression(X,y[k]) for k in range(10)])
#print(k_theta.shape)