Hi,
I want to create a matrix containing 20 rows and 2 cols. I know how to do this, but I have these two files; number and amount (each file contains 1 col and 20 rows) and I don't know how to add these files into this matrix that I'm creating like this,

import numpy as ny
M = ny.zeros((20,2),float)
M[:,0] = number
M=[:,1] = amount

Do I need a for loop to be able to add all the information into all the rows in the matrix? Because now I get this error message saying ValueError: setting an array element with a sequence. Also it complains about my files is not number, e.g. number-file rows looks like this

Recommended Answers

All 16 Replies

The following works for me

import numpy as ny
data = list(ny.loadtxt(filename) for filename in ("data1.txt", "data2.txt"))
result = ny.array(zip(*data))
print result
print result.shape

If there is a numpy way to do the zip(*data), it would probably be faster. You can also time itertools.izip() for comparison.

So what if I want to write these files; number and amount in two different functions in the same porgram and then just return res1 and res2, And then 'call' on these two results in the 'main program'. Would I be able to use it like I did and skip the zip?

If you want to load the files separately, you only need ny.loadtxt("filename") . But you shouldn't worry too much about performance, because 20 lines is not many lines, unless it's called 1000000 times.

Actually its like 1000 lines, but maybe it still works for that purpose.

Actually its like 1000 lines, but maybe it still works for that purpose.

It should be OK. If you want to be sure, learn to use module timeit (but don't read the file 1000000 times, it's not good for your hard disk).

Do I need a for loop? I get error TypeError: zip argument #1 must support iteration

Do I need a for loop? I get error TypeError: zip argument #1 must support iteration

Did you write the same code that I wrote above ? because for me it runs without errors.

yes, it doesn't work. I still get error message.

It seems as it can't convert str to float and my outputfiles looks something like this .

yes, it doesn't work. I still get error message.

Does

print ny.loadtxt("data1.txt")

print a numpy array, as it should ? Please post code (also make sure to read the documentation for loadtxt())
By the way ny.array(zip(*data)) takes 2.49 milliseconds on my computer with python 2.6 and 2 arrays of size 1000.
Here are my files data1.txt and data2.txt

I found a faster way than zip (a numpyish way)

data = tuple(ny.loadtxt(filename) for filename in ("data1.txt", "data2.txt"))
    result = ny.column_stack(data)
    print result
    print result.shape

""" my output -->
[[  45.56564    45.56564 ]
 [ 564.8484    564.8484  ]
 [ 114.25477   114.25477 ]
 ..., 
 [ 114.25477   114.25477 ]
 [   1.325588    1.325588]
 [   2.36547     2.36547 ]]
(1000, 2)
"""

the ny.column_stack(data) is 166 times faster than array(zip(*data)), with 0.015 milliseconds.

It works now! Thank you!

What if I want to add this column1 together with column2, i.e. col1 + col2, into a column 3 in the same matrix. Could I accomplish that with this kind of code?

Sure

data = tuple(ny.loadtxt(filename) for filename in ("data1.txt", "data2.txt"))
    data += (data[0] + data[1],)
    result = ny.column_stack(data)

Thank you! Now it works perfectly!

Thank you! Now it works perfectly!

You are welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.