1

I'm trying to save the results of a model estimation to a file. I have tried a number of things but it doesn't work because I'm obviously not doing it correctly. I'm not even sure os.path.join is the right command from os.path:

#output the vectors for winsize=5 and features=10
study1_path = os.path.join("/Users/dlhoffman/Study 1/")
print(study1_path)

/Users/dlhoffman/Study 1/

ifttt_model.wv.save_word2vec_format("study1_path/vectors.txt", binary=False, total_vec=None)

Since I have to estimate this model a number of times and I'd like to save some brute force typing, I'd also like to label each output file with the values of the hyperparameters winsize and features.

The file name I would like to get for say, winsize=5 and features=10 are:

 /Users/dlhoffman/Study 1/5w10fvectors.txt

So in the above case, the "5" and the "10" come from the values of the variables winsize and features. I've studied the examples & the os.path module documentation, but don't know enough python to get what I should do. Any ideas?

profhoff
  • 1,017
  • 1
  • 13
  • 21

4 Answers4

3

To generate custom directory names, you can easily do that using format strings. For example:

>>> wf = [(1,3), (2,4), (5,10), (2,90)]
>>> for w,f in wf:
...     print '/Users/Study 1/{}w{}fvectors.txt'.format(w, f)
...
/Users/Study 1/1w3fvectors.txt
/Users/Study 1/2w4fvectors.txt
/Users/Study 1/5w10fvectors.txt
/Users/Study 1/2w90fvectors.txt
game0ver
  • 1,250
  • 9
  • 22
2

You would format the string containing the file name to contain the variables as needed. String formatting (in one way) works like this

"number_%d" % (5) == "number_5"

When you use the % operator in python on a string, it takes the arguments in the tuple to the right of the operator and places them into the string. In this case, %d is replaced by a int. %s is replaced by a str, %f is replaced by a float. %r is replaced by the __str__() method of the object being serialized.

Example below

base_path = "/Users/dlhoffman/Study 1/"
filename_template = "%dw%dfvectors.txt"
for winsize, features in [(5, 10), (10, 20), (15, 25)]:
    filename = filename_template % (winsize, features)
    print filename
    # prints  "5w10fvectors.txt"
    # prints  "10w20fvectors.txt"
    # prints  "15w25fvectors.txt"
    fullpath = os.path.join(base_path, filename)
    with open(fullpath) as fh:
        fh.write("some data")
John Allard
  • 3,564
  • 5
  • 23
  • 42
  • So I tried a combo of these suggestions, since writing the results lives inside a function, but my python still isn't good enough. Here's what I tried: `base_path='/Users/dlhoffman/Study 1/' filename_template="%dw%df_words-vectors.csv" filename=filename_template % (winsize, features) fullpath=os.path.join(base_path,filename) vectors.to_csv('fullpath',index=False)` but what this does is write a file called 'fullpath' to the working directory, not the basepath directory. Any thoughts? – profhoff Mar 23 '18 at 23:27
  • Good news is that the contents of the file are correct, but the name is wrong and it's in the wrong directory. – profhoff Mar 23 '18 at 23:36
  • the last line should be `vectors.to_csv(fullpath,index=False)`, fullpath is a variable, not a string. – John Allard Mar 24 '18 at 00:12
1

I think what you are searching for, is something like this:

file_name = os.path.join("folder_path", f"{variable}_vectors.txt")

and you also need to:

import os

at the top of your file.

0

I played with it some more and this works!

#final step - write the results of this cell to a csv
#file contains word, word count, v1-vn

path=r'/Users/dlhoffman/Study 1/'
filename_template="%dw%df_words-vectors.csv"
filename=filename_template % (winsize, features)
vectors.to_csv(os.path.join(path, filename), index=False)
profhoff
  • 1,017
  • 1
  • 13
  • 21