简体   繁体   English

索引超出范围错误:Python

[英]Index out of bounds error : Python

I have a set of data that is not smooth. 我有一组不平滑的数据。 Hence I'm trying to interpolate it with the generated mesh. 因此,我试图用生成的网格对其进行插值。

import numpy as np
data=np.loadtxt('test.txt')
x=data[:,0] #len(x) = 730
y=data[:,1]

Nx= len(x)
Ny=len(y)
del_x= 0.5
xn = np.linspace(0,Nx,2000)
yn = np.linspace(0,Ny,2000)

#loop 
for i in range(0,Nx-1):
 if x[i] > xn[i] and x[i] < xn[i+1]:
  new_x= (i + (xn[i] - x[i])/(xn[i]-xn[i+1]))*del_x
print new_x

I would like to perform the loop that basically does the operation ie : if my original data x[i] is in between the two grid points xn[i],xn[i+1], then compute new_x. 我想执行基本执行该操作的循环,即:如果我的原始数据x [i]在两个网格点xn [i],xn [i + 1]之间,则计算new_x。

But I get the following error 但我收到以下错误

Traceback (most recent call last):
 File "new.py", line 27, in <module>
  if x[i] > xn[i] and x[i] < xn[i+1]:
IndexError: index out of bound

Can someone help me out ? 有人可以帮我吗 ?

If len(x) = 730 than its too short for the loop. 如果len(x) = 730 ,则对于循环而言太短。 Your loop iterates 1000 times, i would be at some point bigger than 730 and there are no values like x[730] , x[731] etc. You will get an out of bound error because you are accessing not existing values in an array. 您的循环迭代了1000次,在某个点上i会大于730 ,并且没有x[730]x[731]等值。由于访问的是数组中不存在的值,因此会出现错误错误。

您可以这样更改for循环:

for i in range(0, len(x)):

First some basics 首先是一些基础知识

  1. Python is "zero indexed", meaning that the first element in the list is indexed as 0 . Python是“零索引”的,这意味着列表中的第一个元素被索引为0
  2. Ranges are given as: [start, stop[ , meaning that the last element is not included. 范围为: [start, stop[ ,表示不包括最后一个元素。

Just see what happens if you try: 请尝试以下操作,看看会发生什么:

a = [1, 2, 3]
print("a has %d elements:" %(len(a)))
for i in range(len(a)):
  print("a[%d] = %d" %(i, a[i]))

The output is: 输出为:

a has 3 elements:
a[0] = 1
a[1] = 2
a[2] = 3

If you try to access a[3] you will see: 如果您尝试访问a[3] ,则会看到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Now to your problems 现在解决您的问题

Numpy "linspace" takes the following input: numpy.linspace(start, stop, N) , resulting in a closed range [start, stop] (notice it includes the "stop") with N elements. Numpy“ linspace”接受以下输入: numpy.linspace(start, stop, N) ,导致具有N元素的封闭范围[start, stop] (注意它包括“ stop”)。

You want the number of elements, or len(xn) to be 730, and I am guessing you want the "stop" value to be max(x) : 你想要的元素个数,或len(xn)为730,和我猜测你想要的“停止”值是max(x)

xn = numpy.linspace(0, max(x), len(x))

for i in range(len(x)):
  ...

Tip: If you are uncertain about the signature of a function you can always ask for help , try: help(numpy.linspace) 提示:如果不确定函数的签名,可以随时寻求help ,请尝试: help(numpy.linspace)

numpy linspace official documentation numpy linspace官方文档

xn = np.linspace(0,Nx,2000)

in here 0 is start value, Nx is end value and 2000 is the number of values to be generated. 其中0是开始值, Nx是结束值, 2000是要生成的值数。

returned object of linspace is something like : linspace返回的对象类似于:

[0, 0.5, 1, ... , ] #2000 elements ie a row containing 2000 elements [0, 0.5, 1, ... , ] #2000 elements即包含2000个元素的行

x=data[:,0] x is something like : x=data[:,0] x类似于:

[[1],[2],[3],[4], .... ,[]] #column vector with number of elements in text data

now row size of x can not be greater than 1 ie index has to be less than 1 or equal to 0 现在x的行大小不能大于1即索引必须小于1或等于0

and you can't compare these two arrays 而且你不能比较这两个数组

[ ... ] and [[], [], [], [], ... ] [ ... ][[], [], [], [], ... ]

are incomparable (you can compare the first element though.) 是无与伦比的(尽管您可以比较第一个元素。)

you could use this : reshape 您可以使用此: 重塑

refer to this answer : What does -1 mean in numpy reshape? 请参阅此答案: -1在numpy重塑中是什么意思?

row_x = x.reshape(-1)

will give you a row vector. 将为您提供行向量。

and you can convert to column format by: 您可以通过以下方式将其转换为列格式:

col_x = row_x.reshape(-1,1)

I would appreciate edits as I am currently at work and thus wrote the answer in hurry. 由于我目前正在工作,因此我很感激,因此急着写了答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM