Пример #1
0
    def test_l1_l2(self):
        lmda1 = 0.001
        lmda2 = 0.001

        model = Sequential(verbose=1)
        model.add(
            Dense(10,
                  kernel_regularizer=l1_l2(lmda1, lmda2),
                  input_dim=2,
                  seed=1))
        model.add(Activation('sigmoid'))

        model.add(Dense(2, kernel_regularizer=l1_l2(lmda1, lmda2), seed=6))
        model.add(Activation('tanh'))

        model.add(Dense(2, kernel_regularizer=l1_l2(lmda1, lmda2), seed=6))
        model.add(Activation('softmax'))

        sgd = StochasticGradientDescent(learning_rate=0.05)
        model.compile(optimizer=sgd, loss="cross_entropy")

        model.fit(self.X_train, self.y_train, epochs=9, batch_size=2)
        print(model.layers[-2].biases)
        print(model.layers[-2].weights)

        expected_biases = np.array([[-0.95917324, -0.32783731]],
                                   dtype=np.float64)
        self.assertTrue(np.allclose(expected_biases, model.layers[-2].biases))

        expected_weights = np.array(
            [[0.71132812, -2.20343103], [1.44723471, -2.40020303]],
            dtype=np.float64)
        self.assertTrue(np.allclose(expected_weights,
                                    model.layers[-2].weights))
Пример #2
0
    def test_l2(self):
        lmda = 0.001

        model = Sequential(verbose=1)
        model.add(Dense(10, kernel_regularizer=l2(lmda), input_dim=2, seed=1))
        model.add(Activation('sigmoid'))

        model.add(Dense(2, kernel_regularizer=l2(lmda), seed=6))
        model.add(Activation('tanh'))

        model.add(Dense(2, kernel_regularizer=l2(lmda), seed=6))
        model.add(Activation('softmax'))

        sgd = StochasticGradientDescent(learning_rate=0.05)
        model.compile(optimizer=sgd, loss="cross_entropy")

        model.fit(self.X_train, self.y_train, epochs=10, batch_size=2)
        print(model.layers[-2].biases)
        print(model.layers[-2].weights)

        expected_biases = np.array([[-0.95917324, -0.32783731]],
                                   dtype=np.float64)
        self.assertTrue(np.allclose(expected_biases, model.layers[-2].biases))

        expected_weights = np.array(
            [[1.58872834, -1.65159914], [2.04547398, -1.63848661]],
            dtype=np.float64)
        self.assertTrue(np.allclose(expected_weights,
                                    model.layers[-2].weights))
Пример #3
0
 def test_eigen(self):
     eigen_vals, eigen_vecs = Decomposition.eigens(self.X)
     expected_eigen_vals = [2.05625000e+02, -2.84217094e-14, -3.48372238e-16]
     expected_eigen_vecs = [[0.82697677, -0.56223609, 0.02881115],
                            [-0.55131785, -0.81091744, -0.15430393],
                            [-0.11026357, -0.16218349, 0.98760327]]
     self.assertTrue(np.allclose(eigen_vals, expected_eigen_vals))
     self.assertTrue(np.allclose(eigen_vecs, expected_eigen_vecs))
Пример #4
0
    def test_rbf_kernel(self):
        # Tests RBF kernel of svc.
        X1 = Distribution.radial_binary(pts=100,
                                        mean=[0, 0],
                                        st=1,
                                        ed=2,
                                        seed=100)
        X2 = Distribution.radial_binary(pts=100,
                                        mean=[0, 0],
                                        st=4,
                                        ed=5,
                                        seed=100)

        Y1 = np.ones(X1.shape[0])
        Y2 = -np.ones(X1.shape[0])

        X_train = np.vstack((X1, X2))
        y_train = np.hstack((Y1, Y2))

        clf = svm.SVC(kernel='rbf', gamma=10)
        clf.fit(X_train, y_train)

        X1 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=1,
                                        ed=2,
                                        seed=100)
        X2 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=4,
                                        ed=5,
                                        seed=100)

        Y1 = np.ones(X1.shape[0])
        Y2 = -np.ones(X2.shape[0])

        X_test = np.vstack((X1, X2))
        y_test = np.hstack((Y1, Y2))

        predictions, projections = clf.predict(X_test, return_projection=True)

        expected_projections = np.array([
            1.2630574, 1.3302442, 1.502788, 1.2003369, 1.4567516, 1.0555044,
            1.434326, 1.4227715, 1.1069533, 1.104987, -1.6992458, -1.5001097,
            -1.0005158, -1.8284273, -1.0863144, -2.238042, -1.2274336,
            -1.2235101, -2.1250129, -2.0870237
        ], )

        self.assertTrue(np.allclose(projections, expected_projections))
        self.assertTrue(np.allclose(predictions, y_test))
Пример #5
0
    def test_pca(self):
        principal_components, rescaled_X = Decomposition.pca(self.X, num_components=3, return_scaled=True)
        expected_rescaled_X = [[1.81383571e+01, 2.22044605e-16, 3.33066907e-16],
                                [9.06917857e+00, 1.11022302e-16, 1.66533454e-16],
                                [0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
                                [-9.06917857e+00, -1.11022302e-16, -1.66533454e-16],
                                [-1.81383571e+01, -2.22044605e-16, -3.33066907e-16]]
        self.assertTrue(np.allclose(rescaled_X, expected_rescaled_X))

        expected_principal_components = [[0.82697677, 0.02881115, -0.56223609],
                               [-0.55131785, -0.15430393, -0.81091744],
                               [-0.11026357, 0.98760327, -0.16218349]]
        self.assertTrue(np.allclose(principal_components, expected_principal_components))

        reconstructed_x = np.dot(rescaled_X, principal_components.T) + self.X.mean(axis=0)
        self.assertTrue(np.allclose(reconstructed_x, self.X))
Пример #6
0
    def test_predictions(self):
        skl_db = skl_DBSCAN(self.eps, self.min_points)
        skl_db.fit(self.X)

        fs2ml_db = fs2ml_DBSCAN(self.eps, self.min_points)
        labels = fs2ml_db.fit_predict(self.X)
        self.assertTrue(np.allclose(np.array(labels, dtype=np.int64), np.array(skl_db.labels_, dtype=np.int64)))
Пример #7
0
    def test_linear_kernel(self):
        # Tests linear kernel of svc.
        X1 = Distribution.linear(pts=100,
                                 mean=[8, 10],
                                 covr=[[1.5, 1], [1, 1.5]],
                                 seed=100)
        X2 = Distribution.linear(pts=100,
                                 mean=[9, 5],
                                 covr=[[1.5, 1], [1, 1.5]],
                                 seed=100)

        Y1 = np.ones(X1.shape[0])
        Y2 = -np.ones(X2.shape[0])
        X_train = np.vstack((X1, X2))
        y_train = np.hstack((Y1, Y2))

        clf_lin = svm.SVC(kernel='linear')
        clf_lin.fit(X_train, y_train)

        X1 = Distribution.linear(pts=10,
                                 mean=[8, 10],
                                 covr=[[1.5, 1], [1, 1.5]],
                                 seed=100)
        X2 = Distribution.linear(pts=10,
                                 mean=[9, 5],
                                 covr=[[1.5, 1], [1, 1.5]],
                                 seed=100)

        Y1 = np.ones(X1.shape[0])
        Y2 = -np.ones(X2.shape[0])

        X_test = np.vstack((X1, X2))
        y_test = np.hstack((Y1, Y2))

        predictions, projections = clf_lin.predict(X_test,
                                                   return_projection=True)

        expected_projections = np.array([
            5.2844825, 2.8846788, 3.898558, 2.4527097, 4.271367, 4.6425023,
            5.170607, 3.3408344, 5.3939104, 2.779106, -2.909471, -5.3092747,
            -4.2953954, -5.7412434, -3.9225864, -3.551451, -3.0233462,
            -4.853119, -2.8000426, -5.4148474
        ])
        self.assertTrue(np.allclose(projections, expected_projections))
        self.assertTrue(np.allclose(predictions, y_test))
Пример #8
0
    def test_predictions(self):
        skl_km = skl_KMeans(n_clusters=self.n_clusters, random_state=5)
        skl_km.fit(self.X)
        skl_labels = sorted(np.array(skl_km.labels_, dtype=np.int64))

        fs2ml_km = fs2ml_KMeans(n_clusters=self.n_clusters, seed=5)
        fs2ml_labels = fs2ml_km.fit_predict(self.X)
        fs2ml_labels = sorted(np.array(fs2ml_labels, dtype=np.int64))

        self.assertTrue(np.allclose(fs2ml_labels, skl_labels))
Пример #9
0
    def test_predictions(self):
        sk_knn = sk_KNeighborsClassifier()
        sk_knn.fit(self.Xtrain, self.Ytrain)

        fs2ml_knn = fs2ml_KNeighborsClassifier()
        fs2ml_knn.fit(self.Xtrain, self.Ytrain)

        sk_labels = sk_knn.predict(self.Xtest)
        fs2ml_labels = fs2ml_knn.predict(self.Xtest)

        self.assertTrue(np.allclose(sk_labels, fs2ml_labels))
Пример #10
0
    def __get_new_centers(self, centers):
        converged = True

        for label in range(self.n_clusters):
            indices = np.where(self.labels == label)
            pts_in_cluster = self.X[indices]
            new_center = pts_in_cluster.mean(0)

            if not np.allclose(centers[label], new_center):
                centers[label] = new_center
                converged = False

        return centers, converged
Пример #11
0
 def test_cov_matrix(self):
     cov_matrix = Decomposition.cov_matrix(self.X, self.X)
     expected_cov_matrix = [[140.625, -93.75, -18.75],
                            [-93.75, 62.5, 12.5],
                            [-18.75, 12.5, 2.5]]
     self.assertTrue(np.allclose(cov_matrix, expected_cov_matrix))
Пример #12
0
    def test_poly_kernel(self):
        # Tests polynomial kernel of svc.
        X1 = Distribution.linear(pts=50,
                                 mean=[8, 20],
                                 covr=[[1.5, 1], [1, 2]],
                                 seed=100)
        X2 = Distribution.linear(pts=50,
                                 mean=[8, 15],
                                 covr=[[1.5, -1], [-1, 2]],
                                 seed=100)

        X3 = Distribution.linear(pts=50,
                                 mean=[15, 20],
                                 covr=[[1.5, 1], [1, 2]],
                                 seed=100)
        X4 = Distribution.linear(pts=50,
                                 mean=[15, 15],
                                 covr=[[1.5, -1], [-1, 2]],
                                 seed=100)

        X1 = np.vstack((X1, X2))
        X2 = np.vstack((X3, X4))

        Y1 = np.ones(X1.shape[0])
        Y2 = -np.ones(X2.shape[0])

        X_train = np.vstack((X1, X2))
        y_train = np.hstack((Y1, Y2))

        clf = svm.SVC(kernel='polynomial', const=1, degree=2)
        clf.fit(X_train, y_train)

        X1 = Distribution.linear(pts=5,
                                 mean=[8, 20],
                                 covr=[[1.5, 1], [1, 2]],
                                 seed=100)
        X2 = Distribution.linear(pts=5,
                                 mean=[8, 15],
                                 covr=[[1.5, -1], [-1, 2]],
                                 seed=100)

        X3 = Distribution.linear(pts=5,
                                 mean=[15, 20],
                                 covr=[[1.5, 1], [1, 2]],
                                 seed=100)
        X4 = Distribution.linear(pts=5,
                                 mean=[15, 15],
                                 covr=[[1.5, -1], [-1, 2]],
                                 seed=100)

        X1 = np.vstack((X1, X2))
        X2 = np.vstack((X3, X4))

        Y1 = np.ones(X1.shape[0])
        Y2 = -np.ones(X2.shape[0])

        X_test = np.vstack((X1, X2))
        y_test = np.hstack((Y1, Y2))

        predictions, projections = clf.predict(X_test, return_projection=True)
        expected_projections = np.array([
            1.2630574, 1.3302442, 1.502788, 1.2003369, 1.4567516, 1.0555044,
            1.434326, 1.4227715, 1.1069533, 1.104987, -1.6992458, -1.5001097,
            -1.0005158, -1.8284273, -1.0863144, -2.238042, -1.2274336,
            -1.2235101, -2.1250129, -2.0870237
        ])
        expected_projections = np.array([
            1.9282368, 4.1053743, 4.449601, 2.8149981, 3.337817, 1.5934888,
            4.237419, 3.699658, 3.8548565, 2.8402433, -6.7378554, -2.9163127,
            -2.5978136, -4.833237, -4.421687, -5.2333884, -2.2744238,
            -3.0598483, -2.4422958, -3.890006
        ], )
        self.assertTrue(np.allclose(projections, expected_projections))
        self.assertTrue(np.allclose(predictions, y_test))
Пример #13
0
    def test_multiclass(self):
        X1 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=1,
                                        ed=2,
                                        seed=100)
        X2 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=4,
                                        ed=5,
                                        seed=100)
        X3 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=6,
                                        ed=7,
                                        seed=100)
        X4 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=8,
                                        ed=9,
                                        seed=100)

        Y1 = -np.ones(X1.shape[0])
        Y2 = np.ones(X2.shape[0])
        Y3 = 2 * np.ones(X3.shape[0])
        Y4 = 3000 * np.ones(X4.shape[0])

        X_train = np.vstack((X1, X2, X3, X4))
        y_train = np.hstack((Y1, Y2, Y3, Y4))

        clf = svm.SVC(kernel='rbf', gamma=10)
        clf.fit(X_train, y_train)

        X1 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=1,
                                        ed=2,
                                        seed=100)
        X2 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=4,
                                        ed=5,
                                        seed=100)
        X3 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=6,
                                        ed=7,
                                        seed=100)
        X4 = Distribution.radial_binary(pts=10,
                                        mean=[0, 0],
                                        st=8,
                                        ed=9,
                                        seed=100)

        X_test = np.vstack((X1, X2, X3, X4))

        _, projections = clf.predict(X_test, return_projection=True)

        expected_projections = np.array([
            1.23564788, 1.15519477, 1.32441802, 1.04496554, 1.29740627, 0.,
            1.25561797, 1.22925452, 0., 1.11920321, 0.2991908, 0.23818634,
            0.55359011, 0.29655677, 0., 0.59992803, 0.52733203, 0.30456398,
            0.6027897, 0.33755249, 0., 0.04997651, 0.12099712, 0.12276944, 0.,
            0.19631702, 0.11836214, 0.06221966, 0.24539362, 0., 1.00000106,
            1.0000021, 1.00000092, 1.19952335, 1.00000283, 1.17741522,
            1.40596479, 1.60945299, 1.41534644, 1.27928235
        ])

        self.assertTrue(np.allclose(projections, expected_projections))