Python中asarray用法

来源:互联网 发布:安知玉如意江云儿 编辑:程序博客网 时间:2024/06/05 08:34

通过在编译环境中输入

import numpyhelp(numpy.asarray)

Help on function asarray in module numpy.core.numeric:


asarray(a, dtype=None, order=None)
    Convert the input to an array.
    
    Parameters
    ----------
    a : array_like
        Input data, in any form that can be converted to an array.  This
        includes lists, lists of tuples, tuples, tuples of tuples, tuples
        of lists and ndarrays.
    dtype : data-type, optional
        By default, the data-type is inferred from the input data.
    order : {'C', 'F'}, optional
        Whether to use row-major ('C') or column-major ('F' for FORTRAN)
        memory representation.  Defaults to 'C'.

举例:

Convert a list into an array:将列表转换为数组

>>> a = [1,2]>>> numpy.asarray(a)array([1, 2])


将数据类型转换为float和int

>>> a= [1,2]>>> numpy.asarray(a,'f')array([ 1.,  2.], dtype=float32)>>> numpy.asarray(a,'i')array([1, 2])

判断a中数是否大于0,如果大于0,则将该数置为1,

>>> a = [[1,2],[1,0]]>>> a = numpy.asarray(a) #必须先转换为array,否则出现array(1.0, dtype=float32)>>> numpy.asarray(a>0,'i') #'i'表示为dtype类型为intarray([[1, 1],       [1, 0]])>>> numpy.asarray(a>0,'f')array([[ 1.,  1.],       [ 1.,  0.]], dtype=float32)



1 0
原创粉丝点击