스탠퍼드 머신러닝 by 오은다-신경 네트워크python 실현(5차 프로그래밍 작업)

33186 단어
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import scipy.optimize
import warnings
warnings.filterwarnings('ignore')

data_file='data/ex5data1.mat'
data=scipy.io.loadmat(data_file)
X=data['X']
y=data['y']
X_test=data['Xtest']
y_test=data['ytest']
X_val=data['Xval']
y_val=data['yval']
X=np.insert(X,0,1,axis=1)
X_test=np.insert(X_test,0,1,axis=1)
X_val=np.insert(X_val,0,1,axis=1)

def plotData(theta,X,y):
    plt.plot(X[:,1],y,'rx')
    plt.xlabel('Change in water level (x)')
    plt.ylabel('Water flowing out of the dam (y)')
    plt.grid(True)
    plt.plot(X[:, 1], X.dot(theta))
    plt.xlim(-50,40)
    plt.ylim(-5,40)
    plt.show()

def computeCost(theta,X,y,l):
    theta=theta.ravel()
    m=X.shape[0]
    theta=theta.reshape((theta.shape[0],1))
    h=np.array(X.dot(theta)).reshape((m,1))
    J=1./(2*m)*(h-y).T.dot((h-y))+l/(2.*m)*(theta[1:].T.dot(theta[1:]))
    J=J.ravel()
    return J

def computeGradient(theta,X,y,l):
    theta=theta.ravel()
    theta=theta.reshape((theta.shape[0],1))
    m = X.shape[0]
    h = X.dot(theta)
    grad=1./m*(X).T.dot(h-y)+1.*l/m*theta
    grad[0][0]=1./m*(X).T.dot(h-y)[0][0]
    grad=grad.reshape((grad.shape[0],1))
    return grad

def computeGradientFlatten(theta,X,y,l):
    return computeGradient(theta,X,y,l).flatten()

def optimizeTheta(theta,X,y,l):
    theta_new=np.array(scipy.optimize.fmin_cg(computeCost,x0=theta,\
                                     fprime=computeGradientFlatten,\
                                     args=(X,y,l),disp=True,\
                                     epsilon=1.49e-12,maxiter=1000))
    theta_new=theta_new.ravel()
    theta_new=theta_new.reshape((theta_new.shape[0],1))
    return theta_new

theta=np.array([[1.],[1.]])
theta_new=optimizeTheta(theta,X,y,0.)
plotData(theta_new,X,y)

def plotLearningCurve(X,y,X_val,y_val):
    theta=np.array([[1.],[1.]])
    num,error_train,error_val=[],[],[]
    for i in range(X.shape[0]):
        X_train=X[:i+1,:]
        y_train=y[:i+1]
        num.append(y_train.shape[0])
        theta_new=optimizeTheta(theta,X_train,y_train,0.)
        error_train.append(computeCost(theta_new,X_train,y_train,0.))
        error_val.append(computeCost(theta_new,X_val,y_val,0.))
    plt.plot(num,error_train,label='Train')
    plt.plot(num,error_val,label='Cross Validation')
    plt.xlabel('Number of training examples')
    plt.ylabel('Error')
    plt.grid(True)
    plt.legend()
    plt.show()

plotLearningCurve(X,y,X_val,y_val)

def addPolyFeatures(X,m):
    X_new=X.copy()
    for i in range(m):
        X_temp=pow(X[:,1],i+2)
        X_new=np.insert(X_new,X_new.shape[1],X_temp,axis=1)
    return X_new

def normalizeFeature(X):
    X_new=X.copy()
    mean_X_new=np.mean(X_new,axis=0)
    std_X_new=np.std(X_new,axis=0,ddof=1)
    X_new[:,1:]=(X_new[:,1:]-mean_X_new[1:])/std_X_new[1:]
    return X_new,mean_X_new,std_X_new

X_new = addPolyFeatures(X, 5)
X_new,mean_X_new,std_X_new=normalizeFeature(X_new)
theta=np.ones((X_new.shape[1],1))
theta_new=optimizeTheta(theta,X_new,y,0.)

def plotPoly()

def plotFit(fit_theta, means, stds):
    """
    Function that takes in some learned fit values (on feature-normalized data)
    It sets x-points as a linspace, constructs an appropriate X matrix,
    un-does previous feature normalization, computes the hypothesis values,
    and plots on top of data
    """
    n_points_to_plot = 50
    xvals = np.linspace(-55, 55, n_points_to_plot)
    xmat = np.ones((n_points_to_plot, 1))

    xmat = np.insert(xmat, xmat.shape[1], xvals.T, axis=1)
    xmat = genPolyFeatures(xmat, len(fit_theta) - 2)
    # This is undoing feature normalization
    xmat[:, 1:] = xmat[:, 1:] - means[1:]
    xmat[:, 1:] = xmat[:, 1:] / stds[1:]
    plotData()
    plt.plot(xvals, h(fit_theta, xmat), 'b--')


plotFit(fit_theta, stored_means, stored_stds)

좋은 웹페이지 즐겨찾기