官术网_书友最值得收藏!

The Numpy way

It is simple when we have a file with only two columns, and only one column to read, but it can get more tedious when we have an extended table containing thousands of columns and rows and we want to convert them into a Numpy matrix later.

Numpy provides a standard one-liner solution:

import numpy as np
np.loadtxt(‘evens.txt’,delimiter=’\t’,usecols=1,dtype=np.int32)

The first parameter is the path of the data file. The delimiter parameter specifies the string used to separate values, which is a tab here. Because numpy.loadtxt() by default separate values separated by any whitespace into columns by default, this argument can be omitted here. We have set it for demonstration.

For usecols and dtype that specify which columns to read and what data type each column corresponds to, you may pass a single value to each, or a sequence (such as list) for reading multiple columns.

Numpy also by default skips lines starting with #, which typically marks comment or header lines. You may change this behavior by setting the comment parameter.

主站蜘蛛池模板: 夏邑县| 新安县| 涡阳县| 方城县| 武义县| 库伦旗| 宁陕县| 绍兴市| 八宿县| 武邑县| 来凤县| 临汾市| 宜宾市| 大洼县| 万宁市| 河西区| 兴和县| 民县| 都江堰市| 清徐县| 嘉兴市| 鹤壁市| 新建县| 金堂县| 贡觉县| 彭州市| 昌邑市| 潢川县| 伊吾县| 乃东县| 井研县| 长春市| 金昌市| 合阳县| 息烽县| 安徽省| 阳泉市| 华蓥市| 三明市| 澳门| 如皋市|