使用 numpy 将 csv 加载到 2D 矩阵中进行绘图 [英] load csv into 2D matrix with numpy for plotting
问题描述
鉴于此 CSV 文件:
"A","B","C","D","E","F","timestamp"611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12
我只想将其加载为 3 行 7 列的矩阵/ndarray.然而,出于某种原因,我能从 numpy 中得到的只是一个 3 行(每行一个)没有列的 ndarray.
r = np.genfromtxt(fname,delimiter=',',dtype=None, names=True)打印打印 r.shape[ (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999991999)6199996(611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.2277699999.16916)316161(611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.227769999916296) 814.07514000000003) 81(3,)
我可以手动迭代并将其修改为我想要的形状,但这似乎很愚蠢.我只想将它作为一个适当的矩阵加载,这样我就可以将它切片到不同的维度并绘制它,就像在 matlab 中一样.
纯 numpy
numpy.loadtxt(open("test.csv", "rb"), delimiter=",", skiprows=1)
查看 loadtxt 文档.
你也可以使用 python 的 csv 模块:
导入csv导入 numpyreader = csv.reader(open("test.csv", "rb"), delimiter=",")x = 列表(读者)结果 = numpy.array(x).astype("float")
您必须将其转换为您喜欢的数字类型.我想你可以在一行中写出整件事:
<前>结果 = numpy.array(list(csv.reader(open("test.csv", "rb"), delimiter=","))).astype("float")添加提示:
您也可以使用 pandas.io.parsers.read_csv
并获取关联的 numpy
数组,这样可以更快.
Given this CSV file:
"A","B","C","D","E","F","timestamp"
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291111964948E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291113113366E12
611.88243,9089.5601,5133.0,864.07514,1715.37476,765.22777,1.291120650486E12
I simply want to load it as a matrix/ndarray with 3 rows and 7 columns. However, for some reason, all I can get out of numpy is an ndarray with 3 rows (one per line) and no columns.
r = np.genfromtxt(fname,delimiter=',',dtype=None, names=True)
print r
print r.shape
[ (611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291111964948.0)
(611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291113113366.0)
(611.88243, 9089.5601000000006, 5133.0, 864.07514000000003, 1715.3747599999999, 765.22776999999996, 1291120650486.0)]
(3,)
I can manually iterate and hack it into the shape I want, but this seems silly. I just want to load it as a proper matrix so I can slice it across different dimensions and plot it, just like in matlab.
Pure numpy
numpy.loadtxt(open("test.csv", "rb"), delimiter=",", skiprows=1)
Check out the loadtxt documentation.
You can also use python's csv module:
import csv
import numpy
reader = csv.reader(open("test.csv", "rb"), delimiter=",")
x = list(reader)
result = numpy.array(x).astype("float")
You will have to convert it to your favorite numeric type. I guess you can write the whole thing in one line:
result = numpy.array(list(csv.reader(open("test.csv", "rb"), delimiter=","))).astype("float")
Added Hint:
You could also use pandas.io.parsers.read_csv
and get the associated numpy
array which can be faster.
这篇关于使用 numpy 将 csv 加载到 2D 矩阵中进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!