Python scatter plot and interpolation8/31/2023 ![]() ![]() ![]() fit ( X_train, y_train ) y_plot = model. scatter ( x_train, y_train, label = "training points" ) # polynomial features for degree in : model = make_pipeline ( PolynomialFeatures ( degree ), Ridge ( alpha = 1e-3 )) model. plot ( x_plot, f ( x_plot ), linewidth = lw, label = "ground truth" ) # plot training points ax. ![]() The surface is cut (due to nan values) along the convex hull of the input points, so even though our gridded arrays define a rectangular grid we only get a cut of the surface where interpolation makes sense.# plot function lw = 2 fig, ax = plt.(The fact that the red points are usually hidden by the surface is only due to how pyplot's renderer mishandles the relative position of complex 3d objects) Fortunately the input points are nice and smooth so everything goes well with interpolation. The surface nicely fits the red points, which is expected from interpolation.Here's the output from two angles (it's better to rotate around interactively such stills don't do the 3d representation justice): # create 3d Axes and plot surface and base pointsĪx = fig.add_subplot(111, projection='3d')Īx.plot_surface(xgrid, ygrid, z_interp, cmap='viridis', # interpolate z data same shape as xgrid and ygrid # go logarithmically in the y grid (considering y distribution) Just to demonstrate how the resulting interpolator is constrained to the convex hull of the input points, here's a surface plot of your data on a gridded mesh we create just for plotting: import matplotlib.pyplot as plt So even if extrapolation was possible I wouldn't believe it. Note that you'll have to stay inside the convex hull of the input points, otherwise you'll get nan (or whatever is passed as the fill_value keyword to the interpolator): > interpolator(2, 30)Įxtrapolation is usually meaningless anyway, and your input points are scattered in a bit erratic way: Now you can call this interpolator with 2 coordinates to give you the corresponding interpolated data point: > interpolator(x, y) = z Interpolator = interp.CloughTocher2DInterpolator(np.array().T, z) So we can do exactly that: import numpy as np Not to worry: griddata with 2d cubic interpolation uses a CloughTocher2DInterpolator. I'd default to using in this case, but you seem to want a callable interpolator, whereas griddata needs a given set of points onto which it will interpolate. You need 2d interpolation over scattered data. Where I can later call f(any (x,y) point within range) and get the corresponding z value. I am trying to use the data points I have above to find intermediate values so something similar to scipy's interp1d where f = interp1d(x, y, kind='cubic') I don't get values for x_,y_,z_ that make sense and I am not sure how to go from there. I was reading that using meshgrid I can interpolate with RegularGridInterpolator from scipy but I am not sure how to set it up when I do: x_,y_,z_ = np.meshgrid(x,y,z) # both indexing ij or xy I am trying to do 2d interpolation, so that I can compute a z value for any given input (x0, y0) point. Z = Įach entry of each list is read as a point so point 0 is (100,300,100) point 1 is (75,300,95) and so on.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |