1、产生样例数据的一些例子
np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random
np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
2、传入的一个list,经过np.array或者转tf ,函数作用于每一个元素上,形成与输入相同的矩阵
def test_list_func(i):
return i**2
if __name__ == '__main__':
a = [1, 2, 3, 4, 5]
res = test_list_func(np.array(a)[:,np.newaxis])
print("res:", res, res.shape)
a = tf.constant([1, 2, 3, 4, 5])
a = a[:, tf.newaxis]
res = test_list_func(a)
print(res)
本文介绍了如何使用numpy的randint函数生成随机数组,并展示了如何将列表转换为numpy数组或TensorFlow张量,同时进行元素级操作。通过实例演示了test_list_func函数在处理数组时的功能。
18万+

被折叠的 条评论
为什么被折叠?



