0

So here is the thing. I have a list of lists and some of the list are empty... but if it is not empty.. all the list are of fix length.. the length which I dont know.. For example

  for feature in features:
     print len(feature)

This will print either 0 or length k.

I am trying to write a function

  def generate_matrix(features):
     # return matrix

Which returns a numpy matrix.. with empty rows replaces with zero vector. Is there a way to do this efficiently? I mean... I don't want to loop thru to find the shape of matrix and then again loop thru to generate row (with either np.array(feature) or np.zeros(shape)..

5
  • Is there a reason why you don't want to use np.zeros? Commented Aug 27, 2015 at 23:55
  • Why don't you want to loop through the list twice? Have you done any timings? Commented Aug 28, 2015 at 1:20
  • @ballsatballsdotballs: I can use np.zeros.. but how do i find the dimensions of zero vec apriori. Commented Aug 28, 2015 at 1:42
  • Do all of the non-empty features have the same length? Commented Aug 28, 2015 at 1:48
  • @ballsatballsdotballs Yepp Commented Aug 28, 2015 at 1:50

1 Answer 1

2

Edit: I didn't realize that all of the non-empty features were the same length. If that is the case then you can just use the length of the first non-zero one. I added a function that does that.

f0 = [0,1,2]
f1 = []
f2 = [4,5,6]

features = [f0, f1, f2]

def get_nonempty_len(features):
    """
    returns the length of the first non-empty element
        of features.     
    """
    for f in features:
        if len(f) > 0:
            return len(f)
    return 0

def generate_matrix(features):
    rows = len(features)
    cols = get_nonempty_len(features)
    m = np.zeros((rows, cols))
    for i, f in enumerate(features):
        m[i,:len(f)]=f
    return m

print(generate_matrix(features))

Output looks like:

[[ 0.  1.  2.]
 [ 0.  0.  0.]
 [ 4.  5.  6.]]
Sign up to request clarification or add additional context in comments.

3 Comments

The OP doesn't want to "loop thru to find the shape of matrix and then again loop thru to generate row (with either np.array(feature) or np.zeros(shape)"
@yangjie Ok but why? He has two options: making all of his lists the same length by padding them with zeros (which seems like a bad idea since you are changing your input lists), or finding the shape and then creating an ndarray with them. Both of those require looping through the features, anyway.
The length fetching loop could stop at the 1st nonzero value. The OP claims length is either 0 or k.

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.