首页 > 编程语言 > 详细

二、Python开发---27、numpy(2)

时间:2020-02-23 12:23:53      阅读:50      评论:0      收藏:0      [点我收藏+]

ndarray对象属性

  ndim 数组轴(维度)的个数,轴的个数被称作秩

import numpy as np
a = np.arange(24)
print(a)        #输出为 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
print(a.ndim)   #输出为 1
b = a.reshape(2,4,3)
print(b)        #输出为 [[[ 0  1  2]
                #        [ 3  4  5]
                #        [ 6  7  8]
                #        [ 9 10 11]]
                #       [[12 13 14]
                #        [15 16 17]
                #        [18 19 20]
                #        [21 22 23]]]
print(b.ndim)   #输出为 3
print(b.shape)  #输出为 (2, 4, 3)

  shape 数组的维度,例如一个2排3列的矩阵,它的shape属性将是(2,3)这个元组的长度显然是秩,即维度或者ndim属性;对于一个已经存在的ndarray数组对象而言,可以通过修改形状相关的参数/方法从而改变数组的形状,直接修改数组ndarray的shape值,,要求修改后乘积不变,直接使用reshape函数创建一个改变尺寸的新数组,原数组的shape保持不变,但是新数组和原数组共享一个内存空间,也就是修改任何一个数组中的值都会对另外一个产生影响,另外要求新数组的元素个数和原数组一致,当指定某一个轴为-1的时候,表示将根据数组元素的数量自动计算该轴的长度值

‘‘‘
输出为 [[1 2 3]
        [4 5 6]]
      (2, 3)
      [[1 2]
       [3 4]
       [5 6]]
‘‘‘
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a)
print(a.shape)
#调整数组大小
a = np.array([[1,2,3],[4,5,6]])
a.shape=(1,6)   #输出为 [[1 2 3 4 5 6]]
print(a)
# reshape 调整数组大小
b = a.reshape(3,2)
print(b)        #输出为  [[1 2]
#                        [3 4]
#                        [5 6]]

  size 数组元素的总个数,等于shape属性中元组元素的乘积

  dtype 一个用来描述数组中元素类型的对象,可以通过创造或指定dtype使用标准Python类型,不过NumPy提供它自己的数据类型

  itemsize 数组中每个元素的字节大小,例如一个元素类型为float64的数组itemsize属性值为8(=64/8),又如一个元素类型为complex32的数组item属性为4(=32/8)

二、Python开发---27、numpy(2)

原文:https://www.cnblogs.com/lanzhijie/p/12348608.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!