Numpy之repeat、tile用法总结

 

# -*- coding: utf-8 -*-
"""
Created on Sun May  3 08:32:25 2020

@author: ZZL
"""

import numpy as np
 
arr = np.arange(12).reshape(1,4,3)
 
arr

#array([[[ 0,  1,  2],
#        [ 3,  4,  5],
#        [ 6,  7,  8],
#        [ 9, 10, 11]]])


# ①repeats为整数N,axis=None:数组arr首先被扁平化,然后将数组arr中的各个元素依次重复N次

#In [4]: arr.repeat(2)
#Out[4]:
#array([ 0,  0,  1,  1,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  7,  8,
#        8,  9,  9, 10, 10, 11, 11])

# ②repeats为整数数组rp_arr,axis=None:数组arr首先被扁平化,然后再将数组arr中元素依次重复对应rp_arr数组中元素对应次数。
#若rp_arr为一个值的一维数组,则数组arr中各个元素重复相同次数,否则rp_arr数组长度必须和数组arr的长度相等,否则报错
#a:rp_arr为单值一维数组,进行广播

#In [5]: arr.repeat([2])
#Out[5]:
#array([ 0,  0,  1,  1,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  7,  8,
#        8,  9,  9, 10, 10, 11, 11])


# ③repeats为整数N,axis=M:数组arr的轴M上的每个元素重复N次,M=-1代表最后一条轴

#In [9]: arr.repeat(2,axis=0)
#Out[9]:
#array([[[ 0,  1,  2],
#        [ 3,  4,  5],
#        [ 6,  7,  8],
#        [ 9, 10, 11]],
# 
#       [[ 0,  1,  2],
#        [ 3,  4,  5],
#        [ 6,  7,  8],
#        [ 9, 10, 11]]])

https://blog.csdn.net/kancy110/article/details/70980127?ops_request_misc=&request_id=&biz_id=102&utm_source=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1

你可能感兴趣的:(numpy)