From 73cc328c815f30c8fbe1d0c1354b827b797568f3 Mon Sep 17 00:00:00 2001 From: estomm Date: Fri, 25 Sep 2020 22:01:28 +0800 Subject: [PATCH] scipy --- Python/numpy/19多项式.md | 54 +++++++++++++++++ Python/numpy/1概述.md | 2 + Python/readme.md | 6 ++ Python/scipy/0introduction.md | 35 +++++++++++ Python/scipy/10spatial.md | 0 Python/scipy/11stats.md | 4 ++ Python/scipy/12ndimage.md | 92 +++++++++++++++++++++++++++++ Python/scipy/13io.md | 22 +++++++ Python/scipy/1basicfunction.md | 10 ++++ Python/scipy/2special.md | 6 ++ Python/scipy/3integrate.md | 101 ++++++++++++++++++++++++++++++++ Python/scipy/4optimize.md | 12 ++++ Python/scipy/5interpolate.md | 28 +++++++++ Python/scipy/6fft.md | 27 +++++++++ Python/scipy/7signal.md | 0 Python/scipy/8linalg.md | 13 ++++ Python/scipy/9sparse.csgraph.md | 0 17 files changed, 412 insertions(+) create mode 100644 Python/numpy/19多项式.md create mode 100644 Python/readme.md create mode 100644 Python/scipy/0introduction.md create mode 100644 Python/scipy/10spatial.md create mode 100644 Python/scipy/11stats.md create mode 100644 Python/scipy/12ndimage.md create mode 100644 Python/scipy/13io.md create mode 100644 Python/scipy/1basicfunction.md create mode 100644 Python/scipy/2special.md create mode 100644 Python/scipy/3integrate.md create mode 100644 Python/scipy/4optimize.md create mode 100644 Python/scipy/5interpolate.md create mode 100644 Python/scipy/6fft.md create mode 100644 Python/scipy/7signal.md create mode 100644 Python/scipy/8linalg.md create mode 100644 Python/scipy/9sparse.csgraph.md diff --git a/Python/numpy/19多项式.md b/Python/numpy/19多项式.md new file mode 100644 index 00000000..6bbdabc9 --- /dev/null +++ b/Python/numpy/19多项式.md @@ -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]) +``` \ No newline at end of file diff --git a/Python/numpy/1概述.md b/Python/numpy/1概述.md index f5c7d6fd..6003de1f 100644 --- a/Python/numpy/1概述.md +++ b/Python/numpy/1概述.md @@ -1,4 +1,6 @@ # 量的定义 +> 定义n维数组,并且在数组上进行简单的变换与操作。 + ## 名称 diff --git a/Python/readme.md b/Python/readme.md new file mode 100644 index 00000000..fa4d3c6a --- /dev/null +++ b/Python/readme.md @@ -0,0 +1,6 @@ +## numpy + +## scipy + +> 内容涉及大量的数学知识,不应该代码驱动学习,应该在学习或者使用相关的数学知识的时候,进行代码实现。 + diff --git a/Python/scipy/0introduction.md b/Python/scipy/0introduction.md new file mode 100644 index 00000000..13ab3d27 --- /dev/null +++ b/Python/scipy/0introduction.md @@ -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) +``` \ No newline at end of file diff --git a/Python/scipy/10spatial.md b/Python/scipy/10spatial.md new file mode 100644 index 00000000..e69de29b diff --git a/Python/scipy/11stats.md b/Python/scipy/11stats.md new file mode 100644 index 00000000..8b011986 --- /dev/null +++ b/Python/scipy/11stats.md @@ -0,0 +1,4 @@ +# 统计函数 + +> 实现概率论和数理统计相关的功能 + diff --git a/Python/scipy/12ndimage.md b/Python/scipy/12ndimage.md new file mode 100644 index 00000000..2d4816b8 --- /dev/null +++ b/Python/scipy/12ndimage.md @@ -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() + +``` + diff --git a/Python/scipy/13io.md b/Python/scipy/13io.md new file mode 100644 index 00000000..89401afe --- /dev/null +++ b/Python/scipy/13io.md @@ -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) +``` \ No newline at end of file diff --git a/Python/scipy/1basicfunction.md b/Python/scipy/1basicfunction.md new file mode 100644 index 00000000..b95ffe4f --- /dev/null +++ b/Python/scipy/1basicfunction.md @@ -0,0 +1,10 @@ +## index trics + +## shape manipulation + +## polynomials + +## vectorizing functions + +## type handling + diff --git a/Python/scipy/2special.md b/Python/scipy/2special.md new file mode 100644 index 00000000..25550bb9 --- /dev/null +++ b/Python/scipy/2special.md @@ -0,0 +1,6 @@ + + +## Bessel functions of real order +> bassel函数 + +## Cython Bindings for Special Functions \ No newline at end of file diff --git a/Python/scipy/3integrate.md b/Python/scipy/3integrate.md new file mode 100644 index 00000000..c5b9fc71 --- /dev/null +++ b/Python/scipy/3integrate.md @@ -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) + diff --git a/Python/scipy/4optimize.md b/Python/scipy/4optimize.md new file mode 100644 index 00000000..f9818805 --- /dev/null +++ b/Python/scipy/4optimize.md @@ -0,0 +1,12 @@ +## 优化算法 + +scipy.optimize包提供了几种常用的优化算法。 + +* 使用各种算法(例如BFGS,Nelder-Mead单纯形,牛顿共轭梯度,COBYLA或SLSQP)的无约束和约束最小化多元标量函数(minimize()) +* 全局(蛮力)优化程序(例如,anneal(),basinhopping()) +* 最小二乘最小化(leastsq())和曲线拟合(curve_fit())算法 +* 标量单变量函数最小化(minim_scalar())和根查找(newton()) +* 使用多种算法(例如,Powell,Levenberg-Marquardt混合或Newton-Krylov等大规模方法)的多元方程系统求解(root) + + + diff --git a/Python/scipy/5interpolate.md b/Python/scipy/5interpolate.md new file mode 100644 index 00000000..dd63bf35 --- /dev/null +++ b/Python/scipy/5interpolate.md @@ -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' + + +``` \ No newline at end of file diff --git a/Python/scipy/6fft.md b/Python/scipy/6fft.md new file mode 100644 index 00000000..7bc533bf --- /dev/null +++ b/Python/scipy/6fft.md @@ -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) +``` \ No newline at end of file diff --git a/Python/scipy/7signal.md b/Python/scipy/7signal.md new file mode 100644 index 00000000..e69de29b diff --git a/Python/scipy/8linalg.md b/Python/scipy/8linalg.md new file mode 100644 index 00000000..cc218617 --- /dev/null +++ b/Python/scipy/8linalg.md @@ -0,0 +1,13 @@ +## 线性代数 +> 主要修改二维数组 +SciPy是使用优化的ATLAS LAPACK和BLAS库构建的。 它具有非常快的线性代数能力。 所有这些线性代数例程都需要一个可以转换为二维数组的对象。 这些例程的输出也是一个二维数组。 + + +## 线性方程组 + +## 行列式 + +## 特征值特征向量 + +## 奇异值分解 + diff --git a/Python/scipy/9sparse.csgraph.md b/Python/scipy/9sparse.csgraph.md new file mode 100644 index 00000000..e69de29b