コード例 #1
0
ファイル: tests.py プロジェクト: YXSIO/coursera-mlclass
 def test_learning_curve_poly(self):
     highest_degree = 8
     X_poly = map_poly_features(self.X, highest_degree)
     X_poly, mu, sigma = feature_normalization(X_poly)
     Xval_poly = map_poly_features(self.Xval, highest_degree)
     Xval_poly = (Xval_poly - mu) / sigma
     lamda = 0.0
     
     error_train, error_val = learning_curve(X_poly, self.y, Xval_poly, self.yval, lamda)
     plt.xlabel('Number of Training Examples')
     plt.ylabel("Error")
     plt.plot( range(1,self.m+1), error_train)
     plt.plot( range(1,self.m+1), error_val)
     plt.show()
コード例 #2
0
    def test_learning_curve_poly(self):
        highest_degree = 8
        X_poly = map_poly_features(self.X, highest_degree)
        X_poly, mu, sigma = feature_normalization(X_poly)
        Xval_poly = map_poly_features(self.Xval, highest_degree)
        Xval_poly = (Xval_poly - mu) / sigma
        lamda = 0.0

        error_train, error_val = learning_curve(X_poly, self.y, Xval_poly,
                                                self.yval, lamda)
        plt.xlabel('Number of Training Examples')
        plt.ylabel("Error")
        plt.plot(range(1, self.m + 1), error_train)
        plt.plot(range(1, self.m + 1), error_val)
        plt.show()
コード例 #3
0
ファイル: tests.py プロジェクト: YXSIO/coursera-mlclass
 def test_validation_curve_poly(self):
     highest_degree = 8
     X_poly = map_poly_features(self.X, highest_degree)
     X_poly, mu, sigma = feature_normalization(X_poly)
     Xval_poly = map_poly_features(self.Xval, highest_degree)
     Xval_poly = (Xval_poly - mu) / sigma
     lamdas = np.array([0,0.001,0.003,0.01,0.03,0.1,0.3,1,3,10])
     
     error_train, error_val = validation_curve(X_poly, self.y, Xval_poly, self.yval, lamdas)
     plt.xlabel('lamda')
     plt.ylabel("Error")
     plt.plot( lamdas, error_train, label='Train')
     plt.plot( lamdas, error_val, label='Validation')
     plt.legend()
     plt.show()
コード例 #4
0
    def test_validation_curve_poly(self):
        highest_degree = 8
        X_poly = map_poly_features(self.X, highest_degree)
        X_poly, mu, sigma = feature_normalization(X_poly)
        Xval_poly = map_poly_features(self.Xval, highest_degree)
        Xval_poly = (Xval_poly - mu) / sigma
        lamdas = np.array([0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10])

        error_train, error_val = validation_curve(X_poly, self.y, Xval_poly,
                                                  self.yval, lamdas)
        plt.xlabel('lamda')
        plt.ylabel("Error")
        plt.plot(lamdas, error_train, label='Train')
        plt.plot(lamdas, error_val, label='Validation')
        plt.legend()
        plt.show()
コード例 #5
0
 def test_gradient_descent(self):
     iterations = 400
     alpha = 0.01 #[0.01, 0.03, 0.1, 0.3, 1.0]
     m,n = self.X.shape
     theta = np.zeros((n+1,1))
     
     # add x_0 and do feature normalization on the rest of the columns
     self.X = np.concatenate([np.ones((self.m,1)),self.X],axis=1)
     self.X[:,1:n+1], mu, sigma = feature_normalization(self.X[:,1:n+1])
     
     theta = gradientDescent(self.X, self.y, theta, alpha, iterations)
     
     test = np.array([1.0, 1650.0, 3.0]).reshape((3,1))
     test[1:,:] = ( test[1:,:] - mu ) / sigma
     test = test.reshape((1,3)) # m=1, n=2, because there is 1 test case, with 2 features in it
     self.assertAlmostEqual(hypothesis(test, theta), 289314.62, places=2)
コード例 #6
0
ファイル: tests.py プロジェクト: YXSIO/coursera-mlclass
 def test_linear_regression_poly(self):
     highest_degree = 8
     X_poly = map_poly_features(self.X, highest_degree)
     X_poly, mu, sigma = feature_normalization(X_poly)
     
     initial_theta = np.ones(highest_degree+1)
     lamda = 0.0
     theta = train_linear_reg(initial_theta, X_poly, self.y, lamda)
     hypo = predictions(theta, X_poly)
     
     # now scatter the data and plot the hypothesis
     df = pd.DataFrame(np.hstack(( self.X, hypo.reshape(self.y.shape), self.y )), columns=['X','hypo','y'])
     df = df.sort('X')
     plt.xlabel("Change in water level (x)")
     plt.ylabel("Water flowing out of the dam (y)")
     plt.scatter( df['X'], df['y'], marker='x', c='r', s=30, linewidth=2 )
     plt.plot( df['X'], df['hypo'], linestyle='--', linewidth=3 )
     plt.show()
コード例 #7
0
    def test_linear_regression_poly(self):
        highest_degree = 8
        X_poly = map_poly_features(self.X, highest_degree)
        X_poly, mu, sigma = feature_normalization(X_poly)

        initial_theta = np.ones(highest_degree + 1)
        lamda = 0.0
        theta = train_linear_reg(initial_theta, X_poly, self.y, lamda)
        hypo = predictions(theta, X_poly)

        # now scatter the data and plot the hypothesis
        df = pd.DataFrame(np.hstack(
            (self.X, hypo.reshape(self.y.shape), self.y)),
                          columns=['X', 'hypo', 'y'])
        df = df.sort('X')
        plt.xlabel("Change in water level (x)")
        plt.ylabel("Water flowing out of the dam (y)")
        plt.scatter(df['X'], df['y'], marker='x', c='r', s=30, linewidth=2)
        plt.plot(df['X'], df['hypo'], linestyle='--', linewidth=3)
        plt.show()
コード例 #8
0
 def test_feature_normalization(self):
     X_norm, mu, sigma = feature_normalization(self.X)
     X_norm_loop, mu_loop, sigma_loop = feature_normalization_loop(self.X)
     
     expected_X_norm = np.array([
         [ 0.13, -0.22],
         [-0.5,  -0.22],
         [ 0.5,  -0.22],
         [-0.74, -1.54],
         [ 1.26,  1.09],
         [-0.02,  1.09],
         [-0.59, -0.22],
         [-0.72, -0.22],
         [-0.78, -0.22],
         [-0.64, -0.22],
         [-0.08,  1.09],
         [-0.0,  -0.22],
         [-0.14, -0.22],
         [ 3.12,  2.4 ],
         [-0.92, -0.22],
         [ 0.38,  1.09],
         [-0.86, -1.54],
         [-0.96, -0.22],
         [ 0.77,  1.09],
         [ 1.3,   1.09],
         [-0.29, -0.22],
         [-0.14, -1.54],
         [-0.5,  -0.22],
         [-0.05,  1.09],
         [ 2.38, -0.22],
         [-1.13, -0.22],
         [-0.68, -0.22],
         [ 0.66, -0.22],
         [ 0.25, -0.22],
         [ 0.8,  -0.22],
         [-0.2,  -1.54],
         [-1.26, -2.85],
         [ 0.05,  1.09],
         [ 1.43, -0.22],
         [-0.24,  1.09],
         [-0.71, -0.22],
         [-0.96, -0.22],
         [ 0.17,  1.09],
         [ 2.79,  1.09],
         [ 0.2,   1.09],
         [-0.42, -1.54],
         [ 0.3,  -0.22],
         [ 0.71,  1.09],
         [-1.01, -0.22],
         [-1.45, -1.54],
         [-0.19,  1.09],
         [-1.0,   -0.22],
     ])
     expected_mu = np.array([2000.68,3.17]).reshape((2,1))
     expected_sigma = np.array([794.7,0.76]).reshape((2,1))
     
     np.testing.assert_almost_equal(X_norm_loop, expected_X_norm, decimal=2)
     np.testing.assert_almost_equal(mu_loop, expected_mu, decimal=2)
     np.testing.assert_almost_equal(sigma_loop, expected_sigma, decimal=2)
     
     np.testing.assert_almost_equal(X_norm, expected_X_norm, decimal=2)
     np.testing.assert_almost_equal(mu, expected_mu, decimal=2)
     np.testing.assert_almost_equal(sigma, expected_sigma, decimal=2)