Numpy学习03——数组的操作

更改形状

  1. numpy.ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
  • 注:通过修改 shap 属性来改变数组的形状。
import numpy as np

x = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8]])
print(x.shape)

x.shape = (4, 2)
print(x)
# [[1 2]
#  [3 4]
#  [5 6]
#  [7 8]]
  1. numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素。
  • flat()返回的是视图
  • 注意下面x和y的不同:
x = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12],
              [13, 14, 15, 16]])
for i in x:
    print(i, end=' ')
print('\n')
y = x.flat
print(y)
for i in y:
    print(i, end=' ')

输出:
在这里插入图片描述

  1. numpy.ndarray.flatten([order=‘C’]) 将数组的副本转换为一维数组,并返回(返回的是拷贝)。
  • a. order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序.
  • 注意和上面的print(y)打印结果有所不同。
x = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12],
              [13, 14, 15, 16]])
y = x.flatten()
print(y)
# [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
  1. numpy.ravel(a, order=‘C’) 将数组的副本转换为一维数组,并返回(返回的是视图
  • order='F’是拷贝模式,即返回的是拷贝。
  1. numpy.reshape(a, newshape, order=‘C’)
    函数当参数 newshape = -1 时,表示将数组降为一维。
x = np.random.randint(9, size=[2, 3, 2])
print(x)
print('\n')
y = np.reshape(x, [2, 2, 3])
print(y)

输出:
在这里插入图片描述

数组转置

  • numpy.transpose(a, axes=None) 或 numpy.ndarray.T
x = np.random.rand(3, 4) * 10
x = np.around(x, 2) # 保留两位小数
print(x)
y = x.T
print(y)

在这里插入图片描述

更改维度

当创建一个数组之后,还可以给它增加一个维度,这在矩阵计算中经常会用到。

  1. numpy.newaxis = None None 的别名,对索引数组很有用。
  • 很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用 newaxis 参数来增加一个维度。
print(np.newaxis)
x = np.array([1, 2, 3, 4, 5])
y = x[:, np.newaxis]
print(y)
print('\n')
y = x[np.newaxis, :]
print(y)

在这里插入图片描述

  • 可以发现x是一维,但是y是二维。
  1. numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
    axis 用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;

数组的组合

如果要将两份数据组合到一起,就需要拼接操作。

  1. numpy.concatenate((a1, a2, …), axis=0, out=None) 连接沿现有轴的数组序列(原来x,y都是一维的,拼接后的结果也是一维的)。

连接沿现有轴的数组序列(原来x,y都是一维的,拼接后的结果也是一维的)。

import numpy as np

x = np.array([1, 2, 3])
y = np.array([5, 6, 7])
z = np.concatenate([x, y], axis=0)
print(z)
# [1 2 3 5 6 7]

原来x,y都是n维的,拼接后的结果也是n维的。
注:a.reshape(x, y, z,…),将矩阵a转变成(x, y, z,…)---->一维长度x,二维长度y,三维长度z,…的矩阵。

x = np.array([1, 2, 3]).reshape(1, 3) 
y = np.array([7, 8, 9]).reshape(1, 3) 
z = np.concatenate([x, y]) 
print(z) 
# [[ 1 2 3] 
#  [ 7 8 9]] 
z = np.concatenate([x, y], axis=0) 
print(z) 
# [[ 1 2 3] 
#  [ 7 8 9]] 
z = np.concatenate([x, y], axis=1) 
print(z) 
# [[ 1 2 3 7 8 9]]
  1. numpy.stack(arrays, axis=0, out=None) 沿着新的轴加入一系列数组(stack为增加维度的拼接)。
x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)
# [[1 2 3]
#  [7 8 9]]
z = np.stack([x, y], axis=1)
print(z.shape)
# (3, 2)
print(z)
# [[1 7]
#  [2 8]
#  [3 9]]
  1. numpy.vstack(tup) )堆栈数组按垂直顺序排列(行顺序)和 numpy.hstack(tup) )堆栈数组按垂直顺序排列(列顺序)。

数组拆分

  1. numpy.split(ary, indices_or_sections, axis=0) 函数沿特定的轴将数组分割为子数组
  • 解释:
    indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
    axis:沿着哪个维度进行切向,默认为0,横向切分;为1时,纵向切分。
x = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12],
              [13, 14, 15, 16]])
y = np.split(x, [1, 3])
print(y)
y = np.split(x, [1, 3], axis=1)
print(y)

在这里插入图片描述

  • numpy.vsplit(ary, indices_or_sections) 垂直切分是把数组按照高度切分
  • numpy.hsplit(ary, indices_or_sections) 水平切分是把数组按照宽度切分

数组平铺

  1. numpy.tile(A, reps) tile 是瓷砖的意思,顾名思义,这个函数就是把数组像瓷砖一样铺展开来。

将原矩阵横向、纵向地复制。

x = np.array([[1, 2],
              [3, 4]])
y = np.tile(x, [1, 3])
print(y)
y = np.tile(x, [3, 3])
print(y)

在这里插入图片描述

  1. numpy.repeat(a, repeats, axis=None)
    a. axis=0 ,沿着y轴复制,实际上增加了行数。
    b. axis=1 ,沿着x轴复制,实际上增加了列数。
    c. repeats ,可以为一个数,也可以为一个矩阵。
    d. axis=None 时就会flatten当前矩阵,实际上就是变成了一个行向量。
x = np.array([[1, 2, 3],
              [4, 5, 6]])
y = np.repeat(x, 2)
print(y)
print('\n')
y = np.repeat(x, 2, axis=0)
print(y)
print('\n')
y = np.repeat(x, 2, axis=1)
print(y)
print('\n')
y = np.repeat(x, [3, 2], axis=0)
print(y)
print('\n')
y = np.repeat(x, [3, 2, 1], axis=1)
print(y)

在这里插入图片描述

添加和删除元素

函数描述
resize返回指定形状的新数组
append将值添加到数组末尾
insert沿指定轴将值插入到指定下标之前
delete删掉某个轴的子数组,并返回删除后的新数组
unique查找数组内的唯一元素
这里是您现有的代码: ```python def take_action(s): # 选取下一步的操作 if np.random.random() < epsilon: action = np.random.randint(n_action) else: change = [[0, -step_action], [0, step_action], [-step_action, 0], [step_action, 0]] F_actions = [] for i in range(4): next_state_action = np.array(s[0]) + np.array(change[i]) (x1, y1) = next_state_action F_next_state__actions = x1 ** 2 + y1 ** 2 F_actions.append(F_next_state__actions) action = np.argmin(F_actions) print('动作值', F_actions) print('动作:', action) return action ``` 这里有一些需要改进的地方: 1. 函数的参数 `s` 是一个状态,应该是一个列表或者数组,但是在函数中却使用了 `s[0]`,这会导致下一步的动作只考虑了状态的第一个元素。应该使用 `s` 直接进行计算。 2. 计算下一步状态的代码中应该使用 `step_action` 而不是 `step`。 3. 在计算动作值时,因为 `F_actions` 是一个列表,最小值的索引可以使用 `np.argmin` 方法来获取。 4. `print` 语句应该在函数的最后,因为函数的主要任务是返回下一步动作,而不是输出信息。 改进后的代码如下: ```python def take_action(s): if np.random.random() < epsilon: action = np.random.randint(n_action) else: change = [[0, -step_action], [0, step_action], [-step_action, 0], [step_action, 0]] F_actions = [] for i in range(4): next_state_action = np.array(s) + np.array(change[i]) (x1, y1) = next_state_action F_next_state_actions = x1 ** 2 + y1 ** 2 F_actions.append(F_next_state_actions) action = np.argmin(F_actions) print('动作:', action) print('动作值', F_actions) return action ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值