Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Hello! Friend!!!

Thank you very much for reading haihong’s article, if there are any mistakes in the article, please point out ~

 

Self-introduction ଘ(੭, ᵕ)੭

Nickname: Haihong

Tag: programmer monkey | C++ contestant | student

Introduction: because of C language to get acquainted with programming, then transferred to the computer major, had the honor to get some national awards, provincial awards… Has been confirmed. Currently learning C++/Linux/Python

Learning experience: solid foundation + more notes + more code + more thinking + learn English well!

Little White stage of learning Python

The article is only used as my own study notes for the establishment of knowledge system and review

The problem is not to learn one more question to understand one more question

Know what is, know why!

1. One-dimensional interpolation

Interpolation: The approximation function over a known finite number of data points. The interpolation function passes through the sample points, and the fitting function is generally based on the least square method as close to all samples as possible and passes through them. The common difference methods include Lagrange interpolation, piecewise interpolation and spline interpolation.

Interp1d (x, y) computs one-dimensional interpolation

1.1 Linear interpolation and Spline Interpolation (B-spline)

Example 1: The voltage data of an electrical element is recorded in the range of 0~2.25πA and the relation between current and voltage is sinusoidal. Linear interpolation and spline interpolation methods are used to give the numerical approximation function curves passing through the data points.

The Demo code

import matplotlib
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt

# Introduce Chinese fonts
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)

# Initial data amount 0-2.25pi divided into 10 evenly divided
x = np.linspace(0.2.25 * np.pi, 10)
y = np.sin(x)

# Get the difference function (using linear interpolation)
f_linear = interpolate.interp1d(x, y)
New data 0-2.25pi divided into 100 evenly divided (linear interpolation)
x_new = np.linspace(0.2.25 * np.pi, 100)
y_new = f_linear(x_new)

# Use b-spline interpolation
tck = interpolate.splrep(x, y)
y_bspline = interpolate.splev(x_new, tck)

# visualization
plt.xlabel(U 'amp/A')
plt.ylabel(U 'V/V.)
plt.plot(x, y, "o", label=U "Raw data")
plt.plot(x_new, f_linear(x_new), label=U "Linear interpolation")
plt.plot(x_new, y_bspline, label="U" B - spline interpolation)
plt.legend()
plt.show()
Copy the code

Output:Knowledge points involved:

  • numpy.linspace
  • scipy.interpolate.interp1d
  • scipy.interpolate.splrep

1.2 Higher-order spline interpolation

As the number of interpolation nodes increases, the polynomial degree becomes higher, and the interpolation curve jumps in some areas and deviates more and more from the original curve, which is called runge phenomenon.

Example 2: The voltage data of an electrical element is recorded in the range of 0 10A and the relation between current and voltage satisfies sine function. The curves of numerical approximation function passing through data points are obtained by using order 0 5 spline interpolation method.

The Demo code

import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from scipy import interpolate
 
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)
Create a data point set
x = np.linspace(0.10.11)
y = np.sin(x)
Draw a set of data points
plt.figure(figsize=(12.9))
plt.plot(x, y, 'ro')
Create interp1D object f based on kind, calculate interpolation result
xnew = np.linspace(0.10.101)
Adjacency zero order linear second order
for kind in ['nearest'.'zero'.'linear'.'quadratic']:
    f = interpolate.interp1d(x, y, kind=kind)
    ynew = f(xnew)
    plt.plot(xnew, ynew, label=str(kind))
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.legend(loc="lower right")
plt.show()
Copy the code

Output:

Look at each interpolation method separately

1. Nearest = kind

import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from scipy import interpolate
 
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)

Create a data point set
x = np.linspace(0.10.11)
y = np.sin(x)

Theta is the interpolation function
f = interpolate.interp1d(x, y, kind='nearest')

# new data
x_new = np.linspace(0.10.101)
y_new = f(x_new)

# visualization
plt.plot(x, y, 'o', x_new, y_new, The '-')
plt.show()
Copy the code

2. When kind = zero

import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from scipy import interpolate
 
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)

Create a data point set
x = np.linspace(0.10.11)
y = np.sin(x)

Theta is the interpolation function
f = interpolate.interp1d(x, y, kind='zero')

# new data
x_new = np.linspace(0.10.101)
y_new = f(x_new)

# visualization
plt.plot(x, y, 'o', x_new, y_new, The '-')
plt.show()
Copy the code

3. When kind = linear

import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from scipy import interpolate
 
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)

Create a data point set
x = np.linspace(0.10.11)
y = np.sin(x)

Theta is the interpolation function
f = interpolate.interp1d(x, y, kind='linear')

# new data
x_new = np.linspace(0.10.101)
y_new = f(x_new)

# visualization
plt.plot(x, y, 'o', x_new, y_new, The '-')
plt.show()
Copy the code

4. When kind = QUADRATIC

import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from scipy import interpolate
 
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)

Create a data point set
x = np.linspace(0.10.11)
y = np.sin(x)

Theta is the interpolation function
f = interpolate.interp1d(x, y, kind='quadratic')

# new data
x_new = np.linspace(0.10.101)
y_new = f(x_new)

# visualization
plt.plot(x, y, 'o', x_new, y_new, The '-')
plt.show()
Copy the code

5. When kind = cubic

import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from scipy import interpolate
 
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)

Create a data point set
x = np.linspace(0.10.11)
y = np.sin(x)

Theta is the interpolation function
f = interpolate.interp1d(x, y, kind='cubic')

# new data
x_new = np.linspace(0.10.101)
y_new = f(x_new)

# visualization
plt.plot(x, y, 'o', x_new, y_new, The '-')
plt.show()
Copy the code

2. Two-dimensional interpolation

Interp2d (x, y, z, kind= “”) computes two-dimensional interpolation

2.1 Image blur processing — spline interpolation

Example 3: certain image expression as z = (x + y) – 5 e ∗ (x2 + y2) z = (x + y) e ^ {- 5 * (x ^ 2 + y ^ 2)} z = (x + y) – 5 e ∗ (x2 + y2), complete the two-dimensional interpolation image to make it clear.

The Demo code

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
 
 
def func(x, y) :
    return (x + y) * np.exp(-5.0 * (x ** 2 + y ** 2))

The x-y axis is divided into a 15*15 grid
# x, y = np.mgrid[-1:1:15j, -1:1:15j]
x = np.linspace(-1.1.15)
y = np.linspace(-1.1.15)
x, y = np.meshgrid(x, y)
fvals = func(x, y)

# Two-dimensional interpolation
newfunc = interpolate.interp2d(x, y, fvals, kind='cubic')

# Compute interpolation on a 100*100 grid
xnew = np.linspace(-1.1.100)
ynew = np.linspace(-1.1.100)
fnew = newfunc(xnew, ynew)
xnew, ynew = np.meshgrid(xnew, ynew)

plt.subplot(121)
Extent x - and Y-axis range
im1 = plt.imshow(fvals, extent=[-1.1, -1.1], interpolation="nearest", origin="lower",cmap="Reds")
plt.colorbar(im1)

plt.subplot(122)
im2 = plt.imshow(fnew, extent=[-1.1, -1.1], interpolation="nearest", origin="lower",cmap="Reds")
plt.colorbar(im2)
plt.show()
Copy the code

Output:

2.2 Three-dimensional diagram of two-dimensional interpolation

Example 4: certain image expression as z = (x + y) – 5 e ∗ (x2 + y2) z = (x + y) e ^ {- 5 * (x ^ 2 + y ^ 2)} z = (x + y) – 5 e ∗ (x2 + y2), to complete the 3 d image visualization of two-dimensional interpolation.

In fact, it is based on 3 two-dimensional interpolation to achieve 3d image rendering

The Demo code

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
from scipy import interpolate
import matplotlib.cm as cm
import matplotlib.pyplot as plt

def func(x, y) :
    return (x + y) * np.exp(-5.0 * (x ** 2 + y ** 2))

The x-y axis is divided into a 20 × 20 grid
x = np.linspace(-1.1.20)
y = np.linspace(-1.1.20)
x, y = np.meshgrid(x, y)
fvals = func(x, y)

# Draw sub-figure 1
fig = plt.figure(figsize=(9.6))
ax = plt.subplot(1.2.1, projection='3d')
surf = ax.plot_surface(x, y, fvals, rstride=2, cstride=2, cmap=cm.coolwarm, linewidth=0.5, antialiased=True)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x,y)')
plt.colorbar(surf, shrink=0.5, aspect=5)  # Add a color bar

# Two-dimensional interpolation
newfunc = interpolate.interp2d(x, y, fvals, kind='cubic')

# Compute interpolation on a 100*100 grid
xnew = np.linspace(-1.1.100)
ynew = np.linspace(-1.1.100)
fnew = newfunc(xnew, ynew)
xnew, ynew = np.meshgrid(xnew, ynew)

ax2 = plt.subplot(1.2.2, projection='3d')
surf2 = ax2.plot_surface(xnew, ynew, fnew, rstride=2, cstride=2, cmap=cm.coolwarm, linewidth=0.5, antialiased=True)
ax2.set_xlabel('xnew')
ax2.set_ylabel('ynew')
ax2.set_zlabel('fnew(x,y)')
plt.colorbar(surf2, shrink=0.5, aspect=5)

# marked
plt.show()
Copy the code

Output:

3. Least square fitting

Fitting refers to a number of discrete function values {f1,f2… ,fn}, by adjusting some undetermined coefficients f(λ1, λ2… ,λn), so that the difference between the function and the known point set (least squares sense) is minimized. If the undetermined function is linear, it is called a linear fit or regression (mainly in statistics), otherwise it is called a nonlinear fit or regression. The expression may also be a piecewise function, in which case it is called a spline fit. Geometrically, fitting is to find a continuous surface with known form and unknown parameters to approximate these points to the maximum extent given some points in space. Interpolation is finding a continuous surface (or surfaces that are piecewise smooth) to pass through these points. Selecting parameter C minimized the weighted square sum of residual (or deviation) ek= yK-f (xk, C) of the fitting model and the actual observed values at each point of curve fitting. At this point, the curve obtained is called the fitting curve of data in the sense of weighted least squares, which is called the least square method.

Knowledge points involved

from scipy.optimize import leastsq
Copy the code

Example 5: The voltage and current records of the following electrical elements were fitted by least square method and the corresponding curves were drawn. Current 8.19 2.72 6.39 8.71 4.7 2.66 3.78 Voltage (V) 7.01 2.78 6.47 6.71 4.1 4.23 4.05

The Demo code

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
 
# Introduce Chinese fonts
font = {
    "family": "Microsoft YaHei"
}
matplotlib.rc("font", **font)

# set the image size
plt.figure(figsize=(9.9))

# Initial data value
X = np.array([8.19.2.72.6.39.8.71.4.7.2.66.3.78])
Y = np.array([7.01.2.78.6.47.6.71.4.1.4.23.4.05])
 
# Calculate the error between the line with parameter P and the original data
def f(p) :
    k, b = p
    return (Y - (k * X + b))
 
# leastsq minimizesthe sum of squares of f's output array. The initial values of k and b are set to [1,0].
r = leastsq(f, [1.0])

# Obtain the calculated optimal k and b
k, b = r[0]

# visualization
plt.scatter(X, Y, s=100, alpha=1.0, marker='o', label=U 'data point')
x = np.linspace(0.10.1000)
y = k * x + b
ax = plt.gca()
plt.plot(x, y, color='r', linewidth=5, linestyle=":", markersize=20, label=U 'Fitting curve')
plt.legend(loc=0, numpoints=1)
leg = plt.gca().get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize='xx-large')
plt.xlabel(U 'amp/A')
plt.ylabel(U 'V/V.)
plt.xlim(0, x.max(*)1.1)
plt.ylim(0, y.max(*)1.1)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.legend(loc='upper left')
plt.show()
Copy the code

Output:

conclusion

Source of learning: STATION B and its classroom PPT, the code is reproduced

The essay is just a study note, recording a process from 0 to 1

Hope to help you, if there is a mistake welcome small partners to correct ~

I am haihong ଘ(੭, ᵕ)੭

If you think it’s ok, please give it a thumbs up

Thanks for your support ❤️