zoukankan      html  css  js  c++  java
  • PYTHON-数组知识

    1.shape

    #1.shape
    #一维数组
    a = [1,2,3,4,5,6,7,8,9,10,11,12] 
    b = np.array(a)
    
    print(b.shape[0])#最外层有12个元素
    #print(b.shape[1])#次外层,#IndexError: tuple index out of range
    
    #为什么不直接 a.shape[0],因为 'list' object has no attribute 'shape'
    
    #二维数组
    a = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    b = np.array(a)
    print(b)
    print(b.shape[0],b.shape[1])#最外层3个,里边4个
    #output:
    12 [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] 3 4
    
    

    2.reshape

    #2.reshape
    a = [1,2,3,4,5,6,7,8,9,10,11,12]
    b = np.array(a).reshape(2,6) #2行6列
    print(b)
    print(a)
    b = np.array(a).reshape(2,3,2) #2行3列的两个矩阵
    print(b)
    print(np.array(a))
    
    #reshape新生成数组和原数组公用一个内存,不管改变哪个都会互相影响。
    #输出:
    [[ 1  2  3  4  5  6]
     [ 7  8  9 10 11 12]]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    [[[ 1  2]
      [ 3  4]
      [ 5  6]]
    
     [[ 7  8]
      [ 9 10]
      [11 12]]]
    [ 1  2  3  4  5  6  7  8  9 10 11 12]
    #3.reshape(-1,1) 解释为:-1行 == 没有行;1 == 1列,那么这个就是1个列向量
    a = [1,2,3,4,5,6,7,8,9,10,11,12]
    b = np.array(a).reshape(-1,1) #12 * 1
    print(b)
    
    a = [1,2,3,4,5,6,7,8,9,10,11,12] 
    b = np.array(a).reshape(-1,2) # 6*2
    print(b)
    
    a = [1,2,3,4,5,6,7,8,9,10,11,12]
    b = np.array(a).reshape(1,-1) #1 * 12
    print(b)
    
    a = [1,2,3,4,5,6,7,8,9,10,11,12]
    b = np.array(a).reshape(2,-1) #2 *6
    print(b)
    #结果:
    [[ 1]
     [ 2]
     [ 3]
     [ 4]
     [ 5]
     [ 6]
     [ 7]
     [ 8]
     [ 9]
     [10]
     [11]
     [12]]
    [[ 1  2]
     [ 3  4]
     [ 5  6]
     [ 7  8]
     [ 9 10]
     [11 12]]
    [[ 1  2  3  4  5  6  7  8  9 10 11 12]]
    [[ 1  2  3  4  5  6]
     [ 7  8  9 10 11 12]]
    >>> 

    3.学这个的时候好像参考了那个网址,有些不记得了,如果有侵犯到您的著作权,立删!

  • 相关阅读:
    关于jpa example使用
    文件下载
    文件夹下的文件根据最后修改时间排序
    前端验证图片是否加载成功
    LocalDate获取当天,本月第一天,本月最后一天,今年第一天,今年最后一天
    将word文档合成一张图片输出
    easyui前端分页与layui前端分页
    Java线程池源码流程图
    hexo发布到gitee和github上及主题优化
    【JVM之美】垃圾收集算法
  • 原文地址:https://www.cnblogs.com/xiao-yu-/p/12727899.html
Copyright © 2011-2022 走看看