通过在编译环境中输入

import numpy
help(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类型为int
array([[1, 1],
       [1, 0]])
>>> numpy.asarray(a>0,'f')
array([[ 1.,  1.],
       [ 1.,  0.]], dtype=float32)



Logo

聚焦前沿AI与大模型技术探索,汇聚开发者及爱好者,共享开源项目、学习资源与行业资讯。

更多推荐