zoukankan      html  css  js  c++  java
  • Numpy基础之创建与属性

    import numpy as np
    
    '''
    1.数组的创建:np.array([])
    2.数组对象的类型:type()
    3.数据类型:a.dtype
    4.数组的型shape:(4,2,3)
    5.定义数组的每个元素的字节: array.itemsize
    '''
    
    ##   创建一个三维数组
    a = [[1,2,1],[1,3,4]]
    b = [[5,6,1],[1,7,8]]
    c = [[9,10,1],[11,12,1]]
    d = [[1,13,14],[1,15,16]]
    
    array_test = np.array([a,b,c,d],dtype='float64')
    
    print(array_test.shape)
    print(array_test.ndim)
    print(array_test.size)
    print(array_test)
    
    '''
    (4, 2, 3)
    3
    24
    解释:
    型    shape:(4, 2, 3)   
    轴    axes:数组的维称为轴,轴的数量称作秩    这里是三维数组:shape有三个整数
    数组长度 size:(分级元素的总个数) 24【=4*2*3】
    
    同质:一级元素4个【其中每个类型都一样,如a】,二级元素2个【a的一级元素,每个类型都一样,如[1,2,1]】,三级元素3个【[1,2,3]的一级元素】
    因此,shape是(4,2,3)
    
    '''
    print(type(array_test))
    '''
    数组类:<class 'numpy.ndarray'>
    '''
    print(array_test.dtype)
    '''
    数据类型:int32
    '''
    
    print(array_test.itemsize)  ## 4
    
    ## 创建等差数组
    arange_array = np.arange(0,12).reshape(3,4)
    linspace_array = np.linspace(0,10,5).reshape(5,1)
    print('轴数',arange_array.ndim)
    print('size:',linspace_array.size)
    print(arange_array)
    print(linspace_array)
    '''
    轴数 2
    size: 5
    
    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]
    [[ 0. ]
     [ 2.5]
     [ 5. ]
     [ 7.5]
     [10. ]]
    '''
    
    ##  创建随机数列
    # 一维随机数列
    rand_1d = np.random.random(4)
    print(rand_1d)
    '''
    [0.08525778 0.12143347 0.56587575 0.83590871]
    '''
    # 将一维改成2维
    print(rand_1d.reshape(2,2))
    '''
    [[0.08525778 0.12143347]
     [0.56587575 0.83590871]]
    '''
    
    ##  直接生成多维数组 传入shape即可
    
    print(np.random.random((3,3)))
    '''
    [[0.56859463 0.98880884 0.52755145]
     [0.26863131 0.22285108 0.71508455]
     [0.31286731 0.2290022  0.7223287 ]]
    '''
  • 相关阅读:
    header
    panel----单个基础版
    vue-demo
    js不同类型变量比较
    reset.css
    关于各个浏览器的兼容问题
    git
    AMD与CMD区别
    喜欢前端的看过来哦
    js中数组去重的几种方法
  • 原文地址:https://www.cnblogs.com/liuhuacai/p/11578650.html
Copyright © 2011-2022 走看看