OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Much faster interpolation in Python?

  • Thread starter Thread starter Anna Majewska
  • Start date Start date
A

Anna Majewska

Guest
I am looking for a very fast interpolation in Python. Here is my code:

Code:
from scipy.integrate import quad
import numpy as np
from scipy import interpolate
import time
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

input="-0.5 0.0 \
-0.4 0.6 \
-0.3 0.9    \
-0.2 0.85 \
-0.1 0.82 \
0.0 0.8 \
0.1 0.7 \
0.2 0.6 \
0.3 0.4 \
0.4 0.3 \
0.5 0.02"

start_time = time.time()

input_coordinates = np.genfromtxt(input.splitlines()).reshape(-1,2) # shape to 2 columns, any number of rows
x_coordinates = input_coordinates[:,0]
H_values = input_coordinates[:,1]
H_interpolation = interpolate.InterpolatedUnivariateSpline(x_coordinates, H_values)
# H_interpolation = interp1d(x_coordinates, H_values)
# H_interpolation = interp1d(x_coordinates, H_values, kind='cubic')

def function(x):
    return H_interpolation(x)*np.exp(2/np.sqrt(1+x))

complex_integral = quad(function, -0.5, 0.5)

print("Quad",complex_integral)

print("--- %s seconds ---" % (time.time() - start_time))

xnew = np.arange(-0.5, 0.5, 0.01)
ynew = H_interpolation(xnew)   # use interpolation function returned by `interp1d`
plt.plot(x_coordinates, H_values, '.', label='original data')
plt.plot(xnew, ynew, '-', label='interpolation')
plt.legend()
plt.show()

Where for:

Code:
interpolate.InterpolatedUnivariateSpline

time is 0.011002779006958008 seconds and for:

Code:
interp1d type linear

time is 0.05301189422607422 seconds and for:

Code:
interp1d type cubic

time is 0.03500699996948242 seconds.

But I am looking for something really much faster due to multiple calculations in huge loops. Is there any much faster function approximation in Python? It should be accurate too.

I observed that if I reduce number of input points in

Code:
input

the time of calculation also drops, but I don't have much possibilities for reducing the number of points in input data.
<p>I am looking for a very fast interpolation in Python. Here is my code:</p>
<pre><code>from scipy.integrate import quad
import numpy as np
from scipy import interpolate
import time
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

input="-0.5 0.0 \
-0.4 0.6 \
-0.3 0.9 \
-0.2 0.85 \
-0.1 0.82 \
0.0 0.8 \
0.1 0.7 \
0.2 0.6 \
0.3 0.4 \
0.4 0.3 \
0.5 0.02"

start_time = time.time()

input_coordinates = np.genfromtxt(input.splitlines()).reshape(-1,2) # shape to 2 columns, any number of rows
x_coordinates = input_coordinates[:,0]
H_values = input_coordinates[:,1]
H_interpolation = interpolate.InterpolatedUnivariateSpline(x_coordinates, H_values)
# H_interpolation = interp1d(x_coordinates, H_values)
# H_interpolation = interp1d(x_coordinates, H_values, kind='cubic')

def function(x):
return H_interpolation(x)*np.exp(2/np.sqrt(1+x))

complex_integral = quad(function, -0.5, 0.5)

print("Quad",complex_integral)

print("--- %s seconds ---" % (time.time() - start_time))

xnew = np.arange(-0.5, 0.5, 0.01)
ynew = H_interpolation(xnew) # use interpolation function returned by `interp1d`
plt.plot(x_coordinates, H_values, '.', label='original data')
plt.plot(xnew, ynew, '-', label='interpolation')
plt.legend()
plt.show()
</code></pre>
<p>Where for:</p>
<pre><code>interpolate.InterpolatedUnivariateSpline
</code></pre>
<p>time is 0.011002779006958008 seconds
and for:</p>
<pre><code>interp1d type linear
</code></pre>
<p>time is 0.05301189422607422 seconds
and for:</p>
<pre><code>interp1d type cubic
</code></pre>
<p>time is 0.03500699996948242 seconds.</p>
<p>But I am looking for something really much faster due to multiple calculations in huge loops. Is there any much faster function approximation in Python? It should be accurate too.</p>
<p>I observed that if I reduce number of input points in</p>
<pre><code>input
</code></pre>
<p>the time of calculation also drops, but I don't have much possibilities for reducing the number of points in input data.</p>
 

Latest posts

G
Replies
0
Views
1
Gamal Othman
G
Top