Python手册(Scientific Computing)--numpy


NumPy(Numerical Python的简称)是Python数值计算最重要的基础包。大多数提供科学计算的包都是用NumPy的数组作为构建基础。

NumPy的部分功能如下:

  • ndarray,一个具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组。
  • 用于对数组执行元素级计算以及直接对数组执行数学运算的函数。
  • 用于读写硬盘上基于数组的数据集的工具。
  • 线性代数运算、傅里叶变换,以及随机数生成。
  • 用于集成由C、C++、Fortran等语言编写的代码的API。
import numpy as np

NumPy的ndarray

  • ndarray 可以矢量化(vectorization)运算。
  • 不同大小的数组之间的运算叫做广播(broadcasting)。

创建ndarray

arr=np.array(obj,dtype=None)

用法说明
np.asarray(a, dtype=None将a转化为数组
np.arange(n)1:(n-1)
np.arange(start,stop,step)等差数列
np.linspace(start,end,num=50)返回等间隔的数字
np.empty(shape, dtype=float)
np.empty_like
没有任何具体值的数组,shape一个表示各维度大小的tuple
np.ones(shape, dtype=None)
np.zeros(shape, dtype=None)
np.ones_like; np.zeros_like
生成全是1或0数组,shape类型为tuple
np.eye(n)n*n单位矩阵,对角线为1,其余为0
np.full(shape, fill_value, dtype=None)生成全是fill_value数组
np.full_like(a,fill_value, dtype=None)根据a的形状生成全是ill_value的数组

ndarrary索引和切片

用法说明
arr[axis0,axis1]
arr[axis0][axis1]
arr[arr < 0]bool索引
arr[indexes_list]花式索引(总是将数据复制到新数组中) arr[[4, 3, 0, 6]]
arr[start:stop:step,start:stop:step]slice

Python关键字and和or在布尔型数组中无效。要使用&|

ndarrary属性

用法说明
arr.ndim维度数量
arr.shape维度信息tupple
arr.size元素总个数
arr.dtypenumpy的数据类型(详情请查阅)
arr.itermsize每个元素的大小,以字节为单位
arr.T转置属性

ndarrary方法

变换数组方法说明
arr.transpose()转置方法
arr.reshape(shape)/arr.resize(shape)修改数组维度
arr.swapaxes(ax1,ax2)多维数组维度调换
arr.flatten()降成一维数组
arr.astype(new_type)修改数组类型
统计方法
arr.sum(axis=None); arr.mean(axis=None)
arr.std(axis=None); arr.var(axis=None)
arr.min(axis=None); arr.max(axis=None)
arr.argmin(axis=None); arr.argmax(axis=None)返回最大值和最小值的索引
arr.cunsum(); arr.cumprod()累积和/累积乘积
arr.sort(axis=None)数组本身排序
线性代数
arr.dot(arr2)矩阵点积

numpy函数

合并和分裂
where(cond, xarr, yarr)xarr if condition else yarr
concatenate((a1,a2,…),axis=0)将数组a1,a2,…合并
hstack(tup)行合并
vstack(tup)列合并
split(ary, indices_or_sections, axis=0)indices_or_sections : int or 1-D array
hsplit(arr, indices_or_sections)行分裂hsplit(x, np.array([3, 6]))
vsplit(arr, indices_or_sections)列分裂
统计
max(a,axis=None); np.min(a,axis=None)
fmax(a,b)数组元素间取较大值
meshgrid(x,y)接受两个一维数组,并产生两个二维矩阵,对应于两个数组中所有的(x,y)对
线性代数
dot(arr1, arr2)矩阵点积
gradient(f)计算斜率(a[0]+a[1])/2
排序
sort(arr,axis=-1)返回的是数组的已排序副本
去重
unique(arr,axis=None)返回数组的唯一值,并排序
重复和堆叠
repeat(a, repeats, axis=None)repeats : int or array of ints
tile(A, reps)堆叠
数学运算
sum,prod求和,求积
cumsum,cumprod累积求和,累积求积
abs绝对值
sqrt计算机各元素的平方根
square计算各元素的平方
exp计算各元素的e的x次方
log,log10,log2,log1p分别自然对数(底数为e)、底数为10的log、底数为2的log、log(1+x)
sign计算各元素的正负号:1(正数)、0(零)、-1(负数)
ceil计算各元素的ceiling值,即大于等于该值的最小正数
floor计算各元素的floor值,即小于等于该值的最大正数
rint将各元素值四舍五入到最接近的整数,保留dtype
modf将数组的小数和整数部分以两个独立数组的形式返回
isnan返回bool数组
isfinite,isinf返回bool数组
cos,cosh,sin,sinh,tan,tanh普通型和双曲型三角函数
arccos,arccosh,arcsin,arcsinh,arctan,arctanh反三角函数
logical_not计算各元素notx的真值。相当于-arr
add将数组中对应的元素想加
subtract相减
multiply数组元素对应相乘
divide(arr1,arr2),floor_divide(arr1,arr2)除法或向下圆整除法(丢弃余数)
power乘方
mod取余数
greater(arr1,arr2),greater_equal(arr1,arr2),
less(arr1,arr2),less_equal(arr1,arr2),
equal(arr1,arr2),not_equal(arr1,arr2)
执行元素级的比较运算,最终产生布尔型数组,相当于运算符>,>=,<,<=,==,!=
logical_and,logical_or,logical_xor逻辑运算&、|、^
ufunc方法**np.ufunc
reduce(a, axis=0, dtype=None, out=None, keepdims=False)对值进行连续聚合
np.add.reduce(arr)
accumulate(array, axis=0, dtype=None, out=None, keepdims=None)保留所有局部聚合结果
reduceat(a, indices, axis=0, dtype=None, out=None)分组约简
outer(A, B, **kwargs)Apply the ufunc op to all pairs (a, b) with a in A and b in B.
ufunc.outer(A,B).ndim=A.ndim+B.ndim
np.frompyfunc(func, nin, nout)nin : input(int), nout : output(int), return: ufunc

NumPy的random随机库(生成n维随机数组)

用法说明
np.random.rand(d0,d1,…,dn)[0,1]区间均匀分布
np.random.randn(d0,d1,…,dn)标准正态分布
np.random.randint(low,high,size)[low,high] 随机整数数组
np.random.seed(s)随机种子数
随机排列
np.random.shuffle(a)按a第一轴随机排列,改变a
np.random.permutation(a)按a第一轴随机排列,生成副本
np.random.choice(a,size,replace,p)以概率p数组a抽样,样本大小为size
随机分布
np.random.uniform(low,high,size)均匀分布数组
np.random.normal(loc,scale,size)正态分布数组
np.random.poisson(lam,size)泊松分布数组
# 示例:随机漫步
nsteps = 1000
draws = np.random.randint(0, 2, size=nsteps)
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum()
plt.plot(walk[:100])

Numba

Numba是一个开源项目,它可以利用CPUs、GPUs或其它硬件为类似NumPy的数据创建快速函数。它使用了LLVM项目,将Python代码转换为机器代码

Scientific Computing with Python 3 English | 23 Dec. 2016 | ISBN: 1786463512 | 332 Pages | AZW3/MOBI/EPUB/PDF (conv) | 17.95 MB Key Features Your ultimate resource for getting up and running with Python numerical computations Explore numerical computing and mathematical libraries using Python 3.x code with SciPy and NumPy modules A hands-on guide to implementing mathematics with Python, with complete coverage of all the key concepts Book Description Python can be used for more than just general-purpose programming. It is a free, open source language and environment that has tremendous potential for use within the domain of scientific computing. This book presents Python in tight connection with mathematical applications and demonstrates how to use various concepts in Python for computing purposes, including examples with the latest version of Python 3. Python is an effective tool to use when coupling scientific computing and mathematics and this book will teach you how to use it for linear algebra, arrays, plotting, iterating, functions, polynomials, and much more. What you will learn The principal syntactical elements of Python The most important and basic types in Python The essential building blocks of computational mathematics, linear algebra, and related Python objects Plot in Python using matplotlib to create high quality figures and graphics to draw and visualize your results Define and use functions and learn to treat them as objects How and when to correctly apply object-oriented programming for scientific computing in Python Handle exceptions, which are an important part of writing reliable and usable code Two aspects of testing for scientific programming: Manual and Automatic About the Author Claus Fuhrer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016. Jan Erik Solem is a Python enthusiast, former associate professor, and currently the CEO of Mapillary, a street imagery computer vision company. He has previously worked as a face recognition expert, founder and CTO of Polar Rose, and computer vision team leader at Apple. Jan is a World Economic Forum technology pioneer and won the Best Nordic Thesis Award 2005-2006 for his dissertation on image analysis and pattern recognition. He is also the author of "Programming Computer Vision with Python" (O'Reilly 2012). Olivier Verdier began using Python for scientific computing back in 2007 and received a PhD in mathematics from Lund University in 2009. He has held post-doctoral positions in Cologne, Trondheim, Bergen, and Umea and is now an associate professor of mathematics at Bergen University College, Norway. Table of Contents Getting Started Variables and Basic Types Container Types Linear Algebra – Arrays Advanced Array Concepts Plotting Functions Classes Iterating Error Handling Namespaces, Scopes, and Modules Input and Output Testing Comprehensive Examples Symbolic Computations - SymPy References
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值