np.repeat() 心已赠人 2023-06-02 09:06 2阅读 0赞 **np.repeat()用于将numpy数组重复。** **numpy.repeat(a, repeats, axis=None);** 参数: axis=0,沿着y轴复制,实际上增加了行数 axis=1,沿着x轴复制,实际上增加了列数 **1. 一维数组重复3次** # 随机生成[0, 5)之间的数,形状1行4列,将此数组按y轴重复3次 import numpy as np pop = np.random.randint(0, 5, size=(1, 4)).repeat(3, axis=0) print(pop) [[1 4 0 4] [1 4 0 4] [1 4 0 4]] **2. 二维数组在第一维和第二维分别重复3次** # 二维数组在第一维和第二维分别重复3次 pop_reshape = pop.reshape(2, 6) pop_reshape_repeataxis0 = pop_reshape.repeat(3, axis=0) pop_reshape_repeataxis1 = pop_reshape.repeat(3, axis=1) print(pop_reshape) print(pop_reshape_repeataxis0) print(pop_reshape_repeataxis1 ) [[1 4 0 4 1 4] [0 4 1 4 0 4]] [[1 4 0 4 1 4] [1 4 0 4 1 4] [1 4 0 4 1 4] [0 4 1 4 0 4] [0 4 1 4 0 4] [0 4 1 4 0 4]] [[1 1 1 4 4 4 0 0 0 4 4 4 1 1 1 4 4 4] [0 0 0 4 4 4 1 1 1 4 4 4 0 0 0 4 4 4]] 来自:https://blog.csdn.net/u013555719/article/details/83855965 转载于:https://www.cnblogs.com/keye/p/11246807.html
还没有评论,来说两句吧...