Beispiel #1
0
 def test_fit_transform_transformer(self):
     trans = Shell(input_type="filename")
     a = AtomKernel(transformer=trans)
     res = a.fit_transform(ALL)
     try:
         numpy.testing.assert_array_almost_equal(RBF_KERNEL, res)
     except AssertionError as e:
         self.fail(e)
Beispiel #2
0
 def test_fit_transform_features(self):
     trans = Shell(input_type="filename")
     feats = trans.fit_transform(ALL)
     a = AtomKernel()
     values = list(zip(feats, ALL_NUMS))
     res = a.fit_transform(values)
     try:
         numpy.testing.assert_array_almost_equal(RBF_KERNEL, res)
     except AssertionError as e:
         self.fail(e)
Beispiel #3
0
 def test_same_element(self):
     # Set depth=2 so the comparison is not trivial
     trans = Shell(input_type="filename", depth=2)
     # Set gamma=1 to make the differences more noticeable
     a = AtomKernel(transformer=trans, same_element=False, gamma=1.)
     res = a.fit_transform(ALL)
     expected = numpy.array([[17.00000033, 14.58016505],
                             [14.58016505, 32.76067832]])
     try:
         numpy.testing.assert_array_almost_equal(expected, res)
     except AssertionError as e:
         self.fail(e)
Beispiel #4
0
 def test_invalid_kernel(self):
     with self.assertRaises(ValueError):
         trans = Shell(input_type="filename")
         a = AtomKernel(kernel=1, transformer=trans)
         a.fit_transform(ALL)
Beispiel #5
0
from molml.kernel import AtomKernel

from utils import load_qm7

if __name__ == "__main__":
    # This is just boiler plate code to load the data
    Xin_train, Xin_test, y_train, y_test = load_qm7()

    # Look at just a few examples to be quick
    n_train = 200
    n_test = 200
    Xin_train = Xin_train[:n_train]
    y_train = y_train[:n_train]
    Xin_test = Xin_test[:n_test]
    y_test = y_test[:n_test]

    gamma = 1e-7
    alpha = 1e-7
    kern = AtomKernel(gamma=gamma,
                      transformer=LocalEncodedBond(n_jobs=-1),
                      n_jobs=-1)
    K_train = kern.fit_transform(Xin_train)
    K_test = kern.transform(Xin_test)

    clf = KernelRidge(alpha=alpha, kernel="precomputed")
    clf.fit(K_train, y_train)
    train_error = MAE(clf.predict(K_train), y_train)
    test_error = MAE(clf.predict(K_test), y_test)
    print("Train MAE: %.4f Test MAE: %.4f" % (train_error, test_error))
    print()
Beispiel #6
0
from molml.kernel import AtomKernel

from utils import load_qm7


if __name__ == "__main__":
    # This is just boiler plate code to load the data
    Xin_train, Xin_test, y_train, y_test = load_qm7()

    # Look at just a few examples to be quick
    n_train = 200
    n_test = 200
    Xin_train = Xin_train[:n_train]
    y_train = y_train[:n_train]
    Xin_test = Xin_test[:n_test]
    y_test = y_test[:n_test]

    gamma = 1e-7
    alpha = 1e-7
    kern = AtomKernel(gamma=gamma, transformer=LocalEncodedBond(n_jobs=-1),
                      n_jobs=-1)
    K_train = kern.fit_transform(Xin_train)
    K_test = kern.transform(Xin_test)

    clf = KernelRidge(alpha=alpha, kernel="precomputed")
    clf.fit(K_train, y_train)
    train_error = MAE(clf.predict(K_train), y_train)
    test_error = MAE(clf.predict(K_test), y_test)
    print("Train MAE: %.4f Test MAE: %.4f" % (train_error, test_error))
    print()