3

Python does not finish the following function (using iPython Notebook and Graphlab)

def print_submission_file(var, filename='submission.txt'):
    with open(filename, 'w') as f:
        f.write('Id,Sales\n')
        for row in var:
            f.write(str(row['Id']) + ',' + str(row['Prediction']) + '\n')

print_submission_file(test, 'submission.txt')

This is the log when the script is stopped (after 5/10 mins)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-9-f51bf8359fb9> in <module>()
  5             f.write(str(row['Id']) + ',' + str(row['Prediction']) + '\n')
  6 
----> 7 print_submission_file(test, 'submission.txt')

<ipython-input-9-f51bf8359fb9> in print_submission_file(var, filename)
  2     with open(filename, 'w') as f:
  3         f.write('Id,Sales\n')
----> 4         for row in var:
  5             f.write(str(row['Id']) + ',' + str(row['Prediction']) + '\n')
  6 

/Users/MaximeDeBruyn/.graphlab/anaconda/lib/python2.7/site-packages/graphlab/data_structures/sframe.pyc in generator()
   3694         def generator():
   3695             elems_at_a_time = 262144
-> 3696             self.__proxy__.begin_iterator()
   3697             ret = self.__proxy__.iterator_get_next(elems_at_a_time)
   3698             column_names = self.column_names()

graphlab/cython/cy_sframe.pyx in        graphlab.cython.cy_sframe.UnitySFrameProxy.begin_iterator()

graphlab/cython/cy_sframe.pyx in graphlab.cython.cy_sframe.UnitySFrameProxy.begin_iterator()

RuntimeError: Runtime Exception. Canceled by user

So the function writes "Id, Sales" in the submission.txt but never finishes the for loop... Thank you for your help. I really don't know what to do.

Maxime De Bruyn
  • 889
  • 7
  • 19
  • 1
    Your indentation is off. – juanchopanza Oct 11 '15 at 10:26
  • 1
    Could we see what `var` is, please? Obviously it's some kind of data frame, but since you seem to be having trouble iterating through it, it would be helpful to know what its generator is trying to do. – Borealid Oct 11 '15 at 10:26
  • 1
    Thanks, I edited the indentation. @Borealid, var is a graphlab SFrame (graphlab.data_structures.sframe.SFrame) – Maxime De Bruyn Oct 11 '15 at 10:30
  • 1
    Have you tried writing the sframe into a list and passing that into the function? That at least would identify if the issue is with iterating over the sframe or not – Rolf of Saxony Oct 11 '15 at 10:47

1 Answers1

1

Your loop is probably just taking a long time to iterate through the SFrame that way. The error message is because you cancelled the command with Ctrl-C (I'm guessing).

If you want to write a csv with just those two columns, try this (assuming var is an SFrame):

var[['Id','Prediction']].save("submission", format="csv")
Evan Samanas
  • 516
  • 3
  • 6