#!/usr/bin/env python # coding: utf-8 # ##内容索引 # 该小结主要介绍了NumPy数组的基本操作。 # # 子目1中,介绍创建和索引数组,数据类型,dtype类,自定义异构数据类型。 # # 子目2中,介绍数组的索引和切片,主要是对[]运算符的操作。 # # 子目3中,介绍如何改变数组的维度,分别介绍了**ravel函数、flatten函数、transpose函数、resize函数、reshape函数的用法。** # In[1]: get_ipython().run_line_magic('pylab', 'inline') # ndarray是一个多维数组对象,该对象由**实际的数据、描述这些数据的元数据**组成,大部分数组操作仅仅修改元数据部分,而不改变底层的实际数据。 # 用arange函数创建数组 # In[2]: a = arange(5) a.dtype # In[3]: a # In[4]: a.shape # 数组的shape属性返回一个元祖(tuple),元组中的元素即NumPy数组每一个维度的大小。 # ##1. 创建多维数组 # array函数可以依据给定的对象生成数组。 # 给定的对象应是类数组,如python的列表、numpy的arange函数 # In[5]: m = array([arange(2), arange(2)]) # In[6]: print m print m.shape print type(m) print type(m.shape) # ###选取元素 # In[7]: a = array([[1,2],[3,4]]) print a[0,0] print a[0,1] # ###NumPy数据类型 # Numpy除了Python支持的整型、浮点型、复数型之外,还添加了很多其他的数据类型。 # # Type Remarks Character code # bool_ compatible: Python bool '?' # bool8 8 bits # Integers: # # byte compatible: C char 'b' # short compatible: C short 'h' # intc compatible: C int 'i' # int_ compatible: Python int 'l' # longlong compatible: C long long 'q' # intp large enough to fit a pointer 'p' # int8 8 bits # int16 16 bits # int32 32 bits # int64 64 bits # Unsigned integers: # # ubyte compatible: C unsigned char 'B' # ushort compatible: C unsigned short 'H' # uintc compatible: C unsigned int 'I' # uint compatible: Python int 'L' # ulonglong compatible: C long long 'Q' # uintp large enough to fit a pointer 'P' # uint8 8 bits # uint16 16 bits # uint32 32 bits # uint64 64 bits # Floating-point numbers: # # half 'e' # single compatible: C float 'f' # double compatible: C double # float_ compatible: Python float 'd' # longfloat compatible: C long float 'g' # float16 16 bits # float32 32 bits # float64 64 bits # float96 96 bits, platform? # float128 128 bits, platform? # Complex floating-point numbers: # # csingle 'F' # complex_ compatible: Python complex 'D' # clongfloat 'G' # complex64 two 32-bit floats # complex128 two 64-bit floats # complex192 two 96-bit floats, platform? # complex256 two 128-bit floats, platform? # Any Python object: # # object_ any Python object 'O' # **每一种数据类型均有对应的类型转换函数** # In[8]: print float64(42) print int8(42.0) print bool(42) print float(True) # In[9]: arange(8, dtype=uint16) # **复数不能转换成整数和浮点数** # Numpy数组中每一个元素均为相同的数据类型,现在给出单个元素所占字节 # In[10]: a.dtype # In[11]: a.dtype.itemsize # **dtype类的属性** # In[12]: t = dtype('float64') print t.char print t.type print t.str # str属性可以给出数据类型的字符串表示,该字符串的首个字符表示字节序,然后是字符编码,然后是所占字节数 # 字节序是指位长为32和64的字(word)存储的顺序,包括大端序(big-endian)和小端序(little-endian)。 # 大端序是将最高位字节存储在最低的内存地址处,用>表示;与之相反,小端序是将最低位字节存储在最低的内存地址处,用<表示。 # **创建自定义数据类型** # 自定义数据类型是一种异构数据类型,可以当做用来记录电子表格或数据库中一行数据的结构。 # # 下面我们创建一种自定义的异构数据类型,该数据类型包括一个用字符串记录的名字、一个用整数记录的数字以及一个用浮点数记录的价格。 # In[13]: t = dtype([('name', str_, 40), ('numitems', int32), ('price', float32)]) # In[14]: t # In[15]: t['name'] # In[16]: itemz = array([('Meaning of life DVD', 32, 3.14), ('Butter', 13, 2.72)], dtype=t) # In[17]: itemz[1] # ##2. 数组的索引和切片 # In[18]: a = arange(9) #下标0-7, 以2为步长 print a[:7:2] #以负数下标翻转数组 print a[::-1] print a[::-2] # ###多维数组的切片和索引 # In[19]: b = arange(24).reshape(2,3,4) print b.shape print b # 用三维坐标选定任意一个房间,即楼层、行号、列号 # In[20]: #选取第一层楼所有房间 print b[0] print print b[0, :, :] # In[21]: #多个冒号用一个省略号代替 b[0, ...] # In[22]: #间隔选元素 b[0,1,::2] # In[23]: #多维数组执行翻转一维数组的命令,将在最前面的维度上翻转元素的顺序 b[::-1] # In[24]: b[::-1,::-1,::-1] # ##3. 改变数组的维度 # **ravel 完成展平操作** # In[25]: b.ravel() # **flatten 也是展平** # # flatten函数会请求分配内存来保存结果,而ravel函数只是返回数组的一个视图(view) # In[26]: b.flatten() # **用元组设置维度** # In[27]: b.shape = (6, 4) # In[28]: b # **transpose转置矩阵** # In[29]: b.transpose() # **resize和reshape函数功能一样** # 但resize会直接改变所操作的数组 # In[30]: b.reshape(2,3,4) # In[31]: b # In[32]: b.resize(2,12) # In[33]: b