0

I am new to this question. I hop to get benefit of your advice. Sorry if it is amateurish.

I have the following code which finally shows a plot. I just write one part of code.

...
cov = np.dot(A, A.T)
samps2 = np.random.multivariate_normal([0]*ndim, cov, size=nsamp)
print(samps2)
names = ["x%s"%i for i in range(ndim)]
labels =  ["x_%s"%i for i in range(ndim)]
samples2 = MCSamples(samples=samps2,names = names, labels = labels, label='Second set')
g = plots.getSubplotPlotter()
g.triangle_plot([samples2], filled=True)

It has no problem. The plot is drawn using the data coming from samps2. To see what the samps2 is, we do print(samps2) and see:

[[-0.11213986 -0.0582685 ]
 [ 0.20346731  0.25309022]
 [ 0.22737737  0.2250694 ]
 [-0.09544588 -0.12754274]
 [-1.05491483 -1.15432073]
 [-0.31340717 -0.36144749]
 [-0.99158936 -1.12785124]
 [-0.5218308  -0.59193326]
 [ 0.76552123  0.82138362]
 [ 0.65083618  0.70784292]]

My question is, If I want to read these data from a txt file. what should I do?

Thank you.

1 Answer 1

3

There are several ways. What comes to my mind is:

plain python:

data = []
with open(filename, 'r') as f:
    for line in f:
        data.append([float(num) for num in line.split()])

numpy:

import numpy as np
data = np.genfromtxt(filename, ...)

pandas:

import pandas as pd
df = pd.read_table(filename, sep='\s+', header=None)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much my friend for you complete answer. I dont know why, but just numpy is working.
You're welcome. I think the three are very similar, except that pandas uses dataframes instead of plain arrays, but still: I don't know what you mean by not working, so perhaps your approach for some reason fits only to numpy arrays, but not to lists or dataframes.

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.