Python 就绪列Numpy/Matplotlib
我正在尝试使用Python 就绪列Numpy/Matplotlib,python,numpy,Python,Numpy,我正在尝试使用numpy.loadtext读取3列,但出现错误: ValueError: setting an array element with sequence. 数据样本: 0.5 0 -22 0.5 0 -21 0.5 0 -22 0.5 0 -21 第1列是一个从0.5增加到6.5的距离,每个距离有15个数据样本 列2是一个角度,当距离返回0.5时,该角度将增加45度 第3列包含正在测量的数据(RSSI),它从-20降低到-70 我
numpy.loadtext
读取3列,但出现错误:
ValueError: setting an array element with sequence.
数据样本:
0.5 0 -22
0.5 0 -21
0.5 0 -22
0.5 0 -21
第1列是一个从0.5增加到6.5的距离,每个距离有15个数据样本
列2是一个角度,当距离返回0.5时,该角度将增加45度
第3列包含正在测量的数据(RSSI),它从-20降低到-70
我正在使用以下代码尝试将这三列加载到单独的数组中:
import numpy as np
r, theta, RSSI, null = np.loadtxt("bot1.txt", unpack=True)
我将平均每个距离/角度组合的采样RSSI,然后我希望将数据绘制为3D
极坐标图。不过我还没走到这一步
关于为什么
np.loadtxt
不起作用,有什么想法吗?除了将3列解压成4个变量之外,我看不出有任何问题。事实上,这适用于我的NumPy 1.6.2,包括:
r, theta, RSSI = np.loadtxt("bot1.txt", unpack=True) # 3 columns => 3 variables
也可以在纯Python中执行相同的操作,以便查看是否有其他原因导致问题(如文件中的错误):
如果文件出现问题(如果一个非空行不能解释为三个浮点数),则会打印出有问题的行并引发异常。Ah,我实际上已经输入了null值,以检查rssi值之后是否有可能导致发生了有趣的事情。我认为这可能是文件大小的问题。您是否绝对确定文件每行只包含三列,表示实数?我在回答中添加了一个测试程序,如果有一行不符合您的期望,它会报告问题。PS:您的输入文件有多大?如果我没记错的话,我已经用
loadtxt()
读取了数百万行的文件;如果文件的大小是个问题,我会感到惊讶。
import numpy
def loadtxt_unpack(file_name):
'''
Reads a file with exactly three numerical (real-valued) columns.
Returns each column independently.
Lines with only spaces (or empty lines) are skipped.
'''
all_elmts = []
with open(file_name) as input_file:
for line in input_file:
if line.isspace(): # Empty lines are skipped (there can be some at the end, etc.)
continue
try:
values = map(float, line.split())
except:
print "Error at line:"
print line
raise
assert len(values) == 3, "Wrong number of values in line: {}".format(line)
all_elmts.append(values)
return numpy.array(all_elmts).T
r, theta, RSSI = loadtxt_unpack('bot1.txt')