numPy 学习1

来源:互联网 发布:网络推广主要做什么 编辑:程序博客网 时间:2024/04/27 03:17
not allowed to change a complex number into a integernot allowed to change a complex number into a floating-point numberyou can convert a floating-point number to a complex number, for example, complex(1.0).The real and imaginary pieces of a complex number can be pulled out with the real() and image() functions, respectively.In [30]: import numpy as npIn [31]: a = np.arange(5)In [32]: a.dtypeOut[32]: dtype('int64')In [33]: a.dtype.itemsizeOut[33]: 8In [34]: aOut[34]: array([0, 1, 2, 3, 4])In [35]: a.shapeOut[35]: (5,)The shape property of the array is a tuple;in this instance,a tuple of 1 element,which holds the length in each dimension.In [36]: m = np.array([np.arange(2),np.arange(2)])In [37]: mOut[37]: array([[0, 1],       [0, 1]])In [38]: m.shapeOut[38]: (2, 2)In [39]: m[0,0]Out[39]: 0In [41]: np.float(42)Out[41]: 42.0In [43]: np.arange(7, dtype=np.uint16)Out[43]: array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)In [44]: m.dtype.itemsizeOut[44]: 8Data type objects are instances of the numpy.dtype class.Once again,arrays have a data type.The data type object can tell you the size of the data in bytes.The size in bytes is given by the itemsize property of the dtype class.typecharacter codeintegeriunsigned integerusingle precision float  fdouble precision float  dboolbcomplexDstringSunicodeUvoidvWe can pass the dtype constructor a two-character code.The first character stands for the type;the second character is a number specifying the number of bytes in the type(the numbers 2,4,and 8 correspond to floats of 16,32,and 64 bits,respectively)In [54]: np.sctypeDict.keys()Out[54]: dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 'Q', 17, 'b', 19, 20, 'byte', 'i', 23, 'cfloat', 'cdouble', 'U', 'Complex64', 'str_', 'Void0', 'H', 'float16', 'bool8', 'complex64', 'u2', 'clongdouble', 'singlecomplex', 'uint8', 'ulonglong', '?', 'short', 'Int8', 'I', 'uint16', 'L', 'Int16', 'Bytes0', 'uintc', 'float', 'half', 'f2', 'uint', 'UInt16', 'float128', 'f16', 'intp', 'm8', 'Object0', 'Bool', 14, 'object_', 'M8', 16, 'Float32', 'object0', 'int_', 'Str0', 'string_', 'd', 18, 'ushort', 'object', 'c32', 'D', 'str0', 'G', 'm', 'bool', 21, 'Timedelta64', 'longcomplex', 'Int64', 22, 'ubyte', 'longfloat', 'u1', 'intc', 'int32', 'str', 'g', 'bytes', 'uintp', 'c16', 'csingle', 'float32', 'Float16', 'uint0', 'f4', 'F', 'double', 'b1', 'UInt8', 'complex256', 'i4', 'i2', 'l', 'B', 'Complex32', 'single', 'P', 'int16', 'a', 'int64', 'i8', 'u8', 'u4', 'bytes0', 'V', 'f8', 'timedelta64', 'p', 'Float128', 'int', 'complex', 'clongfloat', 'UInt32', 'void', 'float_', 'Complex128', 'f', 'bytes_', 'q', 'datetime64', 'longdouble', 'int0', 'float64', 'unicode', 'Float64', 'longlong', 'Datetime64', 'O', 'uint32', 'long', 'unicode_', 'e', 'complex_', 'Int32', 'void0', 'uint64', 'c8', 'h', 'S', 'int8', 'M', 'UInt64', 'bool_', 'i1', 'complex128'])In [69]: t = np.dtype('int32')In [70]: t.charOut[70]: 'i'In [71]: t.strOut[71]: '<i4'In [72]: t.typeOut[72]: numpy.int32The str attribute of the dtype gives a string representation of a data type.It begins with a character representing endianness,if appropriate,then a character code,succeeded by a number corresponding to the number of bytes that each array item needs.Endianness,here,entails the way bytes are ordered inside a 32- or 64-bit word.In the big-endian order,the most significant byte is stored first,indicated by <.Out[71]: '<i4'

0 0
原创粉丝点击