3
我有一個.txt
文件,具有不同長度的行。每一行都是代表一個軌跡的連續點。由於每條軌跡都有其自己的長度,所以行的長度都不相同。也就是說,列的數量因行而異。Python中genfromtxt()的變量列數?
AFAIK,Python中的genfromtxt()
模塊要求列數相同。
>>> import numpy as np
>>>
>>> data=np.genfromtxt('deer_1995.txt', skip_header=2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1638, in genfromtxt
raise ValueError(errmsg)
ValueError: Some errors were detected !
Line #4 (got 2352 columns instead of 1824)
Line #5 (got 2182 columns instead of 1824)
Line #6 (got 1412 columns instead of 1824)
Line #7 (got 1650 columns instead of 1824)
Line #8 (got 1688 columns instead of 1824)
Line #9 (got 1500 columns instead of 1824)
Line #10 (got 1208 columns instead of 1824)
It is also able to fill the missing values by the help of filling_values
。但是,我認爲這會產生不必要的麻煩,我希望避免。
那麼,什麼是最好的(Pythonic)的方式來簡單地導入這個數據集在沒有填寫「缺失值」?
'data'仍然會具有可變長度的行儘管... –
是的。無論如何,嘗試從可變長度的行輸入創建固定長度的行是很奇怪的:它可能表明模型是錯誤的 – lucasg