Numpy:基础

1.Numpy中的ndarray是一个多维数组对象,其包含了数组中的实际数据和描述该数组的元数据。大部分数组的操作仅仅改变元数据的部分,而不改变实际的数据。

>>> 
#维度元素大小不同的情况
m=numpy.array([numpy.arange(2),numpy.arange(3)])
>>> m
array([array([0, 1]), array([0, 1, 2])], dtype=object)
>>> m.shape
(2,)
>>> 
#维度元素大小相同的情况
m=numpy.array([numpy.arange(2),numpy.arange(2)])
>>> 
>>> m
array([[0, 1],
       [0, 1]])
>>> m.dtype##数组中数据的类型
dtype('int32')
>>> m.shape##数组的维度信息
(2, 2)

2.数组中的元素选取使用array[dimen1,dimen2….]这种格式

>>> k
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
>>> k.shape
(3, 3)
>>> k.dtype
dtype('int32')
>>> k[0,0]
0
>>> k[2,2]
2
>>> k[0,]##取出整个维度的数据
array([0, 1, 2])
>>> k[,2]#想去每一个维度同一位置的做法却不可取
SyntaxError: invalid syntax
>>> 

3.Numpy中的数据类型

numpy中许多函数是可以指定数据类型的,比如arange(),array(),ndarray()。数据类型对象是numpy.dtype类的实例。如前所述,NumPy数组是有数据类型的,更确切
地说,NumPy数组中的每一个元素均为相同的数据类型。数据类型对象可以给出单个数组元素在
内存中占用的字节数,即dtype类的itemsize属性:

>>> p=numpy.arange(5,dtype=numpy.float32)
>>> p
array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)
#数据类型对象的大小
>>> p.dtype.itemsize
4
  1. Numpy 中的数据类型编码
    这里写图片描述
Numpy中的数据类型列表,数据类型对象有多个属性,其中包括类型编码,在内存中的存储格式等。
>>> numpy.sctypeDict.keys()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 'unicode', 23, 'cfloat', 'longfloat', 'Int32', 'Complex64', 'unicode_', 'complex', 'timedelta64', 'uint16', 'c16', 'float32', 'int32', 'D', 'float96', 'H', 'void', 'unicode0', 'L', 'P', 'half', 'void0', 'd', 'h', 'l', 'p', 22, 'Timedelta64', 'object0', 'b1', 'M8', 'String0', 'float16', 'ulonglong', 'i1', 'uint32', '?', 'Void0', 'complex64', 'G', 'O', 'UInt8', 'S', 'byte', 'UInt64', 'g', 'float64', 'ushort', 'Complex96', 'float_', 'uint', 'object_', 'Float16', 'complex_', 'Unicode0', 'uintp', 'intc', 'csingle', 'datetime64', 'float', 'bool8', 'Bool', 'intp', 'uintc', 'bytes_', 'u8', 'Float96', 'u4', 'int_', 'cdouble', 'u1', 'complex128', 'u2', 'f8', 'Datetime64', 'ubyte', 'm8', 'complex192', 'B', 'uint0', 'F', 'bool_', 'uint8', 'c8', 'Int64', 'Int8', 'Complex32', 'V', 'int8', 'uint64', 'b', 'c24', 'f', 'double', 'UInt32', 'clongdouble', 'str', 'f12', 'f2', 'f4', 'int', 'longdouble', 'single', 'string', 'q', 'Int16', 'Float64', 'longcomplex', 'UInt16', 'bool', 'Float32', 'string0', 'longlong', 'i8', 'int16', 'str_', 'I', 'object', 'M', 'i4', 'singlecomplex', 'Q', 'string_', 'U', 'a', 'short', 'e', 'i', 'clongfloat', 'm', 'Object0', 'int64', 'i2', 'int0']

numpyz中允许自定义异构数据类型。

##自定义异构数据类型y
>>> y=numpy.dtype([('name',str,40),('numitem',numpy.int32),('price',numpy.float32)])
>>> y
dtype([('name', 'S40'), ('numitem', '<i4'), ('price', '<f4')])
>>> y.str
'|V48'
###使用自定义数据类型定义数组
>>> item=numpy.array([("onething",001,34.3),("otherthing",002,34.5),("onemorething",003,45.6)],dtype=y)
>>> item
array([('onething', 1, 34.29999923706055), ('otherthing', 2, 34.5),
       ('onemorething', 3, 45.599998474121094)], 
      dtype=[('name', 'S40'), ('numitem', '<i4'), ('price', '<f4')])
>>> 

5.numpy中的array可以使用reshape()函数或者resize()函数,不同的是resize()函数会对原数组直接修改。reshape()对原数组不影响。重构数组为多维数组,重构后的数组可以使用原python中的切片索引对数组进行操作。比如o=numpy.arange(24).reshape(2,3,4)就可以把一维的数组重构为三维的数组。重构时要注意数组元素个数的一致性。对重构后的数组切片操作和原python类似。
6.numpy.array的高维对象可以使用arrayobject.reval()(函数只是返回一个视图显示效果)或arrayobject.flatten()(函数返回的结果会有相应内存分配存储数据)函数使高维数组展位一维。

>>> i
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]]])
>>> i[...,1]
#ravel和flatten返回值形式无区别,内存结构有区别
array([[ 1,  5,  9],
       [13, 17, 21]])
>>> i.flatten()
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])  
>>> i.ravel()
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])         

7numpy中支持对数组进行组合操作。我们按照水平、垂直和深度等方式进行了组合数组的操作。我们使用了vstack(垂直组合,垂直方向组合两个数组)、dstack(深度组合,两个元组组合为一个列表元素)、hstack(水平方向上组合)、column_stack(列方向组合数组,这个特别点)、row_stack(行方向组合数组,这个特别点)以及concatenate(可以设置哪个轴方向组合数组)函数。vstack垂直组合实际上是axis=1轴上的组合,hstack实际上是axis=0方向上的组合。二维以上的数组,row_stack和vstack的效果一样而column_stack和hstack的效果一样。

>>> numpy.column_stack((a,b))
array([[[  0,   1,   2,   3],
        [  4,   5,   6,   7],
        [  8,   9,  10,  11],
        [  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121]],

       [[ 12,  13,  14,  15],
        [ 16,  17,  18,  19],
        [ 20,  21,  22,  23],
        [144, 169, 196, 225],
        [256, 289, 324, 361],
        [400, 441, 484, 529]]])
>>> a
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]]])
>>> b
array([[[  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121]],

       [[144, 169, 196, 225],
        [256, 289, 324, 361],
        [400, 441, 484, 529]]])
 >>> numpy.row_stack((a,b))==numpy.vstack((a,b))
array([[[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]],

       [[ True,  True,  True,  True],
        [ True,  True,  True,  True],
        [ True,  True,  True,  True]]], dtype=bool)
>>> 

8.Numpy可以使用hsplit,vsplit,dsplit或者split对数组按照特定的条件分为子数组。hsplit水平方向上的分隔,相当于axis=0方向的分隔,numpy.hsplit(array,num)其中num数字为该维能均分的数字。numpy.vsplit(array,num)与hsplit一样,只是维度为axis=1。numpy.split(array,num,axis=axisnum)。numpy.dsplit(array,num)只用于最少三维的数组进行分隔。

9.数组中的属性。array.dtype求元素的数据类型, array.ndim求数组的轴数, array.size求数组中共有多少个元素,array.T求数组的转置(一维数组的装置是其本身),array.nbytes求整个数组元素所占的内存大小,array.itemsize求单个数组元素所占内存大小,array.real求数组的实部,array.imag求数组的虚部
array.flat会返回一个numpy.flatiter对象,该对象是“扁平迭代器”能遍历整个多维数组。
这里写图片描述

>>> a=numpy.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a.flat
<numpy.flatiter object at 0x02F86940>
>>> for i in a.flat:##迭代器
    print i


0
1
2
3
4
5
6
7
8
9
10
11
>>> a.flat[4]=89##可以对flatiter里边的单个元素赋值
>>> a
array([[ 0,  1,  2,  3],
       [89,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> 

10.可以使用array.tolist()函数使numpy数组转换为python列表。使用array.astype(dtype)转换数据类型,若是复数转为整数会丢失虚部。

>>> a
array([[ 0,  1,  2,  3],
       [89,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a.tolist()
[[0, 1, 2, 3], [89, 5, 6, 7], [8, 9, 10, 11]]
>>> a.astype(float)
array([[  0.,   1.,   2.,   3.],
       [ 89.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.]])
>>> 

11.这里学习了numpy中的数据类型和数组。对于数组而言,有很多属性可以用来描述数组,数据类型就是其中之一。在NumPy中,数组的数据类型是用对象来表示的。类似于Python列表,NumPy数组也可以方便地进行切片和索引操作。在多维数组上,NumPy有明显的优势。涉及改变数组维度的操作有很多种——组合、调整、设置维度和分割等。在这一章中,对很多改变数组维度的实用函数进行了说明。

发布了30 篇原创文章 · 获赞 4 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章