2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -4,7 +4,7 @@
In [1]:
```
```py
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
@@ -17,7 +17,7 @@ import matplotlib.pyplot as plt
In [2]:
```
```py
from numpy import polyfit, poly1d
```
@@ -26,7 +26,7 @@ from numpy import polyfit, poly1d
In [3]:
```
```py
x = np.linspace(-5, 5, 100)
y = 4 * x + 1.5
noise_y = y + np.random.randn(y.shape[-1]) * 2.5
@@ -37,7 +37,7 @@ noise_y = y + np.random.randn(y.shape[-1]) * 2.5
In [4]:
```
```py
%matplotlib inline
p = plt.plot(x, noise_y, 'rx')
@@ -211,13 +211,13 @@ DpwbdZAmKImIJEytUzEiIlJhCuwiIgmjwC4ikjAK7CIiCaPALiKSMArsIiIJo8AuIpIwCuwiIgnz
In [5]:
```
```py
coeff = polyfit(x, noise_y, 1)
print coeff
```
```
```py
[ 3.93921315 1.59379469]
```
@@ -228,7 +228,7 @@ print coeff
In [6]:
```
```py
p = plt.plot(x, noise_y, 'rx')
p = plt.plot(x, coeff[0] * x + coeff[1], 'k-')
p = plt.plot(x, y, 'b--')
@@ -434,7 +434,7 @@ rkJggg==
In [7]:
```
```py
f = poly1d(coeff)
p = plt.plot(x, noise_y, 'rx')
p = plt.plot(x, f(x))
@@ -623,14 +623,14 @@ TbkUxv9z1wIPRzoeHwCXxNtIA5REREIm36kYERHJMgV2EZGQUWAXEQkZBXYRkZBRYBcRCRkFdhGR
kFFgFxEJGQV2EZGQ+X/DyfQNy7jRVwAAAABJRU5ErkJggg==
)In [8]:
```
```py
f
```
Out[8]:
```
```py
poly1d([ 3.93921315, 1.59379469])
```
@@ -638,12 +638,12 @@ poly1d([ 3.93921315, 1.59379469])
In [9]:
```
```py
print f
```
```
```py
3.939 x + 1.594
@@ -653,12 +653,12 @@ print f
In [10]:
```
```py
print f + 2 * f ** 2
```
```
```py
2
31.03 x + 29.05 x + 6.674
@@ -670,7 +670,7 @@ print f + 2 * f ** 2
In [11]:
```
```py
x = np.linspace(-np.pi,np.pi,100)
y = np.sin(x)
@@ -680,7 +680,7 @@ y = np.sin(x)
In [12]:
```
```py
y1 = poly1d(polyfit(x,y,1))
y3 = poly1d(polyfit(x,y,3))
y5 = poly1d(polyfit(x,y,5))
@@ -691,7 +691,7 @@ y9 = poly1d(polyfit(x,y,9))
In [13]:
```
```py
x = np.linspace(-3 * np.pi,3 * np.pi,100)
p = plt.plot(x, np.sin(x), 'k')
@@ -1137,7 +1137,7 @@ iBJ3hUKh0CFK3BUKhUKH/AdKQZTK2jwwogAAAABJRU5ErkJggg==
In [14]:
```
```py
from scipy.linalg import lstsq
from scipy.stats import linregress
@@ -1145,7 +1145,7 @@ from scipy.stats import linregress
In [15]:
```
```py
x = np.linspace(0,5,100)
y = 0.5 * x + np.random.randn(x.shape[-1]) * 0.35
@@ -1155,7 +1155,7 @@ plt.plot(x,y,'x')
Out[15]:
```
```py
[<matplotlib.lines.Line2D at 0xbc98518>]
```
@@ -1309,7 +1309,7 @@ $$\left[ \begin{matrix} x_0^{N-1} & \dots & x_0 & 1 \\\ x_1^{N-1} & \dots & x_1
In [16]:
```
```py
X = np.hstack((x[:,np.newaxis], np.ones((x.shape[-1],1))))
X[1:5]
@@ -1317,7 +1317,7 @@ X[1:5]
Out[16]:
```
```py
array([[ 0.05050505, 1\. ],
[ 0.1010101 , 1\. ],
[ 0.15151515, 1\. ],
@@ -1328,7 +1328,7 @@ array([[ 0.05050505, 1\. ],
In [17]:
```
```py
C, resid, rank, s = lstsq(X, y)
C, resid, rank, s
@@ -1336,7 +1336,7 @@ C, resid, rank, s
Out[17]:
```
```py
(array([ 0.50432002, 0.0415695 ]),
12.182942535066523,
2,
@@ -1347,7 +1347,7 @@ Out[17]:
In [18]:
```
```py
p = plt.plot(x, y, 'rx')
p = plt.plot(x, C[0] * x + C[1], 'k--')
print "sum squared residual = {:.3f}".format(resid)
@@ -1356,7 +1356,7 @@ print "singular values of X = {}".format(s)
```
```
```py
sum squared residual = 12.183
rank of the X matrix = 2
singular values of X = [ 30.23732043 4.82146667]
@@ -1545,7 +1545,7 @@ TkSuQmCC
In [19]:
```
```py
slope, intercept, r_value, p_value, stderr = linregress(x, y)
slope, intercept
@@ -1553,13 +1553,13 @@ slope, intercept
Out[19]:
```
```py
(0.50432001884393252, 0.041569499438028901)
```
In [20]:
```
```py
p = plt.plot(x, y, 'rx')
p = plt.plot(x, slope * x + intercept, 'k--')
print "R-value = {:.3f}".format(r_value)
@@ -1568,7 +1568,7 @@ print "Root mean squared error of the fit = {:.3f}".format(np.sqrt(stderr))
```
```
```py
R-value = 0.903
p-value (probability there is no correlation) = 8.225e-38
Root mean squared error of the fit = 0.156
@@ -1756,7 +1756,7 @@ gzwRkY8xyBMR+RiDPBGRjzHIExH5GIM8EZGPMcgTEfnY/we7I5NFwW+W8wAAAABJRU5ErkJggg==
In [21]:
```
```py
from scipy.optimize import leastsq
```
@@ -1765,7 +1765,7 @@ from scipy.optimize import leastsq
In [22]:
```
```py
def function(x, a , b, f, phi):
"""a function of x with four parameters"""
result = a * np.exp(-b * np.sin(f * x + phi))
@@ -1777,7 +1777,7 @@ def function(x, a , b, f, phi):
In [23]:
```
```py
x = np.linspace(0, 2 * np.pi, 50)
actual_parameters = [3, 2, 1.25, np.pi / 4]
y = function(x, *actual_parameters)
@@ -1924,7 +1924,7 @@ LSKSMApuEZGEUXCLiCSMgltEJGH+Px9Ir5f410BLAAAAAElFTkSuQmCC
In [24]:
```
```py
from scipy.stats import norm
y_noisy = y + 0.8 * norm.rvs(size=len(x))
p = plt.plot(x, y, 'k-')
@@ -2124,7 +2124,7 @@ AABJRU5ErkJggg==
In [25]:
```
```py
def f_err(p, y, x):
return y - function(x, *p)
@@ -2134,7 +2134,7 @@ def f_err(p, y, x):
In [26]:
```
```py
c, ret_val = leastsq(f_err, [1, 1, 1, 1], args=(y_noisy, x))
c, ret_val
@@ -2142,7 +2142,7 @@ c, ret_val
Out[26]:
```
```py
(array([ 3.03199715, 1.97689384, 1.30083191, 0.6393337 ]), 1)
```
@@ -2150,7 +2150,7 @@ Out[26]:
In [27]:
```
```py
p = plt.plot(x, y_noisy, 'rx')
p = plt.plot(x, function(x, *c), 'k--')
@@ -2317,7 +2317,7 @@ A5MAAAAASUVORK5CYII=
In [28]:
```
```py
from scipy.optimize import curve_fit
```
@@ -2326,21 +2326,21 @@ from scipy.optimize import curve_fit
In [29]:
```
```py
p_est, err_est = curve_fit(function, x, y_noisy)
```
In [30]:
```
```py
print p_est
p = plt.plot(x, y_noisy, "rx")
p = plt.plot(x, function(x, *p_est), "k--")
```
```
```py
[ 3.03199711 1.97689385 1.3008319 0.63933373]
```
@@ -2504,12 +2504,12 @@ A5MAAAAASUVORK5CYII=
In [31]:
```
```py
print err_est
```
```
```py
[[ 0.08483704 -0.02782318 0.00967093 -0.03029038]
[-0.02782318 0.00933216 -0.00305158 0.00955794]
[ 0.00967093 -0.00305158 0.0014972 -0.00468919]
@@ -2521,14 +2521,14 @@ print err_est
In [32]:
```
```py
print "normalized relative errors for each parameter"
print " a\t b\t f\tphi"
print np.sqrt(err_est.diagonal()) / p_est
```
```
```py
normalized relative errors for each parameter
a b f phi
[ 0.09606473 0.0488661 0.02974528 0.19056043]