Cara menggunakan python fast interpolation

Learn the fundamentals of neural networks and how to build deep learning models using Keras 2.0 in Python.

See DetailsRight Arrow

Start Course

Introduction to Natural Language Processing in Python

Beginner

4 hr

94.4K

Learn fundamental natural language processing techniques using Python and how to apply them to extract insights from real-world text data.

There are several general facilities available in SciPy for interpolation and smoothing for data in 1, 2, and higher dimensions. The choice of a specific interpolation routine depends on the data: whether it is one-dimensional, is given on a structured grid, or is unstructured. One other factor is the desired smoothness of the interpolator. In short, routines recommended for interpolation can be summarized as follows:

kind

routine

continuity

comment

1D

linear

piecewise continuous

comes from numpy

cubic spline

2nd derivative

monotone cubic spline

1st derivative

non-overshooting

non-cubic spline

(k-1)th derivative

k=3 is equivalent to

nearest

kind=’nearest’, ‘previous’, ‘next’

N-D curve

nearest, linear, spline

(k-1)th derivative

use N-dim y array

N-D regular (rectilinear) grid

nearest

method=’nearest’

linear

method=’linear’

splines

2nd derivatives

method=’cubic’, ‘quintic’

monotone splines

1st derivatives

method=’pchip’

N-D scattered

nearest

alias:

linear

cubic (2D only)

1st derivatives

radial basis function

For data smoothing, for 1- and 2-D data using cubic splines, based on the FORTRAN library FITPACK.

Additionally, routines are provided for interpolation / smoothing using with several kernels.

Futher details are given in the links below.

  • 1-D interpolation
  • Piecewise polynomials and splines
  • Smoothing splines
  • Multivariate data interpolation on a regular grid (RegularGridInterpolator)
  • Scattered data interpolation (numpy.interp1)
  • Extrapolation tips and tricks

Given a masked NumPy array of dimensionality (frames, points, 2) representing a video of frames frames, where in each frame points (x, y) points are being tracked.

I would like to interpolate this video from frames frames to any number of frames, very fast, hopefully with a cubic spline, but any other continuous interpolation will work OK too.


The naive solution I have implemented splits the array into 2 arrays of dimensions (frames, points) for the X array and Y array. Then, I transpose the array to (points, frames). For each row (single point over-time) I map it to an index and value, so the array (frames, points, 2)0 becomes:

(frames, points, 2)1

I feed this to a (frames, points, 2)2, and run on my new array which is for example (frames, points, 2)3 and get a new (frames, points, 2)4 array that I then convert back to NumPy.

This process removes the masks for intermittent frames (which is fine with me).

Current performance: Small array of shape (frames, points, 2)5 to (frames, points, 2)6

  • Cubic interpolation 0.115 seconds
  • Linear interpolation 0.112 seconds

Note!

This is a masked array, like (frames, points, 2)0. so it is important to incorporate the masks in the interpolation in order to not have 0 values (the data array looks like (frames, points, 2)8!