增加维度:
np.expand_dims(distance, 0)
np.expand_dims(distance, 1)
np.expand_dims(distance, 2)
求平均值/最大/最小值:
np.mean()/ np.max()/ np.min()
相邻元素差分
arc_diff = np.diff(arc_hour)
判断数组是否全为某个值
is_constant = np.all(arc_diff == 1)
判断数组是否含有某个值
is_constant = np.any(arc_diff == 1)
随机返回一个整数:
numpy.random.randint(low, high)
# 范围从低(包括)到高(不包括),即[low, high)。如果没有写参数high的值,则返回[0,low)的值。
exmple:
np.random.randint(2,10) # 输出 9
新建空数组动态添加
train_input = np. empty([0, seq_length, 6], dtype=float)
train_input = np.append(train_input, curr_train_input, axis=0)
二值化数组
np.where(a>threshold, upper, lower)
>>> a = np.random.rand(4, 4)
>>> a
array([[0.40619068, 0.5697802 , 0.62462315, 0.72661899],
[0.09580956, 0.86708781, 0.82974038, 0.38233455],
[0.69538282, 0.48197051, 0.48655537, 0.5986295 ],
[0.44688326, 0.41067639, 0.79687831, 0.37464955]])
>>> threshold, upper, lower = 0.5, 1, 0
>>> b = np.where(a>threshold, upper, lower)
>>> b
array([[0, 1, 1, 1],
[0, 1, 1, 0],
[1, 0, 0, 1],
[0, 0, 1, 0]])