python - 这是什么意思,我该如何解决?
问题描述
我正在尝试为未来的异常创建一个多维数组。这个错误不断弹出。有什么问题?我之前对更大的数组使用了相同的代码,试图缩短它,现在它有很多错误。
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
调试这样的错误的基础知识:
确定错误发生的位置
识别表达式中的所有变量
验证这些变量的标识和/或属性
哪些与错误消息匹配或与记录的行为不匹配。
推荐阅读
- c# - 如何在文本文件的每一行中分隔字符串并将它们与用户输入进行比较
- azure - Function App Deployment Failed - 远程服务器返回错误:(403) Forbidden
- r - 无法使用 ggsave() 在 PDF 中嵌入 ggplot2 (/R) 字体
- asp.net-core - asp.net 核心泊坞窗图像“aspnet:2.2-stretch-slim”
- python - 删除每 10 位数字
- python - 在端口上运行 WSGI Web 服务器(不使用 IIS)
- reactjs - React:Lerna React 在 monorepo 上挂钩错误
- c# - 如何打包项目和使用 NuGet 包 + 文件?
- ssh-keys - CloverETL:如何使用 ssh 密钥连接到服务器
- html - 当在 inner-div 上设置宽度时,Z-Index 不适用于border-right