Python SciPy library is a set of convenience functions built on NumPy and mathematical algorithms.

Python SciPy库是一组基于NumPy和数学算法的便捷功能。

Python SciPy (Python SciPy)

  • At present Python SciPy library supports integration, gradient optimization, special functions, ordinary differential equation solvers, parallel programming tools and many more; in other words, we can say that if something is there in general textbook of numerical computation, there are high chances you’ll find it’s implementation in SciPy.目前,Python SciPy库支持集成,梯度优化,特殊功能,常微分方程求解器,并行编程工具等。 换句话说,我们可以说,如果通用的数值计算教科书中有某些内容,您很有可能会在SciPy中找到它的实现。
  • An interactive session with SciPy is basically a data-processing and system-prototyping environment just like MATLAB, Octave, Scilab or R-lab.与SciPy进行的交互式会话基本上是一个数据处理和系统原型制作环境,就像MATLAB,Octave,Scilab或R-lab。
  • SciPy is an open source project, released under BSD license.SciPy是一个开源项目,根据BSD许可发布。

为什么选择SciPy? (Why SciPy?)

SciPy provides high-level commands and classes for data-manipulation and data-visualization, which increases the power of an interactive Python session by significant order.

SciPy提供了用于数据处理和数据可视化的高级命令和类,从而以显着的顺序增加了交互式Python会话的功能。

In addition to mathematical algorithms in SciPy, everything from classes and web and database subroutines to parallel programming is available to Python programmer, making it easier and faster to develop sophisticated and specialized applications.

除了SciPy中的数学算法之外,Python程序员还可以使用从类,Web和数据库子例程到并行编程的所有内容,从而可以更轻松,更快速地开发复杂而专业的应用程序。

Since SciPy is open source, developers across the world can contribute to the development of additional modules which is much beneficial for scientific applications using SciPy.

由于SciPy是开源的,因此世界各地的开发人员都可以为附加模块的开发做出贡献,这对于使用SciPy的科学应用非常有益。

安装SciPy库 (Install SciPy Library)

We’ll discuss some basic functions and important features of SciPy but before let’s install SciPy. We can install SciPy packages simply by using pip, run the following command in terminal (add sudo if you have to):

我们将讨论SciPy的一些基本功能和重要功能,但在安装SciPy之前。 我们可以简单地使用pip来安装SciPy软件包,在终端中运行以下命令(如果需要,可以添加sudo ):

pip install scipy

To install this package with conda run:

要使用conda安装此软件包,请运行:

conda install -c anaconda scipy

导入Scipy库 (Importing Scipy Library)

To start using Scipy in our python projects, we will just import Scipy as:

要在我们的python项目中开始使用Scipy,我们只需将 Scipy 导入为:

import scipy

与Numpy互动 (Interacting with Numpy)

As we already know SciPy is built on NumPy, so for all basic needs we can use NumPy functions itself:

众所周知,SciPy是基于NumPy构建的,因此对于所有基本需求,我们都可以使用NumPy函数本身:

import numpy

Functions from numpy and numpy.lib.scimath are also contained in SciPy, but it’s recommended to use them directly and not go through SciPy in this case.

numpynumpy.lib.scimath中的函数也包含在SciPy中,但是建议直接使用它们,在这种情况下,不要通过SciPy。

使用多项式 (Working with Polynomials)

In SciPy, there are two ways in which we can deal with 1D polynomials. First one is using the class poly1d. This class takes coefficients or roots for initialization and forms a polynomial object. When we print this object we’ll see it prints like a polynomial. Let’s have a look at example code:

在SciPy中,有两种方法可以处理一维多项式。 第一个是使用类poly1d 。 该类采用系数或根进行初始化,并形成多项式对象。 当我们打印该对象时,我们将看到它像多项式一样打印。 让我们看一下示例代码:

from numpy import poly1d# We'll use some functions from numpy remember!!
# Creating a simple polynomial object using coefficients
somePolynomial = poly1d([1,2,3])# Printing the result
# Notice how easy it is to read the polynomial this way
print(somePolynomial)# Let's perform some manipulations
print("\nSquaring the polynomial: \n")
print(somePolynomial* somePolynomial)#How about integration, we just have to call a function
# We just have to pass a constant say 3
print("\nIntegrating the polynomial: \n")
print(somePolynomial.integ(k=3))#We can also find derivatives in similar way
print("\nFinding derivative of the polynomial: \n")
print(somePolynomial.deriv())# We can also solve the polynomial for some value,
# let's try to solve it for 2
print("\nSolving the polynomial for 2: \n")
print(somePolynomial(2))

The program when run gives output like below image, I hope comments made it clear what each piece of code is trying to do:

该程序在运行时会给出如下图所示的输出,我希望注释能使每段代码试图做什么变得清楚:

Another way to handle polynomials is to use an array of coefficients. There are functions available to perform operations on polynomials represented as sequences, first method looks much easier to use and give output in a readable manner, so I prefer first one for the instance.

处理多项式的另一种方法是使用系数数组。 有一些函数可以对表示为序列的多项式执行运算,第一种方法看起来更易于使用,并且以可读的方式给出输出,因此对于该实例,我更喜欢第一个。

SciPy示例–线性代数 (SciPy Example – Linear Algebra)

SciPy holds very fast linear algebra capabilities as it’s built using ATLAS LAPACK and BLAS libraries. The libraries are even available to use if you want more speed but you have to dig deep in that case.

SciPy使用ATLAS LAPACK和BLAS库构建,具有非常快的线性代数功能。 如果您想提高速度,甚至可以使用这些库,但是在这种情况下,您必须深入研究。

All the linear algebra routines in SciPy take an object that can be converted into a 2D array and the output is of the same type.

SciPy中的所有线性代数例程都采用一个可以转换为2D数组的对象,并且输出的类型相同。

Let’s have a look at linear algebra routine with help of an example. We’ll try to solve a linear algebra system which can easily be done using scipy command linalg.solve.

让我们借助示例来了解线性代数例程。 我们将尝试解决线性代数系统,可以使用scipy命令linalg.solve轻松完成。

This method expects input matrix and right-hand side vector:

该方法期望输入矩阵和右侧向量:

# Import required modules/ libraries
import numpy as np
from scipy import linalg# We are trying to solve a linear algebra system which can be given as:
#               1x + 2y =5
#               3x + 4y =6# Create input array
A= np.array([[1,2],[3,4]])# Solution Array
B= np.array([[5],[6]])# Solve the linear algebra
X= linalg.solve(A,B)# Print results
print(X)# Checking Results
print("\n Checking results, following vector should be all zeros")
print(A.dot(X)-B)

Let’s run this script now:

现在运行此脚本:

科学整合 (SciPy Integration)

The integrate sub-package of scipy provides various integration techniques. We can have a look at these simply by typing:

scipy的集成子包提供了各种集成技术。 我们只需输入以下内容即可查看这些内容:

help(integrate)

Let’s try to integrate a lambda in a script:

让我们尝试将lambda集成到脚本中:

# Import required packages
from scipy import integrate# Using quad as we can see in list quad is used for simple integration
# arg1: A lambda function which returns x squared for every x
# We'll be integrating this function
# arg2: lower limit
# arg3: upper limit
result= integrate.quad(lambda x: x**2, 0,3)
print(result)

Again, we have added enough comments so that the code is utmost clear. Let’s run this now:

同样,我们添加了足够的注释,以使代码极为清晰。 让我们现在运行它:

The second item in result here is possible error in result, we can ignore it for now.

结果中的第二项可能是结果中的错误,我们现在可以忽略它。

科学傅立叶变换 (SciPy Fourier Transforms)

Fourier analysis helps us to express a function as a sum of periodic components and for recovering the signal from those components.

傅里叶分析可帮助我们将函数表达为周期成分之和,并从这些成分中恢复信号。

Let’s look at a simple example of Fourier transformation. We’ll be plotting sum of two sines:

让我们看一个简单的傅立叶变换示例。 我们将绘制两个罪孽之和:

# Import Fast Fourier Transformation requirements
from scipy.fftpack import fft
import numpy as np# Number of sample points
N = 600# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)# matplotlib for plotting purposes
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.grid()
plt.show()

The output should be similar to this:

输出应类似于以下内容:

Not to the surprise, Matplotlib drew the graph.

毫不奇怪, Matplotlib绘制了图表。

科学特殊功能 (SciPy Special Functions)

The special sub-package of SciPy has definitions of numerous functions of mathematical physics. The functions available include airy, Bessel, beta, elliptic, gamma, hypergeometric, Kelvin, Mathieu, parabolic cylinder, spheroidal wave and struve. Let’s have a look at Bessel function.

SciPy的特殊子软件包定义了许多数学物理学的功能。 可用功能包括通风,贝塞尔,贝塔,椭圆,伽马,超几何,开尔文,马修,抛物柱面,球面波和曲面。 让我们看一下贝塞尔函数。

Bessel functions are a family of solutions to Bessel’s differential equation with real or complex order alpha.

贝塞尔函数是具有实阶或复阶阿尔法的贝塞尔微分方程的一系列解决方案。

Let’s have a better look at it with help of an example, the example is of a circular drum anchored at edge:

让我们借助一个示例更好地了解它,该示例是一个固定在边缘的圆形鼓:

# Import special package
from scipy import special
import numpy as np
def drumhead_height(n, k, distance, angle, t):kth_zero = special.jn_zeros(n, k)[-1]return np.cos(t) * np.cos(n*angle) * special.jn(n, distance*kth_zero)
theta = np.r_[0:2*np.pi:50j]
radius = np.r_[0:1:50j]
x = np.array([r * np.cos(theta) for r in radius])
y = np.array([r * np.sin(theta) for r in radius])
z = np.array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])# Plot the results for visualization
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

The code on running plots following graph:

运行代码如下图所示:

Some functions like binary entropy function, Heaviside step function and ramp function are straightforward to implement with help of existing functions NumPy and SciPy and thus are not included in this package.

在现有功能NumPy和SciPy的帮助下,一些函数(如二进制熵函数,Heaviside阶跃函数和斜坡函数)可以直接实现,因此不包含在此程序包中。

摘要 (Summary)

In this post, we discovered the SciPy Python library for easy and efficient mathematical operations.

在本文中,我们发现了SciPy Python库,可轻松高效地进行数学运算。

We learned that it is a vast library used for scientific application development and where sophisticated and complex mathematical operations are to be taken care of, like in Machine Learning / Deep Learning. We also had a look at how scipy package helps us in performing various mathematical operations.

我们了解到,这是一个庞大的库,用于科学应用程序开发,并且需要像机器学习/深度学习那样处理复杂的数学运算。 我们还研究了scipy软件包如何帮助我们执行各种数学运算。

Reference: Official Website

参考: 官方网站

翻译自: https://www.journaldev.com/18106/python-scipy-tutorial

Python SciPy教程相关推荐

  1. windows上安装Anaconda和python的教程详解

    一提到数字图像处理编程,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因 ...

  2. python入门教程完整版(懂中文就能学会)-Python入门教程完整版(懂中文就能学会)...

    不过小编的内心是强大的,网友虐我千百遍,我待网友如初恋,因为今天又给大家带来了干货,Python入门教程完整版,完整版啊!完整版! 言归正传,小编该给大家介绍一下这套教程了,希望每个小伙伴都沉迷学习, ...

  3. python安装教程windows-windows上安装Anaconda和python的教程详解

    一提到数字图像处理编程,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因 ...

  4. python基础教程书-7本Python必读的入门书籍

    Python入门书籍不用看太多,看一本就够.重要的是你要学习Python的哪个方向,或者说你对什么方向感兴趣,因为Python这门语言的应用领域比较广泛,比如说可以用来做数据分析.机器学习,也可以用来 ...

  5. python是干什么用的视频-python基础教程千锋最新视频学完之后可以做什么

    从目前来看,人工智能异常火爆,而Python作为人工智能首选语言自然受到了欢迎,而且Python语言在学术上也很受青睐.就算不是计算机专业的人,很多都在学习python.那么python基础教程千锋最 ...

  6. python基础教程书籍推荐-python入门书籍推荐

    python入门书籍推荐 1.Python编程:入门到实践 理论和实践恰到好处,行文逻辑流畅,不跳跃,手把手教的感觉,却不啰嗦,非常适合入门.强烈推荐这本书,书中涵盖的内容是比较精简的,没有艰深晦涩的 ...

  7. anaconda的python使用教程-Python安装教程之Anaconda入门使用总结

    原标题:Python安装教程之Anaconda入门使用总结 如今参加Python培训学习Python开发的小伙伴对Python安装教程比较感兴趣,本篇文章小编就和读者们分享一下Python安装教程之A ...

  8. python矩阵教程_numpy教程:矩阵matrix及其运算

    numpy矩阵简介 NumPy函数库中存在两种不同的数据类型(矩阵matrix和数组array),都可以用于处理行列表示的数字元素.虽然它们看起来很相似,但是在这两个数据类型上执行相同的数学运算可能得 ...

  9. python基础教程书籍-7本Python必读的入门书籍

    Python入门书籍不用看太多,看一本就够.重要的是你要学习Python的哪个方向,或者说你对什么方向感兴趣,因为Python这门语言的应用领域比较广泛,比如说可以用来做数据分析.机器学习,也可以用来 ...

最新文章

  1. [十一]基础数据类型之Character
  2. 基于高德地图的描点操作,监听地图缩放,展示合理数量的marker
  3. ASP.Net请求小周期
  4. Linux 信号signal处理函数--转
  5. 分布式 RPC架构简单理解
  6. 汽车保险解读:解析涉水损失险与自燃险
  7. java ee各类组件_在Java EE组件中使用骆驼路线
  8. 网页制作之CSS超级技巧
  9. mixpanel实验教程(2)
  10. 虚拟现实果真来了吗?
  11. linux I2C读写应用程序
  12. html做新浪体育,新浪体育台看不了怎么办?新浪体育台网页版加载失败的解决方法介绍...
  13. CSS加粗知识与案例
  14. Please make sure that the app id is set correctly.
  15. Pandas中的数据聚合方法
  16. springcloud如何制作一个物联网产品
  17. 检测报告上CNAS、CMA资质含义及联系
  18. bat 当前目录下 子目录文件 移动到当前目录 并 文件改名 改后缀名
  19. ad18放置标尺(测量)
  20. Mybatis-plus-join连表查询

热门文章

  1. IOC容器特性注入第五篇:查找(Attribute)特性注入
  2. 【整理】C#2.0特性之局部类、空属类型和静态类
  3. 成功解决TypeError: a bytes-like object is required, not ‘str‘
  4. [转载] python 字符串包含某个字符_python字符串
  5. [转载] python可视化分析(matplotlib、seaborn、ggplot2)
  6. [转载] python| map()函数应用详解
  7. 在LINUX上部署SOFA
  8. 通过IO流下载Excel文件
  9. node.js中ws模块创建服务端和客户端,网页WebSocket客户端
  10. 实例练习----电影天堂抓取下载链接