top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: How to use the method loadtxt() of numpy neatly?

+4 votes
1,065 views

When I try to use numpy to deal with my dataset in the style of csv, I face a problem.

In my dataset of the csv file, some columns are string that can not convert to float easily. Some of them can ignore, but other columns I need to change the data to a enum style.

for example, one column just contain three kinds : S,Q,C. Each of them can declare one meaning, so I must convert them to a dict just like {1,2,3}

Now the question is, when I use numpy.loadtxt, I must do all things above in just one line and one function.

posted Dec 20, 2013 by Seema Siddique

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Basically numpy.loadtxt() method of Python used for loading big text file . Its not very efficient to use this method.
You can use genfromtxt method loke below:

import numpy as np
print np.genfromtxt('test.txt',dtype='str')

if test.txt has data:
very good
be happy

Output:
[['very' 'good'], ['be' 'happy']]

answer Jan 8, 2014 by Amit Kumar Pandey
Similar Questions
+2 votes

It is difficult to install numpy package for my PC Windows 7, 64-bit OS. In the end, I install Enthought Canopy, which is recommended on line because it does install numpy automatically. Now, I can test it with

import numpy

it succeeds. On http://wiki.scipy.org/Cookbook, it shows some interesting code example snippet, such as Cookbook / ParticleFilter, Markov chain etc.

I don't know how I can access these code examples, because I don't know where Enthought Canopy installs these package.

Could you help me on using numpy examples?

+2 votes

I have a numpy array consisting of 1s and zeros for representing binary numbers:

e.g.

 >>> binary
 array([ 1., 0., 1., 0.])

I wish the array to be in the form 1010, so it can be manipulated. I do not want to use built in binary converters as I am trying to build my own.

+1 vote

A list can contain different types of elements but I am not able to understand how max () method work with different types of data elements.

...