load csv into 2D matrix with numpy for plotting

Given this CSV file:


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)]

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


Check out the loadtxt documentation.

You can also use python's csv module:

import csv
import numpy

You will have to convert it to your favorite numeric type. I guess you can write the whole thing in one line:


Added Hint:

You could also use pandas.io.parsers.read_csv and get the associated numpy array which can be faster.

Need Your Help

Are there any yuy2 splitter and decoder?


Are there are any free yuy2 splitter and/or decoder filters? I coulnd't find any on the web.

Confusion about nested routes in Rails 3, specifically autogenerated paths

ruby-on-rails ruby-on-rails-3

For each species, I have many datasets. For each dataset, I have many phenotypes. The datasets have names which, within a species, are primary keys. The species, too, have string primary keys (e.g....

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.