Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong code in [7] to plot the Rosenbrock function ? #628

Open
jason990420 opened this issue Jul 19, 2024 · 5 comments
Open

Wrong code in [7] to plot the Rosenbrock function ? #628

jason990420 opened this issue Jul 19, 2024 · 5 comments

Comments

@jason990420
Copy link

For statements in [7] of https://github.com/SMTorg/smt/blob/master/tutorial/SMT_Tutorial.ipynb, the shapes for x, y are different from z, and it will get following figure which is different from the figure shown on that web page.

ax.scatter(xt[:, 0], xt[:, 1], yt, zdir='z', marker='x', c='b', s=200, label='Training point')
ax.scatter(xtest[:, 0], xtest[:, 1], ytest, zdir='z', marker='.', c='k', s=200, label='Validation point')

image

Maybe it should be revised as following, but it looks like that there's still a little different.

ax.scatter(xt[:, 0], xt[:, 1], yt[:, 0], zdir='z', marker='x', c='b', s=200, label='Training point')
ax.scatter(xtest[:, 0], xtest[:, 1], ytest[:, 0], zdir='z', marker='.', c='k', s=200, label='Validation point')

image

@Paul-Saves
Copy link
Contributor

Thank you very much for noticing the problem and proposing a fix! It most likely is related to the release of numpy 2.0.0.

@jason990420
Copy link
Author

Installed numpy on my windows: 1.26.4.

@relf
Copy link
Member

relf commented Nov 8, 2024

I can not reproduce the problem in my environment. What are your Python, matplotlib versions?

@jason990420
Copy link
Author

jason990420 commented Nov 8, 2024

  • WIN10
  • python: 3.12.4
  • matplotlib: 3.9.2
  • numpy: 2.1.1

Reduced code from https://github.com/SMTorg/smt/blob/master/tutorial/SMT_Tutorial.ipynb

import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
from smt.problems import Rosenbrock
from smt.sampling_methods import LHS

########### Initialization of the problem, construction of the training and validation points

ndim = 2
ndoe = 20  # int(10*ndim)

# Define the function
fun = Rosenbrock(ndim=ndim)

# Construction of the DOE
# in order to have the always same LHS points, random_state=1
sampling = LHS(xlimits=fun.xlimits, criterion='ese', random_state=1)
xt = sampling(ndoe)
# Compute the outputs
yt = fun(xt)

# Construction of the validation points
ntest = 200 #500
sampling = LHS(xlimits=fun.xlimits, criterion='ese', random_state=1)
xtest = sampling(ntest)
ytest = fun(xtest)

# To plot the Rosenbrock function
x = np.linspace(-2, 2, 50)
res = []
for x0 in x:
    for x1 in x:
        res.append(fun(np.array([[x0, x1]])))
res = np.array(res)
res = res.reshape((50, 50)).T
X, Y = np.meshgrid(x, x)
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(projection="3d")
surf = ax.plot_surface(
    X, Y, res, cmap=cm.viridis, linewidth=0, antialiased=False, alpha=0.5
)

# Bad statements
ax.scatter(xt[:, 0], xt[:, 1], yt, zdir="z", marker="x", c="b", s=200, label="Training point")
ax.scatter(xtest[:, 0], xtest[:, 1], ytest, zdir="z", marker=".", c="k", s=200, label="Validation point")

# Updated statements
# ax.scatter(xt[:, 0], xt[:, 1], yt[:, 0], zdir='z', marker='x', c='b', s=200, label='Training point')
# ax.scatter(xtest[:, 0], xtest[:, 1], ytest[:, 0], zdir='z', marker='.', c='k', s=200, label='Validation point')

plt.title("Rosenbrock function")
plt.xlabel("x1")
plt.ylabel("x2")
plt.legend()
plt.show()

image

Note: Same result for Numpy 1.26.4

@relf
Copy link
Member

relf commented Nov 8, 2024

I've upgraded from matplotlib 3.8.3 to 3.9.2 and succeeded in reproducing the bug. Your change fixes it, thanks. We leave this issue opened till the notebook is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants