not allowed to change a complex number into a integer
not allowed to change a complex number into a floating-point number
you 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 np
In [31]: a = np.arange(5)
In [32]: a.dtype
Out[32]: dtype('int64')
In [33]: a.dtype.itemsize
Out[33]: 8
In [34]: a
Out[34]: array([0, 1, 2, 3, 4])
In [35]: a.shape
Out[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]: m
Out[37]:
array([[0, 1],
[0, 1]])
In [38]: m.shape
Out[38]: (2, 2)
In [39]: m[0,0]
Out[39]: 0
In [41]: np.float(42)
Out[41]: 42.0
In [43]: np.arange(7, dtype=np.uint16)
Out[43]: array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)
In [44]: m.dtype.itemsize
Out[44]: 8
Data 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.
type character code
integer i
unsigned integer u
single precision float f
double precision float d
bool b
complex D
string S
unicode U
void v
We 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.char
Out[70]: 'i'
In [71]: t.str
Out[71]: '<i4'
In [72]: t.type
Out[72]: numpy.int32
The 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'
numPy 学习1
最新推荐文章于 2023-11-04 16:39:12 发布