joelgrus / data-science-from-scratch

code for Data Science From Scratch book
MIT License
8.63k stars 4.5k forks source link

Chapter 4 vector_sum code not working #30

Closed julietwk closed 7 years ago

julietwk commented 7 years ago

Hey, working through some of the examples and it seems the below code is not set up properly even though vector_add has been defined:

def vector_sum2(vectors): return reduce(vector_add, vectors)

When I try and run the below in the ipython console I get an error saying: "zip argument #1 must support iteration"

vector_sum([1,2,3,4])

Please could you verify that the code is correct/incorrect?

Many thanks!

joelgrus commented 7 years ago

vector_sum is supposed to take a list of "vectors" (that is, a list of lists) and vector_add them together. you're giving it a vector. it should work if you did

vector_sum([[1, 2, 3, 4]])  # a list with one 4-vector

or

vector_sum([[1], [2], [3], [4]])  # a list of four 1-vectors
julietwk commented 7 years ago

Amazing!! Thanks a lot Joel! :)