def test_transform_transformer(self): trans = Shell(input_type="filename") a = AtomKernel(transformer=trans) a.fit(ALL) res = a.transform(ALL) try: numpy.testing.assert_array_almost_equal(RBF_KERNEL, res) except AssertionError as e: self.fail(e)
def test_laplace_kernel(self): # Set depth=2 so the comparison is not trivial trans = Shell(input_type="filename", depth=2) a = AtomKernel(transformer=trans, kernel="laplace", gamma=1.) a.fit(ALL) res = a.transform(ALL) try: numpy.testing.assert_array_almost_equal(LAPLACE_KERNEL, res) except AssertionError as e: self.fail(e)
def test_transform_features(self): trans = Shell(input_type="filename") feats = trans.fit_transform(ALL) a = AtomKernel() values = list(zip(feats, ALL_NUMS)) a.fit(values) res = a.transform(values) try: numpy.testing.assert_array_almost_equal(RBF_KERNEL, res) except AssertionError as e: self.fail(e)
def test_custom_kernel(self): # Set depth=2 so the comparison is not trivial trans = Shell(input_type="filename", depth=2) # Simple linear kernel a = AtomKernel(transformer=trans, kernel=lambda x, y: numpy.dot(x, numpy.transpose(y))) a.fit(ALL) res = a.transform(ALL) try: numpy.testing.assert_array_almost_equal(LINEAR_KERNEL, res) except AssertionError as e: self.fail(e)
def test_transform_before_fit(self): a = AtomKernel() with self.assertRaises(ValueError): a.transform(ALL)
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()