常用numpy用法详细介绍
numpy 简介
numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab。
官方文档(https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)
各种用法介绍
首先是numpy中的数据类型,ndarray类型,和标准库中的array.array并不一样。
ndarray的一些属性
ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.
ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.
ndarray.data
the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.
ndarray的创建
>>> import numpy as np>>> a = np.array([2,3,4])>>> a array([2, 3, 4])>>> a.dtype dtype('int64')>>> b = np.array([1.2, 3.5, 5.1])>>> b.dtype dtype('float64')
二维的数组
>>> b = np.array([(1.5,2,3), (4,5,6)])>>> b array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]])
创建时指定类型
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )>>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]])
创建一些特殊的矩阵
>>> np.zeros( (3,4) ) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified array([[[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]], [[ 1, 1, 1, 1], [ 1, 1, 1, 1], [ 1, 1, 1, 1]]], dtype=int16) >>> np.empty( (2,3) ) # uninitialized, output may vary array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
创建一些有特定规律的矩阵
>>> np.arange( 10, 30, 5 ) array([10, 15, 20, 25]) >>> np.arange( 0, 2, 0.3 ) # it accepts float arguments array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) >>> from numpy import pi >>> np.linspace( 0, 2, 9 ) # 9 numbers from 0 to 2 array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ]) >>> x = np.linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points >>> f = np.sin(x)
一些基本的运算
加减乘除三角函数逻辑运算
>>> a = np.array( [20,30,40,50] ) >>> b = np.arange( 4 ) >>> b array([0, 1, 2, 3]) >>> c = a-b >>> c array([20, 29, 38, 47]) >>> b**2 array([0, 1, 4, 9]) >>> 10*np.sin(a) array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854]) >>> a<35 array([ True, True, False, False], dtype=bool)
矩阵运算
matlab中有.* ,./等等
但是在numpy中,如果使用+,-,×,/优先执行的是各个点之间的加减乘除法
如果两个矩阵(方阵)可既以元素之间对于运算,又能执行矩阵运算会优先执行元素之间的运算
>>> import numpy as np>>> A = np.arange(10,20)>>> B = np.arange(20,30)>>> A + B array([30, 32, 34, 36, 38, 40, 42, 44, 46, 48])>>> A * B array([200, 231, 264, 299, 336, 375, 416, 459, 504, 551])>>> A / B array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])>>> B / A array([2, 1, 1, 1, 1, 1, 1, 1, 1, 1])
如果需要执行矩阵运算,一般就是矩阵的乘法运算
>>> A = np.array([1,1,1,1]) >>> B = np.array([2,2,2,2]) >>> A.reshape(2,2) array([[1, 1], [1, 1]]) >>> B.reshape(2,2) array([[2, 2], [2, 2]]) >>> A * B array([2, 2, 2, 2]) >>> np.dot(A,B) 8 >>> A.dot(B) 8
一些常用的全局函数
>>> B = np.arange(3) >>> B array([0, 1, 2]) >>> np.exp(B) array([ 1. , 2.71828183, 7.3890561 ]) >>> np.sqrt(B) array([ 0. , 1. , 1.41421356]) >>> C = np.array([2., -1., 4.]) >>> np.add(B, C) array([ 2., 0., 6.])
矩阵的索引分片遍历
>>> a = np.arange(10)**3 >>> a array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) >>> a[2] 8 >>> a[2:5] array([ 8, 27, 64]) >>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000 >>> a array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729]) >>> a[ : :-1] # reversed a array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000]) >>> for i in a: ... print(i**(1/3.)) ... nan 1.0 nan 3.0 nan 5.0 6.0 7.0 8.0 9.0
矩阵的遍历
>>> import numpy as np >>> b = np.arange(16).reshape(4, 4) >>> for row in b: ... print(row) ... [0 1 2 3] [4 5 6 7] [ 8 9 10 11] [12 13 14 15] >>> for node in b.flat: ... print(node) ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
矩阵的特殊运算
改变矩阵形状--reshape
>>> a = np.floor(10 * np.random.random((3,4))) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]]) >>> a.ravel() array([ 6., 5., 1., 5., 5., 5., 8., 9., 5., 5., 9., 7.]) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]])
resize和reshape的区别
resize会改变原来的矩阵,reshape并不会
>>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]]) >>> a.reshape(2,-1) array([[ 6., 5., 1., 5., 5., 5.], [ 8., 9., 5., 5., 9., 7.]]) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]]) >>> a.resize(2,6) >>> a array([[ 6., 5., 1., 5., 5., 5.], [ 8., 9., 5., 5., 9., 7.]])
矩阵的合并
>>> a = np.floor(10*np.random.random((2,2)))>>> a array([[ 8., 8.], [ 0., 0.]])>>> b = np.floor(10*np.random.random((2,2)))>>> b array([[ 1., 8.], [ 0., 4.]])>>> np.vstack((a,b)) array([[ 8., 8.], [ 0., 0.], [ 1., 8.], [ 0., 4.]])>>> np.hstack((a,b)) array([[ 8., 8., 1., 8.], [ 0., 0., 0., 4.]])
以上是常用numpy用法详细介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

更新numpy版本方法:1、使用“pip install --upgrade numpy”命令;2、使用的是Python 3.x版本,使用“pip3 install --upgrade numpy”命令,将会下载并安装,覆盖当前的NumPy版本;3、若使用的是conda来管理Python环境,使用“conda install --update numpy”命令更新即可。

Numpy是Python中一个重要的数学库,它提供了高效的数组操作和科学计算函数,被广泛应用于数据分析、机器学习、深度学习等领域。在使用numpy过程中,我们经常需要查看numpy的版本号,以便确定当前环境所支持的功能。本文将介绍如何快速查看numpy版本,并提供具体的代码示例。方法一:使用numpy自带的__version__属性numpy模块自带一个__

推荐使用最新版本的NumPy1.21.2。原因是:目前,NumPy的最新稳定版本是1.21.2。通常情况下,推荐使用最新版本的NumPy,因为它包含了最新的功能和性能优化,并且修复了之前版本中的一些问题和错误。

如何升级numpy版本:简单易懂的教程,需要具体代码示例引言:NumPy是一个重要的Python库,用于科学计算。它提供了一个强大的多维数组对象和一系列与之相关的函数,可用于进行高效的数值运算。随着新版本的发布,不断有更新的特性和Bug修复可供我们使用。本文将介绍如何升级已安装的NumPy库,以获取最新特性并解决已知问题。步骤1:检查当前NumPy版本在开始

一步步教你在PyCharm中安装NumPy并充分利用其强大功能前言:NumPy是Python中用于科学计算的基础库之一,提供了高性能的多维数组对象以及对数组执行基本操作所需的各种函数。它是大多数数据科学和机器学习项目的重要组成部分。本文将向大家介绍如何在PyCharm中安装NumPy,并通过具体的代码示例展示其强大的功能。第一步:安装PyCharm首先,我们

numpy增加维度的方法:1、使用“np.newaxis”增加维度,“np.newaxis”是一个特殊的索引值,用于在指定位置插入一个新的维度,可以通过在对应的位置使用np.newaxis来增加维度;2、使用“np.expand_dims()”增加维度,“np.expand_dims()”函数可以在指定的位置插入一个新的维度,用于增加数组的维度

numpy可以通过使用pip、conda、源码和Anaconda来安装。详细介绍:1、pip,在命令行中输入pip install numpy即可;2、conda,在命令行中输入conda install numpy即可;3、源码,解压源码包或进入源码目录,在命令行中输入python setup.py build python setup.py install即可。

随着数据科学、机器学习和深度学习等领域的快速发展,Python成为了数据分析和建模的主流语言。在Python中,NumPy(NumericalPython的简称)是一个很重要的库,因为它提供了一组高效的多维数组对象,也是许多其他库如pandas、SciPy和scikit-learn的基础。在使用NumPy过程中,很有可能会遇到不同版本之间的兼容性问题,那么
