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, z =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
- 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\n2 3")
>>> np.loadtxt(c)
array([[ 0., 1.],
[ 2., 3.]])>>>>>> d = StringIO("M 21 72\nF 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\n3,0,4")
>>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
>>> x
array([ 1., 3.])
>>> y
array([ 2., 4.]) 以上为转载!
loadtxt函数的更多相关文章
- loadtxt()函数的糟心历程
原计划:导入一个csv文件,然后算出平均值 import numpy as np c=np.loadtxt('d:\python36\data.csv', delimiter=',', usecols ...
- Python3NumPy——常用函数
Python3NumPy的常用函数 1. txt文件 (1) 单位矩阵,即主对角线上的元素均为1,其余元素均为0的正方形矩阵. 在NumPy中可以用eye函数创建一个这样的二维数组,我们只需要给定一个 ...
- Python数据分析--Numpy常用函数介绍(2)
摘要:本篇我们将以分析历史股价为例,介绍怎样从文件中载入数据,以及怎样使用NumPy的基本数学和统计分析函数.学习读写文件的方法,并尝试函数式编程和NumPy线性代数运算,来学习NumPy的常用函数. ...
- Python科学计算—numpy模块总结(1)
作为一个本科学数学专业,目前研究非线性物理领域的研究僧.用什么软件进行纯科学计算好,Fortran永远是第一位的:matlab虽然很强大,可以很容易的处理大量的大矩阵,但是求解我们的模型(有时可能是几 ...
- Python中的支持向量机SVM的使用(有实例)
除了在Matlab中使用PRTools工具箱中的svm算法,Python中一样可以使用支持向量机做分类.因为Python中的sklearn也集成了SVM算法. 一.简要介绍一下sklearn Scik ...
- python科学计算_numpy_线性代数/掩码数组/内存映射数组
1. 线性代数 numpy对于多维数组的运算在默认情况下并不使用矩阵运算,进行矩阵运算可以通过matrix对象或者矩阵函数来进行: matrix对象由matrix类创建,其四则运算都默认采用矩阵运算, ...
- Numpy进阶操作
目录 1. 如何获取满足条设定件的索引 2. 如何将数据导入和导出csv文件 3. 如何保存和加载numpy对象 4. 如何按列或行拼接numpy数组 5. 如何按列对numpy数组进行排序 6. 如 ...
- Python NumPy学习总结
一.NumPy简介 其官网是:http://www.numpy.org/ NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.Num ...
- NumPy IO
NumPy IO Numpy 可以读写磁盘上的文本数据或二进制数据. NumPy 为 ndarray 对象引入了一个简单的文件格式:npy. npy 文件用于存储重建 ndarray 所需的数据.图形 ...
随机推荐
- 关于python的一些想法
我来自信息管理与信息系统专业,大一学过c语言但不太精通.学习python是为了学会这门新语言,据了解python会慢慢成为主流编程语言. 因为对绘图方面很感兴趣,希望老师能够在课上多讲一些这方面的东西 ...
- centos7 网卡识别为eth0 eth1
使网卡识别为 eth0 方法一.安装系统是,net.ifnames=0 biosdevname=0 方法二.(没有测试): 修改网卡配置文件中的 DEVICE= 参数的关于 eth0 [root@an ...
- Django Cookie和Seesion
1.cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生.cookie的工作原理是:由服务器产生内容,浏览器 ...
- 有了这个api接口工具-微信跳转其他浏览器下载app就这么简单
现在微信渠道可以说是拉新最快的渠道,因为微信具备强裂变性.但是目前微信对第三方下载链接的拦截是越来越严格了,那么想要在微信内肆无忌惮地推广链接就需要用到微信跳转浏览器的api接口,那如何获取该api接 ...
- Traceback (most recent call last): File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\_pydevd_bundle\pyd
某次编码,debug的时候突然突然突然给我报这个错: Traceback (most recent call last): File "c:\program files (x86)\mi ...
- .net Cache 需要注意的地方
CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(ti ...
- win10x64启动vs2010报错:未能加载C:\Windows\Microsoft.NET\Framework\v2.0.50727\microsoft.vsa.tlb
换了新电脑,因为是win10x64系统,可能是兼容性的问题吧. 启动vs2010,在启动画面直接报错:未能加载C:\Windows\Microsoft.NET\Framework\v2.0.50727 ...
- mysql转移数据目录后无法启动问题
最近在学习mysql,将mysql的数据目录文件路径/var/lib/mysql转移到/data/mysql,然后通过软连接方式关联. 1. ln -s /data/mysql /var/lib/my ...
- springboot指定端口的三种方式
第一配置文件中添加server.port=9090 第二在命令行中指定启动端口,比如传入参数一server. port=9000 java -jar bootsample. jar -- se ...
- CodeWarrior10 如何复制Workspace配置到另一个workspace
https://mcuoneclipse.com/2012/04/04/copy-my-workspace-settings/comment-page-1/#comment-106061