- Matplotlib 2.x By Example
- Allen Yu Claire Chung Aldrin Yim
- 185字
- 2021-07-02 19:34:33
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.
- SPSS數(shù)據(jù)挖掘與案例分析應用實踐
- Java程序設計與開發(fā)
- 兩周自制腳本語言
- arc42 by Example
- 零基礎學Scratch少兒編程:小學課本中的Scratch創(chuàng)意編程
- 機器人Python青少年編程開發(fā)實例
- Expert Data Visualization
- Linux C編程:一站式學習
- Apache Camel Developer's Cookbook
- Android應用開發(fā)實戰(zhàn)
- Everyday Data Structures
- 多媒體技術及應用
- Learning Shiny
- 關系數(shù)據(jù)庫與SQL Server 2012(第3版)
- Web開發(fā)新體驗