def test_planar_data(): x, y = load_planar_dataset() # 加载平面点数据集 print "x shape: " + str(x.shape) print "y shape: " + str(y.shape) iteration_num = 3000 learning_rate = 0.6 params, cost_list = regression(x, y, iteration_num, learning_rate) print "params: %s" % str(params) print "cost_list: %s" % str(cost_list) predicts = predict(x, params) cal_accuracy(predicts, y)
from utils.testCases_v2 import * import sklearn import sklearn.datasets import sklearn.linear_model from utils.planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets from layer_sizes import layer_sizes from initialize_parameters import initialize_parameters from forward_propagation import forward_propagation from compute_cost import compute_cost from backward_propagation import backward_propagation from update_parameters import update_parameters np.random.seed(1) # set a seed so that the results are consistent X, Y = load_planar_dataset() # shape_X = X.shape # shape_Y = Y.shape # m = X.shape[1] # training set size # # print ('The shape of X is: ' + str(shape_X)) # # print ('The shape of Y is: ' + str(shape_Y)) # # print ('I have m = %d training examples!' % (m)) # # Train the logistic regression classifier # clf = sklearn.linear_model.LogisticRegressionCV() # clf.fit(X.T, Y.T) # # Plot the decision boundary for logistic regression # LR_predictions = clf.predict(X.T)
grads = backward_propagate(params, cache, x, y) params = update_params(params, grads, learning_rate) if print_cost and i % 1000 == 0: print('cost after iter %i:%f' % (i, cost)) return params def predict(params, x): a2, cache = forward_propagate(x, params) pred = np.round(a2) return pred if __name__ == "__main__": np.random.seed(1) x, y = load_planar_dataset() # plt.figure(figsize=(5,8)) # plt.scatter(x[0,:],x[1,:],c=y,s=20,cmap=plt.cm.Spectral) # plt.show() # sample_linear_logistic(x,y) print('the shape x is:' + str(x.shape)) print('the shape y is:' + str(y.shape)) m = x.shape[1] # params = nn_model(x,y,n_h=4,num_iter=10000,print_cost=True,learning_rate=1.2) # plot_decision_boundary(lambda x:predict(params,x.T),x,y) # plt.show() # pred = predict(params,x) # print('Accuracy: %d' % float((np.dot(y,pred.T) +np.dot(1-y,1-pred.T))/float(y.size)*100) + '%') plt.figure(figsize=(20, 20)) hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]