且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ndarray 属性| 学习笔记

更新时间:2022-09-06 18:15:19

开发者学堂课程【Python 科学计算库 NumPy 快速入门ndarray属性】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/605/detail/8816


ndarray属性


内容简介:

一、 ndarray 的属性

二、 ndarray 的形状

三、 ndarray 的类型

四、总结


一、ndarray 的属性

数组属性反映了数组本身固有的信息。

ndarray.shape 数组维度的元组

ndarray.ndim 数组维数

ndarray.size 数组中的元素数量

ndarray.itemsize 一个数组元素的长度(字节)

ndarray.dtype 数组元素的类型

score

array ([[80, 89, 86, 67, 79],

[78, 97, 89, 67,81],

[90,94,78,67, 74],

[91,91,90,67,69],

[76,87,75,67,86],

[70,79,84,67, 84],

[94,92,93, 67, 64] ,

[86, 85,83, 67,80]])

score.shape(8, 5)

(8, 5)

score.ndim

2

score.size

40

score.dtype

dtype ('int64')

score. itemaize

8


二、 ndarray 的形状

(1)创建不同形状的数组  

a=np.array ([[1, 2, 3], [4, 5, 6]])

b=np.array ( [1, 2, 3, 4] )  

c=np.array ([ [1, 2, 3] , [4,5,6]],[[1,2,3],[4,5,6]]])

(2)分别打印出形状

a.shape

b.shape

c.shape

(2,3)   #二维数组

(4,)     #一维数组

(2, 2, 3)    #三维数组

(3)如何理解数组的形状?

三维数组:

ndarray 属性| 学习笔记


三、 ndarray 的类型

dtype 是 numpy.dtype 类型,对于数组来说类型有

np.bool   用一个字节存储的布尔类型(True 或 False)       ‘b'

np.int8        一个字节大小,-128至127                ‘i’

np.int16        整数,-32768至32767                  ‘i2'

np.int32        整数,-231至232-1                     ‘i4'

np.int64        整数,-263至263-1                     ‘i8’

np.uint8        无符号整数,0至255                    'u'

np.uint16       无符号整数,0至65535                  'u2'

np.uint32       无符号整数,0至2**32-1               'u4'

np.uint64       无符号整数,0至2**64-1                 ‘u8’

np.float16  半精度浮点数:16位,正负号1位,                ‘f2’

指数5位,精度10位

np.float32  单精度浮点数:32位,正负号1位,

指数8位,精度23位                        ’f4'

np.float64   双精度浮点数:64位,正负号1位,

指数11位,精度52位                       ‘f8’

np.complex128   复数,分别用两个64位浮点数

表示实部和虚部                'c16'

np.object_           python 对象                        ‘O’  

np.string_                 字符串                        ’S’

np.unicode_           unicode 类型                      ’U’

创建数组的时候指定类型:

a=np.array ([ [1, 2, 3] , [4,5,6]],dtype=np.float32)

a.dtype

dtype ('float32')

arr=np.array(['python','tensorflow','scikit-learn','numpy'1,dtype=np.string_)

array( [b'python',b'tensorflow',b'scikit-learn',b'numpy'1,dtype='|S12*)

·注意:若不指定,整数默认 int64,小数默认 float64


四、总结

知道数组的基本属性,不同形状的维度表示以及数组的类型。