1

I want to pairwise compare several lists in a kind of "bag of words" approach. I have only strings in my lists.
Unfortunatelly, I have a bug in my script that I cannot fix.
The code works if there are numbers in the lists but as soon as I have strings in the lists it doesn't run anymore. I appreciate your help.

I receive following error message:

Traceback (most recent call last):
File "test.py", line 21, in <module>
bow_matrix[0, p] = list_words_ab[p]
ValueError: could not convert string to float: 'd'

My code:

a = ["a", "b", "c", "d"]
b = ["b", "c", "d", "e"]

p = 0
if len(a) > len(b):
    max_words = len(a)
else:
    max_words = len(b)
list_words_ab = list(set(a) | set(b))
len_bow_matrix = len(list_words_ab)
bow_matrix = numpy.zeros(shape = (3, len_bow_matrix))

while p < len_bow_matrix:
    bow_matrix[0, p] = list_words_ab[p]
    p = p+1
p = 0
while p < len_bow_matrix:
    bow_matrix[1, p] = a.count(bow_matrix[0, p])
    bow_matrix[2, p] = b.count(bow_matrix[0, p])
    p = p+1
3
  • Please provide the sample inputs and your required output Commented Aug 23, 2016 at 20:05
  • For example a = [NLS, VS40, AMPR], b = [VS40, VS40, AMPR, GFP] I want to get a matrix that looks like this: [ [NLS, VS40, AMPR, GFP], [1, 1, 1, 0], [0, 2, 1, 1] ] In the first row there is the union of all strings in both list (without duplicates) and in row two/three I want to have the frequency how often that string occurs in list a/b. Commented Aug 23, 2016 at 20:09
  • 1
    I believe using numpy.zeros creates a matrix that expects floats. Commented Aug 23, 2016 at 20:11

1 Answer 1

3

By default numpy.zeros makes an empty array of floats, to use strings you need to specify dtype=str:

bow_matrix = numpy.zeros(shape = (3, len_bow_matrix),dtype=str)
Sign up to request clarification or add additional context in comments.

1 Comment

dtype=str only makes S1, single character words. Which is fine here, but not in more general cases.

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.