http://blog.csdn.net/pipisorry/article/details/22107553

array对象属性及内建方法

数组属性

The following attributes contain information about the memory layoutof the array:

ndarray.flags Information about the memory layout of the array.查看数据存储区域的属性。C_CONTIGUOUS : True  F_CONTIGUOUS : False  OWNDATA : True  WRITEABLE : True  ALIGNED : True  UPDATEIFCOPY : False
ndarray.shape Tuple of array dimensions. 代表一个array的形态,是一个向量还是一个矩阵,抑或是一个更复杂的向量组。数组的大小可以通过其shape属性获得.数组的维度。这是一个指示数组在每个维度上大小的整数元组。例如一个n排m列的矩阵,它的shape属性将是(2,3),这个元组的长度显然是秩,即维度或者ndim属性。
ndarray.strides Tuple of bytes to step in each dimension when traversing an array.
ndarray.ndim Number of array dimensions.代表这个array的维度,数组轴的个数,结果是一个数,在python的世界中,轴的个数被称作秩。
ndarray.data Python buffer object pointing to the start of the array’s data.包含实际数组元素的缓冲区,通常我们不需要使用这个属性,因为我们总是通过索引来使用数组中的元素。
ndarray.size Number of elements in the array.在array中拥有的元素数量。数组元素的总个数,等于shape属性中元组元素的乘积。
ndarray.itemsize Length of one array element in bytes.这个array数组中每一个元素所占的字节数、字节大小(数组中的数据项的所占内存空间大小)。例如,一个元素类型为float64的数组itemsiz属性值为8(=64/8),又如,一个元素类型为complex32的数组item属性为4(=32/8).
ndarray.nbytes Total bytes consumed by the elements of the array.这个array的总字节数(=itemsize*size)
ndarray.base Base object if memory is from some other object.

[Memory layout¶]

The data type object associated with the array can be found in thedtype attribute:

ndarray.dtype Data-type of the array’s elements.一个用来描述数组中元素类型的对象,可以通过创造或指定dtype使用标准Python类型。另外NumPy提供它自己的数据类型。

[Data type¶]

ndarray.T Same as self.transpose(), except that self is returned if self.ndim < 2.矩阵转置,同transpose()方法。
ndarray.real The real part of the array.代表一个array中所有元素的实数部分
ndarray.imag The imaginary part of the array.代表一个array中所有元素的虚数部分
ndarray.flat A 1-D iterator over the array.将这个array整理成一维的,可以索引的一系列的元素组合。它实际上是通过iterator实现的,我们可以通过for x in array.flat来取得到所有的元素。
ndarray.ctypes An object to simplify the interaction of the array with the ctypes module.

Note:

1 与matrix不同,没有.I。

2 .reshape函数改变参数形状并返回它,而resize函数改变数组自身。

3 .ndim:     在NumPy中维度(dimensions)叫做轴(axes),轴的个数叫做秩(rank)。例如,以下例子中,数组的秩为2(它有两个维度).第0维度长度为2,第1维度长度为3.
    [[ 1., 0., 0.],
     [ 0., 1., 2.]]

三维数组的维度

如x = [[[10, 20], [30, 40]], [[50, 60], [70, 80]]]

[[[10 20]
  [30 40]]

[[50 60]
  [70 80]]]

4 .T: 一维列向量的转置还是本身。如(3,)的向量[1,2,3]其转置还是[1,2,3]。

5 .base. Slicing creates a view, whose memory is shared with x:

>>>

>>> y = x[2:]
>>> y.base is x
True

6 .strides:Tuple of bytes to step in each dimension when traversing an array.

The byte offset of element (i[0],i[1],...,i[n]) in an arrayais:

offset = sum(np.array(i) * a.strides)
>>> y = np.reshape(np.arange(2*3*4), (2,3,4))
>>> y
array([[[ 0,  1,  2,  3],[ 4,  5,  6,  7],[ 8,  9, 10, 11]],[[12, 13, 14, 15],[16, 17, 18, 19],[20, 21, 22, 23]]])
>>> y.strides
(48, 16, 4)

Note:strides就是n维上数组的offset的元组表示,上面0维上的offset是1*4=4bytes,1维上的offset是4*4=16bytes,2维上的offset是3*4*4=48bytes.

[Other attributes¶]

皮皮Blog

ndarray数组内建方法

Array conversion

ndarray.item(*args) Copy an element of an array to a standard Python scalar and return it.取得某一位置的元素
ndarray.tolist() Return the array as a (possibly nested) list.将array转化成一个Python中的list对象,ndarray转换成python list
ndarray.itemset(*args) Insert scalar into an array (scalar is cast to array’s dtype, if possible)
ndarray.tostring([order]) Construct Python bytes containing the raw data bytes in the array.返回的是bytes。
ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array.
ndarray.tofile(fid[, sep, format]) Write array to a file as text or binary (default).
ndarray.dump(file) Dump a pickle of the array to the specified file.将这个对象序列化至文件。同cPickle中的dump作用
ndarray.dumps() Returns the pickle of the array as a string.将序列化的结果通过字符串加以输出
ndarray.astype(dtype[, order, casting, ...]) Copy of the array, cast to a specified type.
ndarray.byteswap(inplace) Swap the bytes of the array elements
ndarray.copy([order]) Return a copy of the array.
ndarray.view([dtype, type]) New view of array with the same data.
ndarray.getfield(dtype[, offset]) Returns a field of the given array as a certain type.
ndarray.setflags([write, align, uic]) Set array flags WRITEABLE, ALIGNED, and UPDATEIFCOPY, respectively.
ndarray.fill(value) Fill the array with a scalar value.

numpy类型及数据类型转换

numpy函数返回标准Python数值类型

通过下标所获取的数组元素的类型为NumPy中所定义的类型。将其转换为Python的标准类型还需要花费额外的时间。为了解决这个问题,数组提供了item()方法,它用来获取数组中的单个元素,并且直接返回标准的Python数值类型:

>>> a = np.arange(6.0).reshape(2,3)
>>> a.item(1,2) # 和a[1,2]类似
5.0
>>> type(a.item(1,2)) # item()所返回的是Python的标准float类型
<type 'float'>
>>> type(a[1,2]) # 下标方式获得的是NumPy的float64类型
<type 'numpy.float64'>

NumPy和SciPy多维数组相互转化

import numpy
import scipy
a1 = zeros((4,6))
type(a1)
<type 'scipy.ndarray'>
a2 = numpy.asarray(a1)
type(a2)
<type 'numpy.ndarray'>
a3 = numpy.zeros((3,5))
type(a3)
<type 'numpy.ndarray'>
a4 = scipy.asarray(a3)
type(a4)
<type 'scipy.ndarray'>

ndarray中的list转换为tuple

data = np.array(  [[ 0.22403094,  0.08515318],  [ 0.7529882 , -0.65134297],  [ 1.41298052,  0.94194292],  [-0.45589253, -1.00021018]] )
data = data.T
data = zip(data[0], data[1])
print(data)
[(0.22403094000000001, 0.085153179999999995),(0.7529882, -0.65134296999999997),(1.4129805200000001, 0.94194292000000002),(-0.45589253000000002, -1.0002101800000001)]

numpy bool数组转换成整型int数组

a = np.array([True, False, True, False, False, True]).astype(int)
print(a)     #[1 0 1 0 0 1]

numpy string数组转换成double数组

a = np.array(['-0.99', '', '0.56', '0.56', '-2.02', '-0.96'])
a[a == ''] = 0.0
a = a.astype(float)
print(a)    #[-0.99  0.    0.56  0.56 -2.02 -0.96]

Note: 也可以这样转换a = list(map(float, a))

将int数组转换成bool数组

a.astype('bool')

Note: 负数如-1转换成bool是True!

将numpy类型转换为datetime

a.astype(np.datetime64)

类型转换时的出错

ValueError: setting an array element with a sequence

出错原因:

1 trying to create an array from a list that isn't shaped like a multi-dimensional array. For examplenumpy.array([[1,2], [2, 3, 4]]) or numpy.array([[1,2], [2, [3, 4]]])

还有个类似的原因就是将array中的元素如['1', '2']当成一个列表元素,而不是当作二维的。这在pandas数据转换时可能发生的,至今lz不明原因:

l_array = df['VenueLocation'].map(lambda s: np.array(s.split(','))).values

2 Another possible cause for this error message is trying to use a string as an element in an array of type float: numpy.array([1.2, "abc"], dtype=float)

[ValueError: setting an array element with a sequence]

Shape manipulation

For reshape, resize, and transpose, the single tuple argument may bereplaced with n integers which will be interpreted as an n-tuple.

ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape.
ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place.
ndarray.transpose(*axes) Returns a view of the array with axes transposed.
ndarray.swapaxes(axis1, axis2) Return a view of the array with axis1 and axis2 interchanged.
ndarray.flatten([order]) Return a copy of the array collapsed into one dimension.
ndarray.ravel([order]) Return a flattened array.
ndarray.squeeze([axis]) Remove single-dimensional entries from the shape of a.

ravel(): flatten the array.由ravel()展平的数组元素的顺序通常是“C风格”的,就是说,最右边的索引变化得最快,所以元素a[0,0]之后是a[0,1]。如果数组被改变形状(reshape)成其它形状,数组仍然是“C风格”的。NumPy通常创建一个以这个顺   序保存数据的数组,所以ravel()将总是不需要复制它的参数3。但是如果数组是通过切片其它数组或有不同寻常的选项时,它可能需要被复制。函数reshape()和ravel()还可以被同过一些可选参数构建成FORTRAN风格的数组,即最左边的索引变化最快。

Item selection and manipulation

For array methods that take an axis keyword, it defaults toNone. If axis is None, then the array is treated as a 1-Darray. Any other value for axis represents the dimension along whichthe operation should proceed.

ndarray.take(indices[, axis, out, mode]) Return an array formed from the elements of a at the given indices.
ndarray.put(indices, values[, mode]) Set a.flat[n] = values[n] for all n in indices.
ndarray.repeat(repeats[, axis]) Repeat elements of an array.
ndarray.choose(choices[, out, mode]) Use an index array to construct a new array from a set of choices.
ndarray.sort([axis, kind, order]) Sort an array, in-place.
ndarray.argsort([axis, kind, order]) Returns the indices that would sort this array.
ndarray.partition(kth[, axis, kind, order]) Rearranges the elements in the array in such a way that value of the element in kth position is in the position it would be in a sorted array.
ndarray.argpartition(kth[, axis, kind, order]) Returns the indices that would partition this array.
ndarray.searchsorted(v[, side, sorter]) Find indices where elements of v should be inserted in a to maintain order.
ndarray.nonzero() Return the indices of the elements that are non-zero.
ndarray.compress(condition[, axis, out]) Return selected slices of this array along given axis.
ndarray.diagonal([offset, axis1, axis2]) Return specified diagonals.

Calculation

ndarray.argmax([axis, out]) Return indices of the maximum values along the given axis.
ndarray.min([axis, out, keepdims]) Return the minimum along a given axis.取得最小值。还有一点值得说,就是max、min这些函数都可以针对某一坐标轴(具体维度)进行运算,例如array.max(axis=0),就在0坐标上求最大值{0-按列求值,返回一行, 1-按行求值,返回一列}
ndarray.argmin([axis, out]) Return indices of the minimum values along the given axis of a.
ndarray.ptp([axis, out]) Peak to peak (maximum - minimum) value along a given axis.
ndarray.clip([min, max, out]) Return an array whose values are limited to [min, max].
ndarray.conj() Complex-conjugate all elements.
ndarray.round([decimals, out]) Return a with each element rounded to the given number of decimals.
ndarray.trace([offset, axis1, axis2, dtype, out]) Return the sum along diagonals of the array.
ndarray.sum([axis, dtype, out, keepdims]) Return the sum of the array elements over the given axis.
ndarray.cumsum([axis, dtype, out]) Return the cumulative sum of the elements along the given axis.求累计和
ndarray.mean([axis, dtype, out, keepdims]) Returns the average of the array elements along given axis.
ndarray.var([axis, dtype, out, ddof, keepdims]) Returns the variance of the array elements, along given axis. e.g. np.var(a,axis=0)
ndarray.std([axis, dtype, out, ddof, keepdims]) Returns the standard deviation of the array elements along given axis.
ndarray.prod([axis, dtype, out, keepdims]) Return the product of the array elements over the given axis求所有元素之积
ndarray.cumprod([axis, dtype, out]) Return the cumulative product of the elements along the given axis.求累计积
ndarray.all([axis, out, keepdims]) Returns True if all elements evaluate to True.如果所有元素都为真,那么返回真;否则返回假。
e.g. 判断ndarray中的元素是否都>0:if (b > 0).all()
ndarray.any([axis, out, keepdims]) Returns True if any of the elements of a evaluate to True.只要有一个元素为真则返回真

Arithmetic, matrix multiplication, and comparison operations¶

Comparison operators:Unary operations:Arithmetic:Arithmetic, in-place:

Special methods¶

For standard library functions:Basic customization:Container customization: (see Indexing)

Conversion; the operations complex, int,long, float, oct, andhex. They work only on arrays that have one element in themand return the appropriate scalar.

String representations:

怎么没有ndarray.dot(b[, out])了?相当于matlab中的*。dot product of two arrays.参考下面基本运算部分。

np.vdot(a,b)专门计算矢量的点积,和dot()的区别在于对complex数据类型的处理不一样;inner(a,b)用来计算内积;outer(a,b)计算外积。

[Array methods¶]

[numpy-ref-1.8.1: page14]

皮皮Blog

numpy数组操作

[numpy教程:数组操作]

数学运算

[numpy教程:数学函数和基本统计函数 ]

皮皮Blog

多维数组的迭代

是就第一个轴而言的

>>> for row in b:
...         print row

数组元素的迭代器

对每个数组中元素进行运算,我们可以使用flat属性,该属性是数组元素的一个迭代器:

>>> for element in b.flat:
...         print element,

特别的例子

a=np.arange(0,60,10).reshape(-1,1)+np.arange(0,6)

np.arange(0,60,10).reshape(-1,1) =

[[ 0]
 [10]
 [20]
 [30]
 [40]
 [50]]
>>> a
array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])

其中 a[3,3:6]表示第4行的第4到6列,结果是array([33, 34, 35]);

a[:2] 表示前两行:array([[ 0,  1,  2,  3,  4,  5],

[10, 11, 12, 13, 14, 15]])

注意区分:

a[:2] 和a[[0,1]]  a[[0,1],:]都可以表示上面的结果。但是第一个切片是按顺序的,第二个可以随意。比如说:

>>> a[[1,2,0,2]]
array([[10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [ 0,  1,  2,  3,  4,  5],
       [20, 21, 22, 23, 24, 25]])

a[:,2] 表示第3列,array([ 2, 12, 22, 32, 42, 52])

a[1::,::2]表示的是(行从1~5,步长默认1;列从0~5,步长2)
array([[10, 12, 14],
       [20, 22, 24],
       [30, 32, 34],
       [40, 42, 44],
       [50, 52, 54]])

from:http://blog.csdn.net/pipisorry/article/details/22107553

ref: [The N-dimensional array (ndarray)¶]*

numpy - 介绍、基本数据类型、多维数组ndarray及其内建函数

NumPy Reference*

Python For Data Analysis’s documentation

Theano学习二----numpy

python学习笔记1

Numpy教程

python快速处理数据

numpy教程:ndarray属性、内建函数及其运算、迭代相关推荐

  1. [转载] numpy教程:矩阵matrix及其运算

    参考链接: Python中的numpy.asmatrix http://blog.csdn.net/pipisorry/article/details/48791403 numpy矩阵简介 NumPy ...

  2. python科学计算笔记(一)NumPy中ndarray对象、ufunc运算、矩阵运算

    标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1,2,3],需要有3个指针和三个 ...

  3. Python 数据分析三剑客之 NumPy(三):数组的迭代与位运算

    CSDN 课程推荐:<Python 数据分析与挖掘>,讲师刘顺祥,浙江工商大学统计学硕士,数据分析师,曾担任唯品会大数据部担任数据分析师一职,负责支付环节的数据分析业务.曾与联想.亨氏.网 ...

  4. TutorialsPoint NumPy 教程

    来源:NumPy Tutorial - TutorialsPoint 译者:飞龙 协议:CC BY-NC-SA 4.0 在线阅读 PDF格式 EPUB格式 MOBI格式 代码仓库 NumPy - 简介 ...

  5. TutorialsPoint NumPy 教程(转)

    TutorialsPoint NumPy 教程(转) 来源:NumPy Tutorial - TutorialsPoint 译者:飞龙 协议:CC BY-NC-SA 4.0 在线阅读 PDF格式 EP ...

  6. 看到一篇详细的关于Python之Numpy教程分享给和我一样在编程上的小白

    NumPy - 简介 NumPy 是一个 Python 包. 它代表 "Numeric Python". 它是一个由多维数组对象和用于处理数组的例程集合组成的库. Numeric, ...

  7. 最全的NumPy教程

    译者:飞龙 译文:https://www.jianshu.com/p/57e3c0a92f3a 原文:https://www.tutorialspoint.com/numpy/index.htm Nu ...

  8. display属性_Numpy知识点(1)讲解实操安装/属性/数组创建/运算

    # 1.安装包# pip install numpy #原生python安装# conda install numpy #Anaconda的安装 # 使用Numpyimport numpy as np ...

  9. Numpy中ndarray的常见操作

    NumPy(Numerical Python)是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要 ...

  10. 【Python】Numpy基础:数组和矢量运算

    Numpy基础:数组和矢量运算 目录: 文章目录 一 Numpy及其重要性 二 ndarray:一种多维数组对象 1 创建ndarray 2 ndarray的数据类型 3 数组和标量之间的运算 4 基 ...

最新文章

  1. js数组中indexOf/filter/forEach/map/reduce详解
  2. python类方法中使用:修饰符@staticmethod和@classmethod的作用与区别,还有装饰器@property的使用
  3. ServiceMesh究竟解决什么问题?
  4. failed to get the task for process XXX(解决方案)
  5. 2名数学家或发现史上最快超大乘法运算法,欲破解困扰人类近半个世纪的问题...
  6. downloader怎么用 hls_如何下载企业微信直播回放视频(HLS格式)
  7. 三体与计算机科学与技术,《三体》作者刘慈欣:AlphaGo赢了,但人工智能不可能强大到没有弱点...
  8. 人工智能python基础知识_AI 人工智能基础知识-习题
  9. linux中Grep常用的15个例子,Linux中Grep惯用的15个例子
  10. linux读取 dev tty0,linux命令: ls命令
  11. asp.net C#绘制太极图
  12. 高等代数第3版下 [丘维声 著] 2015年版_2020年成人高考 专升本 高等数学复习攻略...
  13. 科研的常用软件高效神器
  14. R语言使用strsplit函数按照指定的分隔符号进行数据拆分、分裂(split)、分割后的数据类型为列表
  15. php 怎么把数组按拼音,php数组如何按拼音顺序排序
  16. python 变量 fields_理解Python数据类:Dataclass fields 的概述(下)
  17. 程序员相亲被大三岁的富婆姐姐看上,让他当《杭州赘婿》
  18. (Emitted value instead of an instance of Error)
  19. 线上抓娃娃机火了三个月了,你玩了吗?
  20. 微信小程序消息订阅超详细流程步骤

热门文章

  1. [转]常见hash算法的原理
  2. 使用libevhtp搭建HTTPS SERVER(单向验证身份)
  3. Python学习笔记(matplotlib篇)--多图figure
  4. Jenkins_第五关_系统管理(1)
  5. 浅谈Js对象的概念、创建、调用、删除、修改!
  6. Linux修行学习,网站持更
  7. 两个小的java程序,用于练习java基本语法
  8. ArchLinux安装配置
  9. 公共网关接口CGI(Common GatewayInterface)
  10. 在wamp集成环境中添加mysql操作记录