熟悉 Numpy 数值类型

Numpy 数值类型

Python 支持的数值类型有 int(整型,python2 中存在 long 长整型)、float(浮点型),bool(布尔型) 和 complex(复数型),而 Numpy 支持比 Python 本身更为丰富的数值类型,细分如下:
bool:布尔类型,1 个字节,值为 True 或 False。
int:整数类型,通常为 int64 或 int32 。
intc:与 C 里的 int 相同,通常为 int32 或 int64。
intp:用于索引,通常为 int32 或 int64。
int8:字节(从 -128 到 127),(tinyint 1字节 -2 ^7 ~ 2^7-1 (-128~127))
int16:整数(从 -32768 到 32767),(smallint 2字节 -2 ^15 ~ 2^15-1 (-32768~32765))
int32:整数(从 -2147483648 到 2147483647),(int 4字节 -2 ^31~ 2^31-1 (-2147483648~2147483647))
int64:整数(从 -9223372036854775808 到 9223372036854775807),(bigint 8字节 -2 ^63 ~ 2^63-1)
uint8:无符号整数(从 0 到 255) unsigned
uint16:无符号整数(从 0 到 65535)
uint32:无符号整数(从 0 到 4294967295)
uint64:无符号整数(从 0 到 18446744073709551615)
float:float64 的简写。
float16:半精度浮点,5 位指数,10 位尾数
float32:单精度浮点,8 位指数,23 位尾数
float64:双精度浮点,11 位指数,52 位尾数
complex:complex128 的简写。
complex64:复数,由两个 32 位浮点表示。
complex128:复数,由两个 64 位浮点表示。
上面所有数值类型都被归于 dtype(data-type) 对象的实例。可以用 numpy.dtype(object, align, copy) 来指定数值类型。

指定矩阵长度,以及Numpy 数值类型:nd1 = np.ndarray(shape=(5,4,3),dtype=np.int64)

# 输出结果(每次输出结果不一样,仅做参考):
array([[[      2191594340480,       2191555581728,       2191611821472],
        [      2191611839376,       2191611821632,       2191611839664],
        [      2191611821952,       2191611839808,       2191611850880],
        [      2191611839952,       2191611851040,       2191611840096]],

       [[      2191611851120,       2191611840240,       2191611792144],
        [      2191611840384,       2191611792240,       2191611840528],
        [      2191611854984,       2191611840672,       2191611855072],
        [      2191611840816,       2191611855160,       2191611840960]],

       [[      2191611855248,       2191611841104,       2191611851520],
        [      2191611841248,       2191611851600,       2191611841392],
        [      2191611851760,       2191611841536,       2191611855512],
        [      2191611841680,       2191611855688,       2191611841824]],

       [[      2191611792528,       2191611845440,       2191611847848],
        [      2191611847904,       2191611847960,       2191611848016],
        [      2191611658760,       2191611658568,       2191611848072],
        [      2191611848128,       2191611848184,       2191611848240]],

       [[      2191611848296,       2191611848352, 1085389569451362832],
        [   9249843516014597,       2191600576192,       2191554927424],
        [ 364801534293181712,  725087305442788624, 1301550256870131984],
        [ 364796036651158800,  146376952399269904, 8462954688898138122]]],
      dtype=int64)

猜你喜欢

转载自blog.csdn.net/darkman_ex/article/details/80719358
今日推荐