numpy.loadtxt用法
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]
参考文献:
numpy.loadtxt用法的更多相关文章
- 数据分析-numpy的用法
一.jupyter notebook 两种安装和启动的方式: 第一种方式: 命令行安装:pip install jupyter 启动:cmd 中输入 jupyter notebook 缺点:必须手动去 ...
- python numpy loadtxt
用numpy加载csv文件数据 发现python numpy loadtxt 方法和数据的结构有很大关系当我的数据有第一行文字是这样子的时候 我程序的运行结果永远都报错,编码格式也处理了统一utf-8 ...
- numpy常用用法总结
numpy 简介 numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab. 官方文档(https://docs.scipy.org/doc/numpy-dev/user/quick ...
- 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 ...
- numpy.loadtxt()
简介 np.loadtxt()用于从文本加载数据. 文本文件中的每一行必须含有相同的数据. loadtxt(fname, dtype=<class 'float'>, comments=' ...
- numpy.where() 用法详解
numpy.where (condition[, x, y]) numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(condition),输出 ...
- Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法
1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引 ...
- numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法
1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...
- python3 numpy基本用法归纳总结
安装numpy : pip install numpy numpy数组生成方法总结 In [4]: import numpy as np #使用列表生成一个一维数组 data = [1,2,3,4,5 ...
随机推荐
- 我所知道的几种display:table-cell的应用
.outer span { display: table-cell; } 一.display:table-cell属性简述 display:table-cell属性指让标签元素以表格单元格的形式呈现 ...
- 洛谷P1126 机器人搬重物【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1126 题意: 给定一个n*m的方格,机器人推着直径是1.6的球在格子的线上运动. 每一秒钟可以向左转,向右转 ...
- hdu3613 Best Reward【Manacher】
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- synchronized 同步代码块,售票问题
package cn.ljs.FristSync; public class SalerDemo extends Thread { static int tickets = 1000; String ...
- algebraically closed field 代数闭域
algebraically closed field https://en.wikipedia.org/wiki/Algebraically_closed_field As an example, ...
- composer命令详解
composer命令行 你已经学会了如何使用命令行界面做一些事情.本章将向你介绍所有可用的命令. 为了从命令行获得帮助信息,请运行composer或者composer list 命令,然后结合--he ...
- Adobe Flex初记
公司项目要用Flex,之前没有接触过,菜鸟只好白手起家,把项目拉下来的同时配置下Flex的环境,以下是一篇参考: http://blog.sina.com.cn/s/blog_4c4a24db0100 ...
- 树和二叉树->遍历
文字描述 二叉树的先根遍历 若二叉树为空,则空操纵,否则 (1) 访问根结点 (2) 先根遍历左子树 (3) 先根遍历右子树 二叉树的中根遍历 若二叉树为空,则空操纵,否则 (1) 中根遍历左子树 ( ...
- PSU/OPATCH/OJVM下载页面及安装方式(最实用版)
中文版:数据库 PSU,SPU(CPU),Bundle Patches 和 Patchsets 补丁号码快速参考 (文档 ID 1922396.1) Download Reference for Or ...
- 20165336 2017-2018-2《Java程序设计》课程总结
每周作业链接汇总 我期望的师生关系:对师生关系的看法 学习基础和C语言基础调查:关于学JAVA与C的调查 Linux安装及学习:Linux的安装 第一周学习总结:认识学习JAVA 第二周学习总结:JA ...