numpy代码片段合集

生成shape为(num_examples, num_inputs),符合0-1分布的数据.
np.random.normal(0, 1, (num_examples, num_inputs))

判断两个ndarray是否近似

https://docs.scipy.org/doc/numpy/reference/generated/numpy.isclose.html

判断是否元素值都为true

Test whether all array elements along a given axis evaluate to True.

根据值从ndarray中找出index

https://thispointer.com/find-the-index-of-a-value-in-numpy-array/
主要就是用np.where

np.where


When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero().
当只有condition的时候等价于np.nonzero(condition)

#判断两个ndarray是否近似
is_same = np.isclose(out_onnx,out_tf,atol=1e-04)
# print(np.all(is_same))
print(is_same)
result = np.where(is_same == False) 
print(result) #result是一个tuple 依次为每个维度满足条件的下标
listOfCoordinates= list(zip(result[0], result[1],result[2], result[3]))
for cord in listOfCoordinates:
    # print(cord)
    print(out_onnx[cord[0]][cord[1]][cord[2]][cord[3]])
    print(out_tf[cord[0]][cord[1]][cord[2]][cord[3]])

np.pad

pad(array, pad_width, mode, **kwargs)

array——表示需要填充的数组;
pad_width——表示每个轴(axis)边缘需要填充的数值数目。
参数输入方式为:((before_1, after_1), … (before_N, after_N)),其中(before_1, after_1)表示第1轴两边缘分别填充before_1个和after_1个数值。取值为:{sequence, array_like, int}
mode——表示填充的方式(取值:str字符串或用户提供的函数),总共有11种填充模式;

参考:https://blog.csdn.net/zenghaitao0128/article/details/78713663

np.tile


对A的每个轴上的元素复制n次,n由第二个参数决定

原文地址:https://www.cnblogs.com/sdu20112013/p/12070425.html