numpy array 数组的创建/索引/类型修改

一.用数值创建1维、2维、3维数组,3维以上依次类推。

# 用固定的值创建数组
array_dim1 = np.array([1, 2, 3, 4, 5])
array_dim2 = np.array([
    [1, 2, 3, 4, 5],
    [5, 4, 3, 2, 1]
])
array_dim3 = np.array([
    [
        [1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2]
    ],
    [
        [0, 0, 0, 0, 0],
        [4, 4, 4, 4, 4]
    ],
    [
        [3, 3, 3, 3, 3],
        [5, 5, 5, 5, 5]
    ]
])

print('\narray_dim1:\n', array_dim1, '\nshape:', array_dim1.shape)
print('\narray_dim2:\n', array_dim2, '\nshape:', array_dim2.shape)
print('\narray_dim3:\n', array_dim3, '\nshape:', array_dim3.shape)

输出结果:

array_dim1:
 [1 2 3 4 5] 
shape: (5,)

array_dim2:
 [[1 2 3 4 5]
 [5 4 3 2 1]] 
shape: (2, 5)

array_dim3:
 [[[1 1 1 1 1]
  [2 2 2 2 2]]

 [[0 0 0 0 0]
  [4 4 4 4 4]]

 [[3 3 3 3 3]
  [5 5 5 5 5]]] 
shape: (3, 2, 5)

二.创建特定类型的数组

# 创建特定类型的数组
zero_array_dim1 = np.zeros(3)                     # 一维0数组
one_array_dim2 = np.ones((4, 2))                  # 二维单位数组
empty_array_dim3 = np.empty((4, 2, 1))            # 三维空数组
rand_array_dim4 = np.random.rand(2, 3, 4, 4)      # 四维随机数组,也可以用np.random.randn()
range_array_dim3 = np.arange(12).reshape(3, 2, 2) # reshape的尺寸3*2*2必须等于arange中的数字12

print('\nzero_array_dim1:\n', zero_array_dim1, '\nshape:', zero_array_dim1.shape)
print('\none_array_dim2:\n', one_array_dim2, '\nshape:', one_array_dim2.shape)
print('\nempty_array_dim3:\n', empty_array_dim3, '\nshape:', empty_array_dim3.shape)
print('\nrand_array_dim4:\n', rand_array_dim4, '\nshape:', rand_array_dim4.shape)
print('\nrange_array_dim3:\n', range_array_dim3, '\nshape:', range_array_dim3.shape)

输出结果:

zero_array_dim1:
 [0. 0. 0.] 
shape: (3,)

one_array_dim2:
 [[1. 1.]
 [1. 1.]
 [1. 1.]
 [1. 1.]] 
shape: (4, 2)

empty_array_dim3:
 [[[0.00000000e+000]
  [0.00000000e+000]]

 [[0.00000000e+000]
  [0.00000000e+000]]

 [[0.00000000e+000]
  [2.35175247e-321]]

 [[2.45886122e+198]
  [1.48971569e+214]]] 
shape: (4, 2, 1)

rand_array_dim4:
 [[[[8.71962343e-01 9.47499422e-01 7.64976480e-01 4.15361218e-01]
   [6.80023725e-01 6.14945946e-01 6.41473216e-03 1.37538563e-01]
   [9.47332111e-01 8.37384366e-01 3.01030989e-01 2.44884136e-01]
   [3.10888205e-01 9.41520333e-01 3.84919622e-01 6.74390123e-01]]

  [[1.94107941e-02 4.37883992e-01 4.24784373e-01 3.94210111e-02]
   [8.90954421e-02 4.40022751e-01 2.19890764e-01 4.01649737e-01]
   [4.35490302e-01 6.85398042e-01 8.80543399e-01 5.93975243e-01]
   [7.90063608e-01 6.12052945e-02 3.84000035e-01 1.08048790e-01]]

  [[3.57632194e-01 5.44411423e-01 6.70843534e-01 2.87669720e-01]
   [6.33962591e-01 6.50379390e-01 3.56973621e-01 5.64596983e-02]
   [7.85136189e-01 6.59735467e-01 3.16805122e-01 1.22479365e-01]
   [5.83184531e-01 8.83987162e-01 7.38901133e-01 8.51870775e-01]]]


 [[[9.49641052e-01 3.81744378e-01 1.34296814e-01 1.17447075e-01]
   [6.89226963e-01 2.67766176e-01 1.38457294e-01 7.57906027e-01]
   [4.37812554e-01 8.47029983e-01 5.87532169e-01 9.03586286e-04]
   [1.67894557e-01 5.00059029e-01 5.44522072e-01 1.31581580e-01]]

  [[9.58449786e-01 6.33072330e-01 6.55896120e-01 8.82333944e-01]
   [7.76980085e-01 5.82268104e-01 9.73752000e-01 1.94825195e-01]
   [8.93244693e-02 9.45442541e-02 4.24203903e-01 9.04128368e-01]
   [6.97358079e-01 9.77035228e-01 2.37057901e-01 6.55376999e-01]]

  [[1.52044346e-01 6.94156270e-02 7.59458463e-02 9.31827877e-01]
   [4.92090609e-02 4.05328198e-01 8.86543569e-01 9.96587451e-01]
   [6.05645943e-01 8.79965769e-01 2.82805701e-01 4.51762062e-01]
   [3.08718335e-02 1.58919390e-01 2.83062993e-02 5.28301329e-01]]]] 
shape: (2, 3, 4, 4)

range_array_dim3:
 [[[ 0  1]
  [ 2  3]]

 [[ 4  5]
  [ 6  7]]

 [[ 8  9]
  [10 11]]] 
shape: (3, 2, 2)

三.查看数组的关键属性

属性

描述

C_CONTIGUOUS (C)

数据是在一个单一的C风格的连续段中

F_CONTIGUOUS (F)

数据是在一个单一的Fortran风格的连续段中

OWNDATA (O)

数组拥有它所使用的内存或从另一个对象中借用它

WRITEABLE (W)

数据区域可以被写入,将该值设置为 False,则数据为只读

ALIGNED (A)

数据和所有元素都适当地对齐到硬件上

UPDATEIFCOPY (U)

这个数组是其它数组的一个副本,当这个数组被释放时,原数组的内容将被更新

# np.array的关键属性
new_array = np.random.rand(2, 1, 3, 3)
print('\nnew_array:\n', new_array)
print('dims=', new_array.ndim, '| shape=', new_array.shape, '| size=', new_array.size,
      '| dtype=', new_array.dtype, '| itemSize=', new_array.itemsize)

输出结果:

new_array:
 [[[[0.79477963 0.9791731  0.84296204]
   [0.81459028 0.41071809 0.60891876]
   [0.5550956  0.70852454 0.50363746]]]


 [[[0.13276552 0.84312079 0.68343545]
   [0.52590361 0.05245756 0.10091942]
   [0.15484995 0.04238362 0.72579296]]]]
dims= 4 | shape= (2, 1, 3, 3) | size= 18 | dtype= float64 | itemSize= 8

四.索引数组中的子数组或数值

# 索引
new_array = np.random.rand(2, 1, 3, 3)
print('\nnew_array:\n', new_array)
sub_array_01 = new_array[0]
print('\nsub_array_01:\n', sub_array_01, '\nshape:', sub_array_01.shape)
sub_array_02 = new_array[0, 0, 1]
print('\nsub_array_02:\n', sub_array_02, '\nshape:', sub_array_02.shape)

输出结果:

new_array:
 [[[[0.79536423 0.08368581 0.77069393]
   [0.46756076 0.00215098 0.34791715]
   [0.03074363 0.31827965 0.89997761]]]


 [[[0.08272213 0.16338877 0.27321205]
   [0.06699474 0.8887422  0.23668874]
   [0.14746627 0.77472828 0.44872595]]]]

sub_array_01:
 [[[0.79536423 0.08368581 0.77069393]
  [0.46756076 0.00215098 0.34791715]
  [0.03074363 0.31827965 0.89997761]]] 
shape: (1, 3, 3)

sub_array_02:
 [0.46756076 0.00215098 0.34791715] 
shape: (3,)

五.改变数组的数据类型

# 改变元素的数据类型
# 直接修改dtype会改变数组长度,这种方法不对
print('Error Version')
new_array = np.random.rand(2, 1, 3, 3)
print('Before change: new_array dtype=', new_array.dtype, ' shape=', new_array.shape)
new_array.dtype = 'float16'
print('After change: new_array dtype=', new_array.dtype, ' shape=', new_array.shape)
new_array.dtype = 'int'
print('After change: new_array dtype=', new_array.dtype, ' shape=', new_array.shape)

# 用astype修改则不会改变数组长度
print('Correct Version')
new_array = np.random.rand(2, 1, 3, 3)
print('Before change: new_array dtype=', new_array.dtype, ' shape=', new_array.shape)
new_array.astype('float32')
print('After change: new_array dtype=', new_array.dtype, ' shape=', new_array.shape)
new_array.astype('int')
print('After change: new_array dtype=', new_array.dtype, ' shape=', new_array.shape)

输出结果:

Error Version
Before change: new_array dtype= float64  shape= (2, 1, 3, 3)
After change: new_array dtype= float16  shape= (2, 1, 3, 12)
After change: new_array dtype= int32  shape= (2, 1, 3, 6)
Correct Version
Before change: new_array dtype= float64  shape= (2, 1, 3, 3)
After change: new_array dtype= float64  shape= (2, 1, 3, 3)
After change: new_array dtype= float64  shape= (2, 1, 3, 3)

六.其他(待总结)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的NumPy库是用于科学计算的重要工具,其中包含了处理多维数组的功能。可以使用NumPy创建和操作数组,例如通过np.array()函数传入可迭代对象来创建数组。可以使用该函数传入一个列表或元组等作为参数,如np.array([0, 1, 2, 3, 4])可以创建一个包含这些元素的一维数组,而np.array([[11, 12, 13],[21, 22, 23]])则可以创建一个2*3的二维数组。还可以通过指定数据类型,例如np.array([0, 1, 2, 3, 4], dtype=float)可以创建一个包含浮点数的数组。 此外,NumPy还提供了许多数学函数来操作数组。例如,可以使用np.sin()函数对一维数组中的所有元素进行求正弦,使用np.cos()函数求二维数组中的所有元素的余弦。可以使用np.round()函数进行四舍五入,np.ceil()函数向上取整,等等。 总之,Python中的NumPy库提供了丰富的功能来创建、操作和处理数组,使得在科学计算和数据分析中更加方便和高效。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python中找出numpy array数组的最及其索引方法](https://download.csdn.net/download/weixin_38640830/12869491)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Python数据分析之numpy数组全解析](https://blog.csdn.net/weixin_30851409/article/details/101717473)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [【Python】numpy——数组array](https://blog.csdn.net/fftx_00/article/details/122265623)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值