numpy.loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)

  • frame要读取的文件、文件名或生成器;
  • dtype数据类型,默认float;
  • comment的是指, 如果行的开头为#就会跳过该行;
  • delimiter分隔符,默认是空格;
  • converters是对数据进行预处理的参数, 我们可以先定义一个函数, 这里的converters是一个字典, 表示第零列使用函数add_one来进行预处理;
  • skiprows是指跳过前1行, 如果设置skiprows=2, 就会跳过前两行,默认读取所有行;
  • usecols是指只使用0,2两列;
  • unpack如果是True会把每一列当成一个向量输出, 而不是合并在一起;

举例:

def add_one(x):
return int(x)+1#注意到这里使用的字符的数据结构
(a, b) = np.loadtxt('test.txt', dtype=int, skiprows=1, converters={0:add_one}, comments='#', delimiter=',', usecols=(0, 2), unpack=True)
print(a, b)

输入:

%%writefile test.txt # 这是用来写入文件的代码
A B C
1 2 3
4 5 6
7 8 9

输出:

[2 5 8] [3 6 9]

参考文献:

【1】numpy中loadtxt 的用法

numpy.loadtxt用法的更多相关文章

  1. 数据分析-numpy的用法

    一.jupyter notebook 两种安装和启动的方式: 第一种方式: 命令行安装:pip install jupyter 启动:cmd 中输入 jupyter notebook 缺点:必须手动去 ...

  2. python numpy loadtxt

    用numpy加载csv文件数据 发现python numpy loadtxt 方法和数据的结构有很大关系当我的数据有第一行文字是这样子的时候 我程序的运行结果永远都报错,编码格式也处理了统一utf-8 ...

  3. numpy常用用法总结

    numpy 简介 numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab. 官方文档(https://docs.scipy.org/doc/numpy-dev/user/quick ...

  4. 1.numpy的用法

    numpy创建ndarray对象的三种方法 1.1.list转化 In [8]: import numpy as np In [9]: a = [1,2,3,4] In [10]: x1 = np.a ...

  5. numpy.loadtxt()

    简介 np.loadtxt()用于从文本加载数据. 文本文件中的每一行必须含有相同的数据. loadtxt(fname, dtype=<class 'float'>, comments=' ...

  6. numpy.where() 用法详解

    numpy.where (condition[, x, y]) numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(condition),输出 ...

  7. Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法

    1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引 ...

  8. numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法

    1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...

  9. python3 numpy基本用法归纳总结

    安装numpy : pip install numpy numpy数组生成方法总结 In [4]: import numpy as np #使用列表生成一个一维数组 data = [1,2,3,4,5 ...

随机推荐

  1. Vue----目录结构

    目录结构: (1):build:---------------------------------------------------------------------------------:保存 ...

  2. Glufster挂载失败Mount failed. Please check the log file for more details解决办法

    设置两台glusterfs服务器主机名分别为gfs1,gfs2 设置好glusterfs挂载不成功提示如下 Mount failed. Please check the log file for mo ...

  3. php安全

    1.会话安全性 会话固化 一种获取有效回话标识符的方法,他将运行恶意用户通过强制使用回话ID来轻松模拟一个真实用户 攻击方法:<a href="http://a.com/index.p ...

  4. hash_map

    点开一道第是自己oj的第440大关,想a了,一直想却无果,学长一句点醒,开始写hash. 关于这道题呢很无语了,两天卡在这上面,而且有些dalao不到20min就a了.我太菜了. 所以要深入讨论这道题 ...

  5. python运算符,数据类型,数据类型操作,三目运算,深浅拷贝

    算数运算符: Py2中精确除法需要导入:from __future__ import division,(符由特  ,将来的.滴未省,除法) py3不需要导入 赋值运算符: 比较运算符: 成员运算符: ...

  6. LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告

    题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  7. SQL函数汇总大全

    聚合函数对一组值计算后返回单个值.除了count(统计项数)函数以外,其他的聚合函数在计算式都会忽略空值(null).所有的聚合函数均为确定性函数.即任何时候使用一组相同的输入值调用聚合函数执行后的返 ...

  8. 页面调用qq

    tencent://message/?uin=516999605&Site=&Menu=yes

  9. UITextView设置placeholder

    下面是我的代码,可以直接拿来用 #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property(nonat ...

  10. 20180426 Linq to excel

    PM> Install-Package LinqToExcel 引用好像可以直接把excel 导入得到实体对象,  var excelfile = new ExcelQueryFactory(& ...