def test_predict_output(self): d, n = (3, 10) sx = LHS( xlimits=np.repeat(np.atleast_2d([0.0, 1.0]), d, axis=0), criterion="m", random_state=42, ) x = sx(n) sy = LHS( xlimits=np.repeat(np.atleast_2d([0.0, 1.0]), 1, axis=0), criterion="m", random_state=42, ) y = sy(n) kriging = KPLS(n_comp=2) kriging.set_training_values(x, y) kriging.train() x_fail_1 = np.asarray([0, 0, 0, 0]) x_fail_2 = np.asarray([0]) self.assertRaises(ValueError, lambda: kriging.predict_values(x_fail_1)) self.assertRaises(ValueError, lambda: kriging.predict_values(x_fail_2)) var = kriging.predict_variances(x) self.assertEqual(y.shape[0], var.shape[0])
def test_kpls_training_with_zeroed_outputs(self): # Test scikit-learn 0.24 regression cf. https://github.com/SMTorg/smt/issues/274 x = np.random.rand(50, 3) y = np.zeros(50) kpls = KPLS() kpls.set_training_values(x, y) kpls.train() x_test = np.asarray([[0, 0, 0], [0.5, 0.5, 0.5], [1, 1, 1]]) y_test = kpls.predict_values(x_test) # KPLS training fails anyway but not due to PLS exception StopIteration self.assertEqual(np.linalg.norm(y_test - np.asarray([[0, 0, 0]])), 0)
def test_predict_output(self): x = np.random.random((10, 3)) y = np.random.random((10)) kriging = KPLS(n_comp=2) kriging.set_training_values(x, y) kriging.train() x_fail_1 = np.asarray([0, 0, 0, 0]) x_fail_2 = np.asarray([0]) self.assertRaises(ValueError, lambda: kriging.predict_values(x_fail_1)) self.assertRaises(ValueError, lambda: kriging.predict_values(x_fail_2)) var = kriging.predict_variances(x) self.assertEqual(y.shape[0], var.shape[0])
def test_kpls(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KPLS xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) sm = KPLS(theta0=[1e-2]) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) # estimated variance s2 = sm.predict_variances(x) # to compute the derivative according to the first variable dydx = sm.predict_derivatives(xt, 0) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show() # add a plot with variance plt.plot(xt, yt, "o") plt.plot(x, y) plt.fill_between( np.ravel(x), np.ravel(y - 3 * np.sqrt(s2)), np.ravel(y + 3 * np.sqrt(s2)), color="lightgrey", ) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction", "Confidence Interval 99%"]) plt.show()
def test_kpls(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KPLS xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) yt = np.array([0.0, 1.0, 1.5, 0.5, 1.0]) sm = KPLS(theta0=[1e-2]) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0.0, 4.0, num) y = sm.predict_values(x) plt.plot(xt, yt, "o") plt.plot(x, y) plt.xlabel("x") plt.ylabel("y") plt.legend(["Training data", "Prediction"]) plt.show()
def test_kpls(self): import numpy as np import matplotlib.pyplot as plt from smt.surrogate_models import KPLS xt = np.array([0., 1., 2., 3., 4.]) yt = np.array([0., 1., 1.5, 0.5, 1.0]) sm = KPLS(theta0=[1e-2]) sm.set_training_values(xt, yt) sm.train() num = 100 x = np.linspace(0., 4., num) y = sm.predict_values(x) plt.plot(xt, yt, 'o') plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.legend(['Training data', 'Prediction']) plt.show()