J.D.
J.D.

Reputation: 169

Reading and Writing Matrices in Python, Could not Convert String to Float Error

I'm trying to write a matrix (i.e. list of lists) to a txt file and then read it out again. I'm able to do this for lists. But for some reason when I tried to move up to a matrix yesterday, it didn't work.

    genotypes=[[] for i in range(10000)]
    for n in range(10000):
        for m in range(1024):
            u=np.random.uniform()
            if u<0.9:
                genotypes[n].append(0)
            elif 0.9<u<0.99:
                genotypes[n].append(1)
            elif u>0.99:
                genotypes[n].append(2)
    return genotypes

#genotypes=genotype_maker()
#np.savetxt('genotypes.txt',genotypes)

g=open("genotypes.txt","r")
genotypes=[]
for line in g:
    genotypes.append(int(float(line.rstrip())))

I run the code twice. The first time the middle two lines are not commented out while the last four are commented out. It looks like this successfully writes a matrix of floats to a .txt file

The second time, I comment out the middle two lines and uncomment the last four. Unfortunately I then get the error message: ValueError: could not convert string to float: '0.000000000000000000e+00 0.000000000000000000e+00 (and a whole lot more of these)

What's wrong with the code?

Thanks

Upvotes: 0

Views: 112

Answers (2)

Karl Knechtel
Karl Knechtel

Reputation: 61527

a matrix (i.e. list of lists)

Since we are already using numpy, it is possible to have numpy directly generate one of its own array types storing data of this sort, directly:

np.random.choice(
    3, # i.e., allow values from 0..2
    size=(10000, 1024), # the dimensions of the array to create
    p=(0.9, 0.09, 0.01) # the relative probability for each value
)

Documentation here.

Upvotes: 0

Nakor
Nakor

Reputation: 1514

In your case, you should just do np.loadtxt("genotypes.txt") if you want to load the file. However, if you want to do it manually, you need to parse everything yourself. You get an error because np.savetxt saves the matrix in a space-delimited file. You need to split your string before converting it. So for instance:

def str_to_int(x):
    return int(float(x))

g=open("genotypes.txt","r")
genotypes=[]

for line in g:
    values = line.rstrip().split(' ') # values is an array of strings
    values_int = list(map(str_to_int,values)) # convert strings to int
    genotypes.append(values_int) # append to your list

Upvotes: 1

Related Questions