[英]What does this mean and how can I fix it?
我正在嘗試為將來的異常創建多維數組。 該錯誤不斷彈出。 有什么問題? 我以前在更大的數組上使用了相同的代碼,試圖縮短它,但現在出現了很多錯誤。
import numpy as np
import csv
emptystns=[]
with open('stationlist2.tsv') as i:
j = csv.reader(i, delimiter='\t')
next (j)
for r in j:
emptystns.append(r[0])
lines = []
ns = sum(1 for line in open('stationlist2.tsv'))-1
#3660 stations (minus the headerline which is not a stationid)
nmons = 12
#12 months in a year
nlays = 8
#the number of depths in layers is the number of layers
stnslist= []
data=np.ma.masked_all((ns, nlays, nmons), dtype=np.float64)
with open ('SoilAverage1981.tsv', 'r') as f:
reader= csv.reader(f,delimiter = '\t')
next(reader)
for line in reader:
temp = line[2:]
istn= emptystns.index(line[0])
ilayers = line[1]
for i, info in enumerate(temp):
imonth = i
if info !='':
data[istn, ilayers, imonth] = info
print 'done'
錯誤信息:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
我會進行有根據的猜測,因為似乎只有一個地方為numpy數組建立索引:
data[istn, ilayers, imonth]
查看data
,檢查其shape
和dtype
。
查看索引istn
, ilayers
, imonth
。 錯誤提供的列表中是否沒有? 可能是字符串,可能是浮點數或列表?
integers,
slices (`:`),
ellipsis (`...`),
numpy.newaxis (`None`)
integer or boolean arrays
調試錯誤的基礎如下:
識別錯誤發生的地方
識別表達式中的所有變量
驗證這些變量的標識和/或屬性
哪個與錯誤消息匹配或與記錄的行為不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.