zoukankan      html  css  js  c++  java
  • loadtxt函数

    numpy.loadtxt

    numpy.loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)[source]

    Load data from a text file.

    Each row in the text file must have the same number of values.

    Parameters:

    fname : file or str

    File, filename, or generator to read. If the filename extension is .gz or .bz2, the file is first decompressed. Note that generators should return byte strings for Python 3k.

    dtype : data-type, optional

    Data-type of the resulting array; default: float. If this is a structured data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type.

    comments : str or sequence, optional

    The characters or list of characters used to indicate the start of a comment; default: ‘#’.

    delimiter : str, optional

    The string used to separate values. By default, this is any whitespace.

    converters : dict, optional

    A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: converters {0: datestr2num}. Converters can also be used to provide a default value for missing data (but see also genfromtxt): converters {3: lambda s:float(s.strip() or 0)}. Default: None.

    skiprows : int, optional

    Skip the first skiprows lines; default: 0.

    usecols : sequence, optional

    Which columns to read, with 0 being the first. For example, usecols (1,4,5) will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read.

    unpack : bool, optional

    If True, the returned array is transposed, so that arguments may be unpacked using x, y, =loadtxt(...). When used with a structured data-type, arrays are returned for each field. Default is False.

    ndmin : int, optional

    The returned array will have at least ndmin dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2.

    New in version 1.6.0.

    Returns:

    out : ndarray

    Data read from the text file.

    See also

    loadfromstringfromregex

    genfromtxt
    Load data with missing values handled as specified.
    scipy.io.loadmat
    reads MATLAB data files

    Notes

    This function aims to be a fast reader for simply formatted files. The genfromtxt function provides more sophisticated handling of, e.g., lines with missing values.

    New in version 1.10.0.

    The strings produced by the Python float.hex method can be used as input for floats.

    Examples

    >>>
    >>> from io import StringIO   # StringIO behaves like a file object
    >>> c = StringIO("0 1
    2 3")
    >>> np.loadtxt(c)
    array([[ 0.,  1.],
           [ 2.,  3.]])
    
    >>>
    >>> d = StringIO("M 21 72
    F 35 58")
    >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
    ...                      'formats': ('S1', 'i4', 'f4')})
    array([('M', 21, 72.0), ('F', 35, 58.0)],
          dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])
    
    >>>
    >>> c = StringIO("1,0,2
    3,0,4")
    >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
    >>> x
    array([ 1.,  3.])
    >>> y
    array([ 2.,  4.])

    以上为转载!
  • 相关阅读:
    Python高级特性-迭代
    Python列表生成式测试
    Python函数基础学习(定义、函数参数、递归函数)
    Html介绍,认识head标签
    Html介绍,认识html文件基本结构
    Html介绍,标签的语法
    Html介绍,认识html标签
    Html介绍,了解html与css关系
    Html介绍,如何用代码展示我制作的第一个网页?
    C++走向远洋——38(用对象数组操作长方柱类)
  • 原文地址:https://www.cnblogs.com/master-road/p/10451169.html
Copyright © 2011-2022 走看看