将数据作为数组导入以在Python中打印

2024-09-29 19:35:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这个问题不太熟悉。我想听听你的建议。抱歉,如果是业余的。你知道吗

我有下面的代码,它最终显示了一个情节。我只写了一部分代码。你知道吗

...
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)

没问题。绘图是使用来自samps2的数据绘制的。为了了解samps2是什么,我们做print(samps2),然后看:

[[-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]]

我的问题是,如果我想从txt文件中读取这些数据。我该怎么办?你知道吗

谢谢你。你知道吗


Tags: 数据代码inforlabelsnamesnprange
1条回答
网友
1楼 · 发布于 2024-09-29 19:35:35

有几种方法。我想到的是:

普通Python:

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

努比:

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

熊猫:

import pandas as pd
df = pd.read_table(filename, sep='\s+', header=None)

相关问题 更多 >

    热门问题