I would like to determine the contour of a boolean function defined over two variables, each in the 0, 1 range. I do not have access to the explicit form of the function: it is a black box that returns a boolean for each pairs of variables, so I don't know how to convert it to a continuous function and use the first answer below. The region is convex, and it may reach the edges of the ranges of the parameters. Language is python. The function may be expensive to calculate, so the brute force grid approach I have below is slow in addition to being inaccurate. How can I improve things?
import numpy as np
import matplotlib.pyplot as plt
def f(x, y): return ((x-0.3)**2+(y-0.4)**2 <= 0.1) # Example of function; may extend outside region. In reality, form is not known.
xs, ys = np.linspace(0, 1, 11), np.linspace(0, 1, 11) # Brute force grid
for x in xs:
for y in ys: plt.plot(x, y, 'ro' if f(x, y) else 'b+')
cntr = [] # List of unique, approximate contour points
for x in xs:
col = [y for y in ys if f(x, y)]
if (col != []) and (not ((x, min(col)) in cntr)): cntr.append((x, min(col)))
if (col != []) and (not ((x, max(col)) in cntr)): cntr.append((x, max(col)))
for y in ys:
row = [x for x in xs if f(x, y)]
if (row != []) and (not ((min(row), y) in cntr)): cntr.append((min(row), y))
if (row != []) and (not ((max(row), y) in cntr)): cntr.append((max(row), y))
plt.plot(np.transpose(cntr)[0], np.transpose(cntr)[1], 'kv') # Contour in black
plt.show()
EDIT: illustration of effect of level on #Arne's answer:
Of course this is always going to be somewhat approximate, because you can't sample every point, but there are two functions that can help to simplify the code and speed things up: np.meshgrid() and plt.contour(). The latter allows specifying the levels you want to plot, so you can define a real-valued function by leaving the threshold out of your Boolean function, and pass the threshold as the level to plot to plt.contour() instead:
import numpy as np
import matplotlib.pyplot as plt
def f_continuous(x, y):
return (x - 0.3)**2 + (y - 0.4)**2 # real-valued instead of Boolean;
# define the 0.1 threshold in the contour plot function instead
xs, ys = np.linspace(0, 1, 11), np.linspace(0, 1, 11) # Brute force grid
X, Y = np.meshgrid(xs, ys)
plt.contour(X, Y, f_continuous(X, Y), levels=[0.1])
plt.show()
Edit: What if the function is a black box and can't be turned into a continuous one?
You can still use plt.contour(), by passing a value between the Boolean values 0 and 1 as the contour level. The interpolation just doesn't turn out as smooth in this case:
plt.contour(X, Y, f(X, Y), levels=[0.5])
plt.show()
As the answer you linked to notes, you can get the coordinates of the calculated points on the contour line by catching the QuadContourSet object returned by plt.contour() and accessing its allsegs attribute:
contour = plt.contour(X, Y, f(X, Y), levels=[0.5])
contour.allsegs
[[array([[0. , 0.25],
[0.05, 0.2 ],
[0.1 , 0.15],
...
[0.1 , 0.65],
[0.05, 0.6 ],
[0. , 0.55]])]]
Related
I want to be able to find the intersection between a line and a three-dimensional surface.
Mathematically, I have done this by taking the following steps:
Define the (x, y, z) coordinates of the line in a parametric manner. e.g. (x, y, z) = (1+t, 2+3t, 1-t)
Define the surface as a function. e.g. z = f(x, y)
Substitute the values of x, y, and z from the line into the surface function.
By solving, I would be able to get the intersection of the surface and the line
I want to know if there is a method for doing this in Python. I am also open to suggestions on more simple ways to solving for the intersection.
You can use the following code:
import numpy as np
import scipy as sc
import scipy.optimize
from matplotlib import pyplot as plt
def f(x, y):
""" Function of the surface"""
# example equation
z = x**2 + y**2 -10
return z
p0 = np.array([1, 2, 1]) # starting point for the line
direction = np.array( [1, 3, -1]) # direction vector
def line_func(t):
"""Function of the straight line.
:param t: curve-parameter of the line
:returns xyz-value as array"""
return p0 + t*direction
def target_func(t):
"""Function that will be minimized by fmin
:param t: curve parameter of the straight line
:returns: (z_line(t) - z_surface(t))**2 – this is zero
at intersection points"""
p_line = line_func(t)
z_surface = f(*p_line[:2])
return np.sum((p_line[2] - z_surface)**2)
t_opt = sc.optimize.fmin(target_func, x0=-10)
intersection_point = line_func(t_opt)
The main idea is to reformulate the algebraic equation point_of_line = point_of_surface (condition for intersection) into a minimization problem: |point_of_line - point_of_surface| → min. Due to the representation of the surface as z_surface = f(x, y) it is convenient to calculate the distance for a given t-value only on basis of the z-values. This is done in target_func(t). And then the optimal t-value is found by fmin.
The correctness and plausibility of the result can be checked with some plotting:
from mpl_toolkits.mplot3d import Axes3D
ax = plt.subplot(projection='3d')
X = np.linspace(-5, 5, 10)
Y = np.linspace(-5, 5, 10)
tt = np.linspace(-5, 5, 100)
XX, YY = np.meshgrid(X, Y)
ZZ = f(XX, YY)
ax.plot_wireframe(XX, YY, ZZ, zorder=0)
LL = np.array([line_func(t) for t in tt])
ax.plot(*LL.T, color="orange", zorder=10)
ax.plot([x], [y], [z], "o", color="red", ms=10, zorder=20)
Note that this combination of wire frame and line plots does not handle well, which part of the orange line should be below the blue wire lines of the surface.
Also note, that for this type of problem there might be any number of solutions from 0 up to +∞. This depends on the actual surface. fmin finds an local optimum, this might be a global optimum with target_func(t_opt)=0 or it might not. Changing the initial guess x0 might change which local optimum fmin finds.
I have 2 sets of points (X, Y). I want to:
Use polifit to fit the line
Given a Y predict an X
This is the dataset:
X Y
-0.00001 5.400000e-08
-0.00001 5.700000e-08
0.67187 1.730000e-07
1.99997 9.150000e-07
2.67242 1.582000e-06
4.00001 3.734000e-06
4.67193 5.414000e-06
5.99998 9.935000e-06
6.67223 1.311300e-05
8.00000 2.102900e-05
Which looks like this:
I have seen numpy has the function polyval. But here you pass an X and get a y. How do i reverse it.
As I said in the comments, you can subtract the y value, fit an appropriate degree polynomial, then find it's roots. numpy is easily good enough for that task.
Here is a simple example:
import numpy as np
x = np.arange(-10, 10.1, 0.3)
y = x ** 2
def find_x_from_y(x, y, deg, value, threshold=1E-6):
# subtract the y value, fit a polynomial, then find the roots of it
r = np.roots(np.polyfit(x, y - value, deg))
# return only the real roots.. due to numerical errors, you
# must introduce a threshold value to its complex part.
return r.real[abs(r.imag) < threshold]
>>> find_x_from_y(x, y, 2, 0.5)
array([ 0.70710678, -0.70710678])
Finding roots is a numerical algorithm, it produces the numerical approximation of the actual roots. This might result in really small, but nonzero imaginary parts. To avoid this, you need a small threshold to distingush real and imaginary roots. This is why you can't really use np.isreal:
>>> np.isreal(3.2+1E-7j)
False
A visual example with a 3 degree polynomial:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 10.1, 0.3)
y = x ** 3 - 3 * x ** 2 - 9 * x
def find_x_from_y(x, y, deg, value, threshold=1E-6):
r = np.roots(np.polyfit(x, y - value, deg))
return r.real[abs(r.imag) < threshold]
value = -10
rts = find_x_from_y(x, y, 3, value)
fig = plt.figure(figsize=(10, 10))
plt.plot(x, y)
plt.axhline(value, color="r")
for r in rts:
plt.axvline(r, color="k")
I am trying to run a least square algorithm using numpy and is having trouble. Can someone please tell me what I am doing wrong in the given code? When I set y to be y = np.power(X, 1) + np.random.rand(20)*3 or some other reasonable function of x, everything is working fine. But for that particular y defined by those given y values, the plot I am getting is senseless.
Is this some kind of numerical problem?
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
X = np.arange(1,21)
y = np.array([-0.00454712, -0.00457764, -0.0045166 , -0.00442505, -0.00427246,
-0.00411987, -0.00378418, -0.003479 , -0.00314331, -0.00259399,
-0.00213623, -0.00146484, -0.00082397, -0.00030518, 0.00027466,
0.00076294, 0.00146484, 0.00192261, 0.00247192, 0.00314331])
#y = np.power(X, 1) + np.random.rand(20)*3
w = np.linalg.lstsq(X.reshape(20, 1), y)[0]
plt.plot(X, y, 'red')
plt.plot(X, X*w[0], 'blue')
plt.show()
Are you sure there is a linear relationship between what you are fitting and the y variable data?
Using the code (y = np.power(X, 1) + np.random.rand(20)*3) from your example, you have a linear relationship built into the y variable itself (with some noise) which allows your plot to track relatively well with the linear equation.
X = np.arange(1,21)
#y = np.power(X, 1) + np.random.rand(20)*3
w = np.linalg.lstsq(X.reshape(20, 1), y)[0]
plt.plot(X, y, 'red')
plt.plot(X, X*w[0], 'blue')
plt.show()
However, when you alternate to something like your y variable
y = np.array([-0.00454712, -0.00457764, -0.0045166 , -0.00442505, -0.00427246,
-0.00411987, -0.00378418, -0.003479 , -0.00314331, -0.00259399,
-0.00213623, -0.00146484, -0.00082397, -0.00030518, 0.00027466,
0.00076294, 0.00146484, 0.00192261, 0.00247192, 0.00314331])
You end up with something less easy to fit.
Looking at the documentation, if you are attempting to something that fits this set of values, you will need to build in a constant component in which case lstsq does not do by default.
The docs state for lstsq
Return the least-squares solution to a linear matrix equation.
Solves the equation a x = b
If you really want to fit the data to a linear equation, running code like the below will give you something that almost matches your original data. However, the data behind this process seems to have polynomial/exponential driver which would make polyfit better.
X = np.arange(1,21)
y = np.array([-0.00454712, -0.00457764, -0.0045166 , -0.00442505, -0.00427246,
-0.00411987, -0.00378418, -0.003479 , -0.00314331, -0.00259399,
-0.00213623, -0.00146484, -0.00082397, -0.00030518, 0.00027466,
0.00076294, 0.00146484, 0.00192261, 0.00247192, 0.00314331])
#y = np.power(X, 1) + np.random.rand(20)*3
X2 = np.vstack([X, np.ones(len(X))]).T
w = np.linalg.lstsq(X2, y)[0]
plt.plot(X, y, 'red')
plt.plot(X, X.dot(w[0])+w[1], 'blue')
plt.show()
I'm new to Python so please be patient. I appreciate any help!
What I have: three 1D lists (xr, yr, zr), one containing x-values, the other two y- and z-values
What I want to do: create a 3D contour plot in matplotlib
I realized that I need to convert the three 1D lists into three 2D lists, by using the meshgrid function.
Here's what I have so far:
xr = np.asarray(xr)
yr = np.asarray(yr)
zr = np.asarray(zr)
X, Y = np.meshgrid(xr,yr)
znew = np.array([zr for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = znew.reshape(X.shape)
Running this gives me the following error (for the last line I entered above):
total size of new array must be unchanged
I went digging around stackoverflow, and tried using suggestions from people having similar problems. Here are the errors I get from each of those suggestions:
Changing the last line to:
Z = znew.reshape(X.shape[0])
Gives the same error.
Changing the last line to:
Z = znew.reshape(X.shape[0], len(znew))
Gives the error:
Shape of x does not match that of z: found (294, 294) instead of (294, 86436).
Changing it to:
Z = znew.reshape(X.shape, len(znew))
Gives the error:
an integer is required
Any ideas?
Well,sample code below works for me
import numpy as np
import matplotlib.pyplot as plt
xr = np.linspace(-20, 20, 100)
yr = np.linspace(-25, 25, 110)
X, Y = np.meshgrid(xr, yr)
#Z = 4*X**2 + Y**2
zr = []
for i in range(0, 110):
y = -25.0 + (50./110.)*float(i)
for k in range(0, 100):
x = -20.0 + (40./100.)*float(k)
v = 4.0*x*x + y*y
zr.append(v)
Z = np.reshape(zr, X.shape)
print(X.shape)
print(Y.shape)
print(Z.shape)
plt.contour(X, Y, Z)
plt.show()
TL;DR
import matplotlib.pyplot as plt
import numpy as np
def get_data_for_mpl(X, Y, Z):
result_x = np.unique(X)
result_y = np.unique(Y)
result_z = np.zeros((len(result_x), len(result_y)))
# result_z[:] = np.nan
for x, y, z in zip(X, Y, Z):
i = np.searchsorted(result_x, x)
j = np.searchsorted(result_y, y)
result_z[i, j] = z
return result_x, result_y, result_z
xr, yr, zr = np.genfromtxt('data.txt', unpack=True)
plt.contourf(*get_data_for_mpl(xr, yr, zr), 100)
plt.show()
Detailed answer
At the beginning, you need to find out for which values of x and y the graph is being plotted. This can be done using the numpy.unique function:
result_x = numpy.unique(X)
result_y = numpy.unique(Y)
Next, you need to create a numpy.ndarray with function values for each point (x, y) from zip(X, Y):
result_z = numpy.zeros((len(result_x), len(result_y)))
for x, y, z in zip(X, Y, Z):
i = search(result_x, x)
j = search(result_y, y)
result_z[i, j] = z
If the array is sorted, then the search in it can be performed not in linear time, but in logarithmic time, so it is enough to use the numpy.searchsorted function to search. but to use it, the arrays result_x and result_y must be sorted. Fortunately, sorting is part of the numpy.unique method and there are no additional actions to do. It is enough to replace the search (this method is not implemented anywhere and is given simply as an intermediate step) method with np.searchsorted.
Finally, to get the desired image, it is enough to call the matplotlib.pyplot.contour or matplotlib.pyplot.contourf method.
If the function value does not exist for (x, y) for all x from result_x and all y from result_y, and you just want to not draw anything, then it is enough to replace the missing values with NaN. Or, more simply, create result_z as numpy.ndarray` from NaN and then fill it in:
result_z = numpy.zeros((len(result_x), len(result_y)))
result_z[:] = numpy.nan
The bicubic spline interpolation is an extension of cubic spline for interpolating on a 2D regular grid. The interpolated surface is smoother than corresponding surfaces obtained by bilinear interpolation.
Does anyone have already the corresponding function that enables such an interpolation?
Here is the beginning of the code:
def bicubicspline_interpolation(x, y, points):
'''Interpolate (x,y) from values associated with four points.
The four points are a list of four triplets: (x, y, value).
The four points can be in any order. They should form a rectangle.
>>> bilinear_interpolation(12, 5.5,
... [(10, 4, 100),
... (20, 4, 200),
... (10, 6, 150),
... (20, 6, 300)])
165.0
'''
# See formula at: http://en.wikipedia.org/wiki/Bicubic_interpolationon
points = sorted(points) # order points by x, then by y
(x1, y1, q11), (_x1, y2, q12), (x2, _y1, q21), (_x2, _y2, q22) = points
if x1 != _x1 or x2 != _x2 or y1 != _y1 or y2 != _y2:
raise ValueError('points do not form a rectangle')
if not x1 <= x <= x2 or not y1 <= y <= y2:
raise ValueError('(x, y) not within the rectangle')
value =
return value
Any help please?
Thank you!
a
As #Will pointed out, scipy has some interpolation function. Check out griddata, as it has cubic interpolation. I just came up with a little example.
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
def func( x, y ):
return np.sin(x*12.0)*np.sin(y*20.0)
points = np.random.rand(1000, 2)
values = func(points[:,0], points[:,1])
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
grid_z = griddata(points, values, (grid_x, grid_y), method='cubic')
plt.imshow(grid_z.T, extent=(0,1,0,1), origin='lower')
plt.scatter(points[:,0], points[:,1], c='k')
plt.show()
At the risk of not answering your question directly, you may want to look into scipy interpolate functions. SciPy and family is usually a good idea for this sort of processing.
Take a look at the method found in 'Numerical Recipes in C++' under 'Higher Order Smoothness: Bicubic Interpolation'. Watch the indices on the derivative formulas, some should start at 1 not 0 and could cause a division by zero when evaluated at a control point. The grid here needs to be rectangular but the width and height do not need to be the same. The matrix listed is correct. I was able to produce the following interpolation (sans rendering) in 3 micro seconds.