Linearly interpolate data in python



Python
library
pip

MeCab

numpy
digitize

mgrid

pad

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

scipy
interpolate
tkinter
postscript

image display

frame, grid

other
linear interpolation

OpenAI gym
CartPole-v0


Release date:2023/4/29         

In Japanese


■Description
Linearly interpolate the data to subdivide it.

■Example

import numpy as np

a      = np.array([1.2, 2.7, 0.3]) # data to linearly interpolate
b      = []                                  # Data storage location after linear interpolation
divnum = 4                               # Division number

for n in np.arange(a.shape[0]-1):
    for i in np.arange(divnum):
        c= round(a[n] + (a[n+1]-a[n])/divnum * i,2)  # Linear interpolation processing
        b.append(c)              # Store data
b.append(a[a.shape[0]-1]) # store last data

np.savetxt('test.txt', b,fmt="%.2f") # save to text


The results are as follows.

1.20
1.58
1.95
2.33
2.70
2.10
1.50
0.90
0.30

<Linear interpolation method using functions in python>

Linear interpolation can also be done using the linspace function, but linspace is a binary linear interpolation. Linear interpolation is also possible with the interpolate function, and linear interpolation of 2D data is also possible.









List of related articles



Python
library
pip

MeCab

numpy
digitize

mgrid

pad

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

scipy
interpolate
tkinter
postscript

image display

frame, grid

other
linear interpolation

OpenAI gym
CartPole-v0