Interpolation of 2D arrays in python using interp2d - python

I´m trying to interpolate data from a 2D array in python using scipy.interpolate.interp2d. However, I don´t think I fully understand what it is doing ,so any help is appreciated. I am trying to do something similar to what is suggested here Link.
My code is below:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
from scipy.interpolate import griddata
p = np.array([[1,2,3,4,5,6,7,8,9,1],[1,2,3,4,5,6,7,8,9,1],
[1,6,3,4,8,6,7,8,9,1],[1,2,3,4,5,6,7,89,9,1],
[1,56,3,4,5,6,7,67,6,1],[1,2,3,4,7,6,7,8,9,1],
[1,2,3,5,5,6,7,6,9,1],[1,2,3,6,5,6,7,45,9,1],
[9,2,3,4,21,6,7,8,9,1],[1,8,3,3,5,6,7,5,9,1]])
print(pf)
mymin, mymax = 0, 9
X = np.linspace(mymin,mymax,10)
Y = np.linspace(mymin,mymax,10)
Xnew = np.linspace(mymin,mymax,20)
Ynew = np.linspace(mymin,mymax,20)
f = interpolate.interp2d(x,y,p,kind='cubic')
Po = f(Xnew,Ynew)
print(Po)
When I run it, the following error message is displayed:
ValueError: Invalid length for input z for non rectangular grid

Related

Hough Transform on arrays of coordinates(Stock prices)

I want to apply Hough Transform on stock prices (array of numbers).
I read OpenCV and scikit-image docs and examples ,but got nothing how to apply the transformation to the arrays of numbers instead of images.
I created 2D array from data. First dimension is X(simply index of data) and second dimension is close prices.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pywt as wt
from skimage.transform import (hough_line, hough_line_peaks,probabilistic_hough_line)
from matplotlib import cm
path = "22-31May-100Tick.csv"
df = pd.read_csv(path)
y = df.Close.values
x = np.arange(0,len(y),1)
data = []
for i in x:
a = [i,y[i]]
data.append(a)
data = np.array(data)
How is it possible to apply the transformation with OpenCV or sickit-image?
Thank you

Why does numpy give correct least squares solution but not scipy?

I am scratching my head over this very simple problem. Given this toy data:
randgen = np.random.RandomState(9)
npoints = 1000
noise = randgen.randn(npoints)
x = np.linspace(0, 1, npoints)
y = 5 + 10*x + noise
Solving this using numpy's least squares:
# design matrix::
X = np.ones((npoints, 2))
X[:,0] = np.copy(x)
p, res, rnk, s = np.linalg.lstsq(X, y)
p
gives a reasonable answer: array([ 9.94406755, 5.05954009]) for p. However, solving using scipy's least squares gives wildly different answer (which changes on each invocation of the function):
p, res, rnk, s = scipy.linalg.lstsq(X, y)
p
An example solution is array([ 1.16328381e+08, -2.26560583e+06]). I don't understand what I am missing. I encountered this problem when using Scikit-learn's LinearRegression which internally uses scipy's lstsq. That was giving me weird answers.
Edit:
Numpy version: 1.11.2
Scipy version: 0.18.1
Python: 3.5
Edit 2:
I have realized that loading a particular library before loading scipy is causing this problem. The following order of loading libraries causes problem:
import numpy as np
from numpy.ma import MaskedArray
from matplotlib import pyplot as plt
from netCDF4 import Dataset
import matplotlib as mpl
from mpl_toolkits.basemap import Basemap
from pyeemd import ceemdan
from scipy.sparse.linalg import svds
from sklearn.utils.extmath import svd_flip
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator
from scipy.signal import convolve, boxcar
If I removed the from pyeemd import ceemdan line then the problem is solved! This raises the following question: why could this be happening?

searching for min(y) with the corresponding x (x and y are arrays)

`
import numpy as np
import matplotlib.pyplot as plt
x = np.array([3900.06,3900.16,3900.26])
y = np.array([0.311254,0.588623,0.724301])
if min(y):
print(x[min(y)])`
maybe someone can help me with the following problem:
I'm searching for the x point where the point y is at minimum.
enter image description here
Thanks, for any kind of help :)
Without testing whether or not y has a min (not sure if this is a requirement for the application without more source), using np.argmin():
import numpy as np
x = np.array([3900.06, 3900.16, 3900.26])
y = np.array([0.311254, 0.588623, 0.724301])
print(x[np.argmin(y)])

Plotting 2D integral function in python

Here is my first steps within the NumPy world.
As a matter of fact the target is plotting below 2-D function as a 3-D mesh:
N = \frac{n}{2\sigma\sqrt{\pi}}\exp^{-\frac{n^{2}x^{2}}{4\sigma^{2}}}
That could been done as a piece a cake in Matlab with below snippet:
[x,n] = meshgrid(0:0.1:20, 1:1:100);
mu = 0;
sigma = sqrt(2)./n;
f = normcdf(x,mu,sigma);
mesh(x,n,f);
But the bloody result is ugly enough to drive me trying Python capabilities to generate scientific plots.
I searched something and found that the primary steps to hit above mark in Pyhton might be acquired by below snippet:
from matplotlib.patches import Polygon
import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sigma = 1
def integrand(x,n):
return (n/(2*sigma*np.sqrt(np.pi)))*np.exp(-(n**2*x**2)/(4*sigma**2))
t = np.linespace(0, 20, 0.01)
n = np.linespace(1, 100, 1)
lower_bound = -100000000000000000000 #-inf
upper_bound = t
tt, nn = np.meshgrid(t,n)
real_integral = quad(integrand(tt,nn), lower_bound, upper_bound)
Axes3D.plot_trisurf(real_integral, tt,nn)
Edit: With due attention to more investigations on Greg's advices, above code is the most updated snippet.
Here is the generated exception:
RuntimeError: infinity comparisons don't work for you
It is seemingly referring to the quad call...
Would you please helping me to handle this integrating-plotting problem?!...
Best
Just a few hints to get you in the right direction.
numpy.meshgrid can do the same as MatLABs function:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html
When you have x and n you can do math just like in matlab:
sigma = numpy.sqrt(2)/n
(in python multiplication/division is default index by index - no dot needed)
scipy has a lot more advanced functions, see for example How to calculate cumulative normal distribution in Python for a 1D case.
For plotting you can use matplotlibs pcolormesh:
import matplotlib.pyplot as plt
plt.pcolormesh(x,n,real_integral)
Hope this helps until someone can give you a more detailed answer.

Convert vtkPoints to numpy array?

I am using Mayavi2 in a Python script to calculate 3d iso-surfaces. As a result I get a vtkPoints object. Now I want to convert this vtkPoints object ('vtkout' in the code sample below) to a simple numpy array with 3 lines containing all x, y and z values.
I get vtkout using a code like this:
import numpy
from enthought.mayavi import mlab
import array
randVol = numpy.random.rand(50,50,50) # fill volume with some random potential
X, Y, Z = numpy.mgrid[0:50, 0:50, 0:50] # grid
surf = mlab.contour3d(X, Y, Z, randVol, contours=[0.5]) # calc contour
vtkout = surf.contour.contour_filter.output.points # get the vtkPoints object
At the moment I use the following code to extract the points into an array:
pointsArray = numpy.zeros((3, vtkout.number_of_points))
for n in range(vtkout.number_of_points):
pointsArray[0,n] = vtkout[n][0]
pointsArray[1,n] = vtkout[n][1]
pointsArray[2,n] = vtkout[n][2]
I wonder if there is no general routine doing such conversions for me in a convenient, fast and safe way?
vtk_points.to_array() did not work for me (to_array() doesn't seem to exist in plain vtk).
What has actually worked in my case is using the numpy_support module:
from vtk.util import numpy_support
as_numpy = numpy_support.vtk_to_numpy(vtk_points.GetData())
As confirmed from comments on the original post, you might try:
vtkout.to_array().T
This is a direct method that does not require looping.

Categories