zoukankan      html  css  js  c++  java
  • NumPy之ndarry数组创建

    numpy.empty方法

    用来创建一个指定形状和类型的数组,并且未初始化

    numpy.empty(shape,dtype=float,order='C')

    其中shape代表数组形状,dtype代表数据类型,order中”C“代表行优先、”F“代表列优先。

    # 创建空数组
    x=np.empty([3,2],dtype=np.int32,order='C')
    print(x)

    运行后结果:

    [[  7209029   6422625]
     [  6619244       100]
     [  6553673 911867904]]
    
    Process finished with exit code 0

    numpy.zeros方法

    numpy.zeros(shape,dtype=float,order='F')

    创建指定大小的数组,数组元素以0来填充。例使用自定义的类型创建zeros数组:

    z=np.zeros((5,),dtype=[('name','i8'),('age','i4')])
    print(z)

    numpy.ones方法

    numpy.ones方法创建一个指定形状的数组,数组元素以1填充。

    numpy.ones(shape,dtype=None,order='C')

    numpy.asarray方法

    numpy.asarray 类似 numpy.array,但 numpy.asarray 参数只有三个,比 numpy.array 少两个。

    numpy.asarray(a,dtype=None,order='C')

    其中dtype、order属性与array属性相同,参数a是指 任意形式的输入参数,可以是列表,列表元组,元组,元组的元组,元组的列表,多维数组。

    如将列表x=[1,3,4]转化为ndarry:   a=np.asarray(x)

    将元组转化为ndarray:

    xa=[(1,3,4),(2,4,2),(1,4,2,5)]
    xs=np.asarray(xa)
    print(xs)

    运行结果:

    [(1, 3, 4) (2, 4, 2) (1, 4, 2, 5)]
    
    Process finished with exit code 0

    numpy.frombuffer

    主要用于实现动态数组,numpy.frombuffer接受buffer输入参数,以流的形式读入转化成ndarray对象。

    注意:buffer 是字符串的时候,Python3 默认 str 是 Unicode 类型,所以要转成 bytestring 在原 str 前加上 b。

    numpy.frombuffer(buffer,dtype=float,count=-1,offset=0)

    buffer: 可以是任意对象,以流的形式读入、

    dtype: 数据类型

    count: 读取数据的数量,默认为-1,读取所有的数据

    offset: 读取的起始位置,默认从头开始度

    下面实现一串字符的转化

    # 实现动态数组 numpy,frombuffer
    s=b'Hellow'
    zs=np.frombuffer(s,dtype='S3');
    print(zs)

    代码中要注意的有两点:一是python3.x中要转化成bytestring需要在原str前加上b 二是dtype的类型”SN“中N的值要能够被bytestring类型字符的长度整除。

    运行结果:

    [b'Hel' b'low']

    numpy.fromiter方法

    此方法可以从可迭代对象中建立ndarray对象,返回一组数组

    numpy.fromiter(iterable,dtype,count=-1)

    iterable: 可迭代对象

    dtype:    返回数组的数据类型

    count: 读取数据的数量

    下面使用range列表函数创建列表对象再使用迭代器创建ndarray对象

    # 使用迭代器创建ndarray
    list=range(5)
    it=iter(list)
    xt=np.fromiter(it,dtype=float)
    print(xt)

    运行结果:

    [0. 1. 2. 3. 4.]
    
    Process finished with exit code 0

    numpy.arange函数

    numpy包中使用arange函数创建数值范围并返回ndarray对象,函数格式如下:

    numpy.arange(start,stop,step,dtype)

    其中 start: 起始值

           stop:终止值

           step:  步长,默认为1

           dtype:  返回ndarray的类型,默认返回输入数据的类型

    如设置一个起始值、终止值与步长:

    # 设置起始值 终止值 步长
    xc=np.arange(10,20,2)
    print(xc)

    运行结果:

    [10 12 14 16 18]
    
    Process finished with exit code 0

    numpy.linspace函数

    用于创建一个等差的一维数组

    np.linspace(start,stop,num=50,endpoint=True,rerstep=False,dtype=None)

    其中 num:要生成等步长的样本数量,默认50

           endpoint 该值为True时,数列中包含stop值,反之不包含,默认为True

           retstep   如果为True时,生成的数组会显示间距,反之不显示

    at=np.linspace(1,10,12,endpoint=True ).reshape([2,6])
    结果显示:
    [[ 1.          1.81818182  2.63636364  3.45454545  4.27272727  5.09090909]
     [ 5.90909091  6.72727273  7.54545455  8.36363636  9.18181818 10.        ]]

    numpy.logspace函数

         用来创建一个等比数列,格式如下

    np.logspace(start,stop,num=50,endpoint=True,base=10.0, dtype=None)

    其中base:指对数log的底数

  • 相关阅读:
    Idea导入tomcat源码
    SpringBoot学习 (一) Eclipse中创建新的SpringBoot项目
    Zookeeper客户端Curator使用详解
    解决老是提示找不到Mapper文件无法执行定义的方法问题!
    人工智能、机器学习、深度学习三者之间的关系
    java5增加对https的支持
    Spring静态注入的三种方式
    Thrift入门及Java实例演示【转】
    activeMQ 学习
    python中 import 和from ... import 的区别
  • 原文地址:https://www.cnblogs.com/supershuai/p/12217134.html
Copyright © 2011-2022 走看看