2

I'm reading data (numbers) from a file into a list, as follows:

    weight_file = open(model_name, 'r').readlines()
    weights = weight_file[6:]

It seems that I can't read them straight into a numpy.array because the first rows of the file contains words.

So now, in weights I have a list as follows: [['1 2 3'] ['4 5 6']]

Now I want to convert it into a numpy.array. I tried this:

    weights_np = np.array([])
    for weight in weights:
        weights_np = np.append(weights_np, weight.split())

What this does is create a single vector: [1, 2, 3, 4, 5, 6], but I need it to be represented as some kind of matrix - like a list of lists. How can I do that?

Thanks

4 Answers 4

2

If weights_np is this:

In [23]: weights_np = np.array([1, 2, 3, 4, 5, 6])

then you could use reshape to make it 2-dimensional with 3 columns:

In [24]: weights_np = weights_np.reshape((-1, 3))

In [25]: weights_np
Out[25]: 
array([[1, 2, 3],
       [4, 5, 6]])

But a more satisfying method would be to parse the file properly with np.loadtxt or np.genfromtxt. (This would be much much faster than using Python loops and calling np.append.) Note that these functions have a skiprows parameter which you could use to skip the first few rows:

weights = np.loadtxt(model_name, skiprows=6)
Sign up to request clarification or add additional context in comments.

Comments

2

To get a "kind of matrix", simply use:

numpy.array([[int(val) for val in line.split()] for line in open(model_name)])

To get a simple vector, use:

numpy.array([int(val) for line in open(model_name) for val in line.split()])

Comments

1
>>> import numpy as np
>>> weights = ['1 2 3', '4 5 6']
>>> weights_np = np.array(map(lambda x: map(int, x.split()), weights))
>>> weights_np
array([[1, 2, 3],
       [4, 5, 6]])

Comments

1

Try it this way:

weights_np = np.empty((0,len(weights[0].split())),int)
for weight in weights:
    weights_np = np.append(weights_np, [map(int,weight.split())], axis=0)

Demo:

>>> weights = ['1 2 3','4 5 6']
>>> weights_np = np.empty((0,len(weights[0].split())),int)
>>> for weight in weights:
...     weights_np = np.append(weights_np, [map(int,weight.split())], axis=0)
... 
>>> weights_np
array([[1, 2, 3],
       [4, 5, 6]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.