美文网首页
18. numpy.reshape()使用示例

18. numpy.reshape()使用示例

作者: 十里江城 | 来源:发表于2019-11-11 10:13 被阅读0次

    对随机生成的两种数组randint, asfortranarray(r)分别进行测试:

    import numpy as np
    from numpy import random as nr
    
    r = nr.randint(0, 10, size = (4, 3))
    r1 = r.reshape((3, 4), order = 'A') # randint A与C相同 行优先 用原始数组的行排现有的行
    r2 = r.reshape((3, 4), order = 'C') # 行优先
    r3 = r.reshape((3, 4), order = 'F') # 列优先 用原始数组的列排现有的列
    r4 = r.reshape((3, 4)) # 默认行优先
    print('r : ', r)
    print('r1 : ', r1)
    print('r2 : ', r2)
    print('r3 : ', r3)  
    print('r4 : ', r4)
    
    
    r = np.asfortranarray(r)
    
    r1 = r.reshape((3, 4), order = 'A') # asfortranarray 此时A与F相同
    r2 = r.reshape((3, 4), order = 'C') # 行优先
    r3 = r.reshape((3, 4), order = 'F') # 列优先
    r4 = r.reshape((3, 4))  # 默认行优先
    print('r : ', r) # r 不变
    print('r1 : ', r1)
    print('r2 : ', r2)
    print('r3 : ', r3)  
    print('r4 : ', r4)
    

    结果如下:

    
    r :  [[1 2 7]
     [0 2 2]
     [3 9 8]
     [1 5 0]]
    r1 :  [[1 2 7 0]
     [2 2 3 9]
     [8 1 5 0]]
    r2 :  [[1 2 7 0]
     [2 2 3 9]
     [8 1 5 0]]
    r3 :  [[1 1 9 2]
     [0 2 5 8]
     [3 2 7 0]]
    r4 :  [[1 2 7 0]
     [2 2 3 9]
     [8 1 5 0]]
    r :  [[1 2 7]
     [0 2 2]
     [3 9 8]
     [1 5 0]]
    r1 :  [[1 1 9 2]
     [0 2 5 8]
     [3 2 7 0]]
    r2 :  [[1 2 7 0]
     [2 2 3 9]
     [8 1 5 0]]
    r3 :  [[1 1 9 2]
     [0 2 5 8]
     [3 2 7 0]]
    r4 :  [[1 2 7 0]
     [2 2 3 9]
     [8 1 5 0]]
    1
    ​
    

    相关文章

      网友评论

          本文标题:18. numpy.reshape()使用示例

          本文链接:https://www.haomeiwen.com/subject/bdnzbctx.html