1

Hi All: I am new to python and the question might be straightforward to some of you. My details question (writing in the steps of my task) is the following:

  1. I need to generate 10 random 2D points under Gaussian distribution, which is easy:

    Testing = np.random.multivariate_normal(mean, cov, 10)
    

where mean, cov are already defined.

  1. I need to store the above values into a txt file, for later testing, which is also done:

    with open("Testing.txt", "w") as f:
        f.write(str(Testing))
    
  2. In step 3, I need to read all the 10 points created in step 1 which are stored in step 2 into a new list and do computation, which I wrote:

    with open('Testing.txt') as f:
        Data = f.read()
    

    note here the above two lines of codes are written in a separate script different from the script of step 1 & 2.

Step 3 itself seems OK, because if I test

    print(Data)

it will print out in the terminal the same format as the values and format in the txt file, the problem is the data structure seems different from the Testing data structure created in step 1. For example if in Step 1, I add an additional line

    plt.scatter(Testing[:,0],Testing[:,1],c='yellow')

it will plot the 10 points for me. But if I write the same command in the script of step 3

    plt.scatter(Data[:,0],Data[:,1],c='yellow')

this will be an error "TypeError: string indices must be integers, not tuple". So apparently the structures for Data and Testing are different. Anyone can help me on this? Thanks!

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
DemonSu
  • 13
  • 3
  • 2
    `Data` is just a regular `str`, whereas the fancy `[:,0]` indexing syntax is for e.g. `np.array` objects. You need to parse the incoming data into a more usable form, or make your own life easier with `numpy`'s `fromfile` and `tofile`. – jonrsharpe Sep 19 '14 at 07:53
  • @jonrsharpe Yes you are right, I understand that. I can tell that the values written into the txt file are actually strings because of the str(Testing) command, and naturally when I read them back, they are still strings. I am just confused about how to convert the strings in Data back into the fancy form. – DemonSu Sep 19 '14 at 07:58
  • Well I have no idea why you expected that after dumping a string representation of an object to a file and reading back in that string you could treat that string like the object, but if you search for the functions I mention there's a much better way. – jonrsharpe Sep 19 '14 at 08:03

1 Answers1

0

If you use NumPy functions for file input-output when you read the data back what you get is an array that can be sliced as Data[:,0], for example:

step 2) np.savetxt("Testing.txt", Testing)

step 3) Data = np.loadtxt("Testing.txt", dtype=object). Note that dtype=object may be needed here based on your comments, that you are actually reading str objects.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234