This commit is contained in:
estomm
2020-09-25 22:01:28 +08:00
parent 5fcc3ef102
commit 73cc328c81
17 changed files with 412 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
## basic API
| poly1d\(c\_or\_r\[, r, variable\]\) | A one\-dimensional polynomial class\. |
|-------------------------------------|--------------------------------------------------------------------------|
| polyval\(p, x\) | Evaluate a polynomial at specific values\. |
| poly\(seq\_of\_zeros\) | Find the coefficients of a polynomial with the given sequence of roots\. |
| roots\(p\) | Return the roots of a polynomial with coefficients given in p\. |
## filter
| polyfit\(x, y, deg\[, rcond, full, w, cov\]\) | Least squares polynomial fit\. |
|-----------------------------------------------|--------------------------------|
## calculus
| polyder\(p\[, m\]\) | Return the derivative of the specified order of a polynomial\. |
|------------------------|--------------------------------------------------------------------|
| polyint\(p\[, m, k\]\) | Return an antiderivative \(indefinite integral\) of a polynomial\. |
## Arithmetic
| polyadd\(a1, a2\) | Find the sum of two polynomials\. |
|-------------------|-------------------------------------------------------------|
| polydiv\(u, v\) | Returns the quotient and remainder of polynomial division\. |
| polymul\(a1, a2\) | Find the product of two polynomials\. |
| polysub\(a1, a2\) | Difference \(subtraction\) of two polynomials\. |
## 创建多项式
* f = np.poly1d(a)
## 求微分和积分
* f.deriv()
* f.integ()
```py
from numpy import poly1d
p = poly1d([3,4,5])
print(p)
2
3 x + 4 x + 5
print(p*p)
4 3 2
9 x + 24 x + 46 x + 40 x + 25
print(p.integ(k=6))
3 2
1 x + 2 x + 5 x + 6
print(p.deriv())
6 x + 4
p([4, 5])
array([ 69, 100])
```

View File

@@ -1,4 +1,6 @@
# 量的定义
> 定义n维数组并且在数组上进行简单的变换与操作。
## 名称

6
Python/readme.md Normal file
View File

@@ -0,0 +1,6 @@
## numpy
## scipy
> 内容涉及大量的数学知识,不应该代码驱动学习,应该在学习或者使用相关的数学知识的时候,进行代码实现。

View File

@@ -0,0 +1,35 @@
## 功能概述
SciPy is a collection of mathematical algorithms and convenience functions built on the NumPy extension of Python.
Scipy是一个高级的科学计算库建立在低一级的numpy的多维数组之上。Scipy有很多子模块可以完成不同的操作如傅里叶变换、插值运算、优化算法和数学统计等。Scipy的常用的子模块如下
```py
scipy.cluster 向量量化
scipy.constants 数学常量
scipy.fftpack 快速傅里叶变换
scipy.integrate 积分
scipy.interpolate 插值
scipy.io 数据输入输出
scipy.linalg 线性代数
scipy.ndimage N维图像
scipy.odr 正交距离回归
scipy.optimize 优化算法
scipy.signal 信号处理
scipy.sparse 稀疏矩阵
scipy.spatial 空间数据结构和算法
scipy.special 特殊数学函数
scipy.stats 统计函数
>>> from scipy import linalg, optimize
```
> numpy提供了ndarray对象和关于该对象的基本操作和基本运算。scipy提供了ndarray的科学计算。
## 查看帮助
```py
help(optimize.fmin)
np.info(optimize.fmin)
```

View File

4
Python/scipy/11stats.md Normal file
View File

@@ -0,0 +1,4 @@
# 统计函数
> 实现概率论和数理统计相关的功能

92
Python/scipy/12ndimage.md Normal file
View File

@@ -0,0 +1,92 @@
## ndimage用途
SciPy的ndimage子模块专用于图像处理。这里ndimage表示一个n维图像。
图像处理中一些最常见的任务如下:
* 输入/输出/显示图像
* 基本操作:裁剪,翻转,旋转等图像过滤
* 去噪,锐化等图像分割
* 标记对应于不同对象的像素
* 分类
* 特征提取
* 注册/配准
## 示例
```
# 导入图像
from scipy import misc
f = misc.face()
misc.imsave('face.png', f) # uses the Image module (PIL)
import matplotlib.pyplot as plt
plt.imshow(f)
plt.show()
# 基本信息
from scipy import misc
f = misc.face()
misc.imsave('face.png', f) # uses the Image module (PIL)
face = misc.face(gray = False)
print (face.mean(), face.max(), face.min())
## 几何裁剪
from scipy import misc
f = misc.face()
misc.imsave('face.png', f) # uses the Image module (PIL)
face = misc.face(gray = True)
lx, ly = face.shape
crop_face = face[int(lx/4): -int(lx/4), int(ly/4): -int(ly/4)]
import matplotlib.pyplot as plt
plt.imshow(crop_face)
plt.show()
# 倒置图像
from scipy import misc
face = misc.face()
flip_ud_face = np.flipud(face)
import matplotlib.pyplot as plt
plt.imshow(flip_ud_face)
plt.show()
# 旋转图像
# rotation
from scipy import misc,ndimage
face = misc.face()
rotate_face = ndimage.rotate(face, 45)
import matplotlib.pyplot as plt
plt.imshow(rotate_face)
plt.show()
# 模糊滤镜
from scipy import misc
face = misc.face()
blurred_face = ndimage.gaussian_filter(face, sigma=3)
import matplotlib.pyplot as plt
plt.imshow(blurred_face)
plt.show()
# 边缘检测
import scipy.ndimage as nd
import numpy as np
im = np.zeros((256, 256))
im[64:-64, 64:-64] = 1
im[90:-90,90:-90] = 2
im = ndimage.gaussian_filter(im, 8)
import matplotlib.pyplot as plt
plt.imshow(im)
plt.show()
```

22
Python/scipy/13io.md Normal file
View File

@@ -0,0 +1,22 @@
## IO
* loadmat
加载一个MATLAB文件
* savemat
保存为一个MATLAB文件
* whosmat
列出MATLAB文件中的变量
```py
import scipy.io as sio
import numpy as np
#Save a mat file
vect = np.arange(10)
sio.savemat('array.mat', {'vect':vect})
#Now Load the File
mat_file_content = sio.loadmat('array.mat')
print (mat_file_content)
```

View File

@@ -0,0 +1,10 @@
## index trics
## shape manipulation
## polynomials
## vectorizing functions
## type handling

6
Python/scipy/2special.md Normal file
View File

@@ -0,0 +1,6 @@
## Bessel functions of real order
> bassel函数
## Cython Bindings for Special Functions

101
Python/scipy/3integrate.md Normal file
View File

@@ -0,0 +1,101 @@
## Breif
The scipy.integrate sub-package provides several integration techniques including an ordinary differential equation integrator.
```py
>>> help(integrate)
Methods for Integrating Functions given function object.
quad -- General purpose integration.
dblquad -- General purpose double integration.
tplquad -- General purpose triple integration.
fixed_quad -- Integrate func(x) using Gaussian quadrature of order n.
quadrature -- Integrate with given tolerance using Gaussian quadrature.
romberg -- Integrate func using Romberg integration.
Methods for Integrating Functions given fixed samples.
trapz -- Use trapezoidal rule to compute integral from samples.
cumtrapz -- Use trapezoidal rule to cumulatively compute integral.
simps -- Use Simpson's rule to compute integral from samples.
romb -- Use Romberg Integration to compute integral from
(2**k + 1) evenly-spaced samples.
See the special module's orthogonal polynomials (special) for Gaussian
quadrature roots and weights for other weighting factors and regions.
Interface to numerical integrators of ODE systems.
odeint -- General integration of ordinary differential equations.
ode -- Integrate ODE using VODE and ZVODE routines.
```
## General integration (quad)
$$
I = \int_0^{4.5}f(x)dx
$$
```py
def f(x,y):
pass
# 可以用lambda函数补充其他参数。
result = integrate.quad(lambda x:f(x,19), 0, 4.5)
```
## 带参数积分
$$
I(a,b)=\int_0^1ax^2+bxdx
$$
```
from scipy.integrate import quad
def integrand(x, a, b):
return a*x**2 + bx
a = 2
b = 1
I = quad(integrand, 0, 1, args=(a,b))
I
(1.6666666666666667, 1.8503717077085944e-14)
```
## 使用单积分实现多重积分
$$
I_n=\int_0^{\infin}\int_1^{\infin}\frac{e^{-xt}}{t^n}dtdx=\frac{1}{n}
$$
```py
# 内层积分
from scipy.integrate import quad
def integrand(t, n, x):
return np.exp(-x*t) / t**n
>>>
def expint(n, x):
return quad(integrand, 1, np.inf, args=(n, x))[0]
# 外层积分
result = quad(lambda x: expint(3, x), 0, np.inf)
print(result)
(0.33333333324560266, 2.8548934485373678e-09)
```
## General multiple integration (dblquad, tplquad, nquad)¶
$$
I_n=\int_0^{\infin}\int_1^{\infin}\frac{e^{-xt}}{t^n}dtdx=\frac{1}{n}
$$
```
from scipy.integrate import quad, dblquad
def I(n):
return dblquad(lambda t, x: np.exp(-x*t)/t**n, 0, np.inf, lambda x: 1, lambda x: np.inf)
```
## Gaussian quadrature
## Romberg Integration
## Integrating using Samples
## Faster integration using low-level callback functions
## Ordinary differential equations (solve_ivp)

12
Python/scipy/4optimize.md Normal file
View File

@@ -0,0 +1,12 @@
## 优化算法
scipy.optimize包提供了几种常用的优化算法。
* 使用各种算法(例如BFGSNelder-Mead单纯形牛顿共轭梯度COBYLA或SLSQP)的无约束和约束最小化多元标量函数(minimize())
* 全局(蛮力)优化程序(例如anneal()basinhopping())
* 最小二乘最小化(leastsq())和曲线拟合(curve_fit())算法
* 标量单变量函数最小化(minim_scalar())和根查找(newton())
* 使用多种算法(例如PowellLevenberg-Marquardt混合或Newton-Krylov等大规模方法)的多元方程系统求解(root)

View File

@@ -0,0 +1,28 @@
## 定义
插值是在直线或曲线上的两点之间找到值的过程。 为了帮助记住它的含义我们应该将“inter”这个词的第一部分想象为“输入”表示要查看原来数据的“内部”。 这种插值工具不仅适用于统计学,而且在科学,商业或需要预测两个现有数据点内的值时也很有用。
```py
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 12)
y = np.cos(x**2/3+4)
plt.plot(x, y,o)
plt.show()
```
## 一维插值
一维插值scipy.interpolate中的interp1d类是一种创建基于固定数据点的函数的便捷方法可以使用线性插值在给定数据定义的域内的任意位置评估该函数。
通过使用上述数据,创建一个插值函数并绘制一个新的插值图。
```
f1 = interp1d(x, y,kind = 'linear')
f2 = interp1d(x, y, kind = 'cubic'
```

27
Python/scipy/6fft.md Normal file
View File

@@ -0,0 +1,27 @@
## 傅里叶变换
对时域信号计算傅里叶变换以检查其在频域中的行为。 傅里叶变换可用于信号和噪声处理图像处理音频信号处理等领域。SciPy提供fftpack模块可让用户计算快速傅立叶变换。
以下是一个正弦函数的例子它将用于使用fftpack模块计算傅里叶变换。
## 一维傅里叶变换
```py
#Importing the fft and inverse fft functions from fftpackage
from scipy.fftpack import fft
#create an array with random n numbers
x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
#Applying the fft function
y = fft(x)
print (y)
```
## 离散余弦变换
```py
from scipy.fftpack import dct
mydict = dct(np.array([4., 3., 5., 10., 5., 3.]))
print(mydict)
```

0
Python/scipy/7signal.md Normal file
View File

13
Python/scipy/8linalg.md Normal file
View File

@@ -0,0 +1,13 @@
## 线性代数
> 主要修改二维数组
SciPy是使用优化的ATLAS LAPACK和BLAS库构建的。 它具有非常快的线性代数能力。 所有这些线性代数例程都需要一个可以转换为二维数组的对象。 这些例程的输出也是一个二维数组。
## 线性方程组
## 行列式
## 特征值特征向量
## 奇异值分解

View File