Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:48:47+00:00 2026-06-14T16:48:47+00:00

I have a 3D array that I need to interpolate over one axis (the

  • 0

I have a 3D array that I need to interpolate over one axis (the last dimension). Let’s say y.shape = (nx, ny, nz), I want to interpolate in nz for every (nx, ny). However, I want to interpolate for a different value in each [i, j].

Here’s some code to exemplify. If I wanted to interpolate to a single value, say new_z, I’d use scipy.interpolate.interp1d like this

# y is a 3D ndarray
# x is a 1D ndarray with the abcissa values
# new_z is a number
f = scipy.interpolate.interp1d(x, y, axis=-1, kind='linear')
result = f(new_z)

However, for this problem what I actually want is to interpolate to a different new_z for each y[i, j]. So I do this:

# y is a 3D ndarray
# x is a 1D ndarray with the abcissa values
# new_z is a 2D array
result = numpy.empty(y.shape[:-1])
for i in range(nx):
    for j in range(ny):
        f = scipy.interpolate.interp1d(x, y[i, j], axis=-1, kind='linear')
        result[i, j] = f(new_z[i, j])

Unfortunately, with multiple loops this becomes inefficient and slow. Is there a better way to do this kind of interpolation? Linear interpolation is sufficient. A possibility is to implement this in Cython, but I was trying to avoid that because I want to have the flexibility of changing to cubic interpolation and don’t want to do it by hand in Cython.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:48:49+00:00Added an answer on June 14, 2026 at 4:48 pm

    To speedup high order interpolate, you can call interp1d() only once, and then use the _spline attribute and the low level function _bspleval() in the _fitpack module. Here is the code:

    from scipy.interpolate import interp1d
    import numpy as np
    
    nx, ny, nz = 30, 40, 50
    x = np.arange(0, nz, 1.0)
    y = np.random.randn(nx, ny, nz)
    new_x = np.random.random_integers(1, (nz-1)*10, size=(nx, ny))/10.0
    
    def original_interpolation(x, y, new_x):
        result = np.empty(y.shape[:-1])
        for i in xrange(nx):
            for j in xrange(ny):
                f = interp1d(x, y[i, j], axis=-1, kind=3)
                result[i, j] = f(new_x[i, j])
        return result
    
    def fast_interpolation(x, y, new_x):
        from scipy.interpolate._fitpack import _bspleval
        f = interp1d(x, y, axis=-1, kind=3)
        xj,cvals,k = f._spline
        result = np.empty_like(new_x)
        for (i, j), value in np.ndenumerate(new_x):
            result[i, j] = _bspleval(value, x, cvals[:, i, j], k, 0)
        return result
    
    r1 = original_interpolation(x, y, new_x)
    r2 = fast_interpolation(x, y, new_x)
    
    >>> np.allclose(r1, r2)
    True
    
    %timeit original_interpolation(x, y, new_x)
    %timeit fast_interpolation(x, y, new_x)
    1 loops, best of 3: 3.78 s per loop
    100 loops, best of 3: 15.4 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of float that I need to send over TCP/IP, the
I have a php array that has a bunch of data that I need
I have an array in javascript that I need to join and send through
I just have the array of 15 values that all need to be inserted
I have following array, that I need to operate by hand on bitmaps. const
I have a file that I need to parse into an array but I
I need to get array of images that have to call from server and
I have a function that takes an array of objects and I need to
I have a method that takes an array of queries, and I need to
I have a PHP file that returns a JSON array. I need to extract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.