I am really
new to python. I am getiing an error stating Indexerror list index out of range. Kindly help me out. Thanks in advance .
Edit 1
x = np.array([10,0])
Phi = np.array([[ 1. , 0.01],
[ 0. , 1. ]])
Gamma = np.array([[ 0.0001048 ],
[ 0.02096094]])
Z = np.array([[ 0.0001048 ],
[ 0.02096094]])
wd = 0
u_new = 0
x1d = 0
x2d = 0
xd = [[0 for col in range(len(x))] for row in range(1000)]
xd[0][0] = 10
xd[1][0] = 0
k = 10
DistPeriodNo1 = 500
FirstPeriod = 1
k=k+1 #Update PeriodNo(so PeriodNo is now equal to No. of current period)
if (k == 100): #If maximum value of PeriodNo is reached,
k = 11 #set it to 1
DistPeriodNo1 = random.randint(11,99)
if (FirstPeriod == 0):
if (k == DistPeriodNo1):
wd = random.randint(-1,1)
else:
wd = 0
xd[0][k] = Phi*xd[0][k-1] - Gamma*u_new + Z*wd
# >>indexerror list index out of range
xd[1][k] = Phi*xd[1][k-1] - Gamma*u_new + Z*wd
x1d = xd[0][k]
x2d = xd[1][k]
To answer your question in the comments about tracebacks (stack traces): running the following
a = [1,2,3]
b = [True, False]
print(a[2])
print(b[2])
produces one answer and one traceback.
>>>
3
Traceback (most recent call last):
File "C:\Programs\python34\tem.py", line 4, in <module>
print(b[2])
IndexError: list index out of range
The traceback shows what line and what code raised the error. People were asking you to copy the last 4 line and paste them into your question (by editing it).
Related
How do I find the rows(indices) of my array, where its values change?
for example I have an array:
0 -0.638127 0.805294 1.30671
1 -0.638127 0.805294 1.30671
2 -0.085362 0.523378 0.550509
3 -0.085362 0.523378 0.550509
4 -0.323397 0.94502 0.49001
5 -0.323397 0.94502 0.49001
6 -0.323397 0.94502 0.49001
7 -0.291798 0.421398 0.962115
I want a result like:
[0 2 4 7]
I am happy to use existing librarys and I am not limited to anything. All I want are the numbers of the rows. How would I calculate that?
I tried
a = []
for i, row in enumerate(vecarray):
if i > 0:
a[i] = vecarray[i] - vecarray[i-1]
b = np.where(a != 0)
but that gives me IndexError: list assignment index out of range
arr = [
(-0.638127, 0.805294, 1.30671),
(-0.638127, 0.805294, 1.30671),
(-0.085362, 0.523378, 0.550509),
(-0.085362, 0.523378, 0.550509),
(-0.323397, 0.94502, 0.49001),
(-0.323397, 0.94502, 0.49001),
(-0.323397, 0.94502, 0.49001),
(-0.291798, 0.421398, 0.962115)
]
i = 0
prev_t = None
for t in arr:
if t != prev_t:
prev_t = t
print(i)
i += 1
As mentioned in the title, I am getting an IndexError when iterating through a n-dimensional array. What I am trying to do with the code, is to set portions of the matrix to 0 and keep the rest of the data.
I also realize that selecting an element in a n-dimensional array should be array[row#][column#] but it is just a problem with my naming convention which I will fix later. However, the values are correct.
currentMatrix = np.array(ast.literal_eval(safety.iloc[i]), dtype=np.int)
currentMatrix.shape = (27,15)
tpHeight = int(df['touchH'].iloc[i])
tpWidth = int(df['touchW'].iloc[i])
fullRows = np.arange(15)
fullColumns = np.arange(27)
beforeAreaColumns = np.arange(df['touchX'].iloc[i] - 1, dtype=np.int)
afterAreaColumns = np.arange(df['touchX'].iloc[i] + tpWidth - 1, dtype=np.int)
beforeAreaRows = np.arange(df['touchY'].iloc[i] - 1, dtype=np.int)
afterAreaRows = np.arange(df['touchY'].iloc[i] + tpHeight - 1, dtype=np.int)
print beforeAreaColumns #returns [0 1 2 3 4 5] at i = 0
currentMatrix[beforeAreaColumns][fullRows] = 0
currentMatrix[afterAreaColumns][fullRows] = 0
currentMatrix[fullColumns][beforeAreaRows] = 0
currentMatrix[fullColumns][afterAreaRows] = 0
The error I receive is:
IndexError Traceback (most recent call last)
<ipython-input-114-342e7c1b89ae> in <module>()
26 print beforeAreaColumns
27
---> 28 currentMatrix[beforeAreaColumns][fullRows] = 0
29 currentMatrix[afterAreaColumns][fullRows] = 0
30 currentMatrix[fullColumns][beforeAreaRows] = 0
IndexError: index 6 is out of bounds for axis 0 with size 6
From my understanding, I am getting the error because the code tries to find an index in position 7 when there are only 6 positions. However, I am not sure why it tries index 6 when the array stops at index 5.
I tried changing the code so that it uses for loops, for example:
for beforeAreaColumn in beforeAreaColumns:
for fullRow in fullRows:
currentMatrix[beforeAreaColumn][fullRow] = 0
but I get the same error. The above code is written and run on Python 2.7.
randoms = [rand.uniform(-1,1)for items in range(10)]
x= 0
for i in List:
cm.setKeyframe(at = "%s.cv[x].xValue"%i , t=5, v=rand.choice(randoms))
x+=1
this part of code that i wanna use, but maya can't accept this [x].
someone told me you should do this:
for i in List:
x = 0
cvAttrX = i+".cv["+str(x)+"].xValue"
cm.setKeyframe(at = cvAttrX , t=5, v=rand.choice(randoms))
x+=1
but i got this error:
# Error: line 1: can only concatenate list (not "str") to list
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# File "<maya console>", line 7, in randList
# TypeError: can only concatenate list (not "str") to list #
if someone fix this please explain that deeply to me.
i wanna learn this one very good
and please fix this example too:
randoms = [rand.uniform(cm.floatField(Ceil, q = True , v = True),0.5)for i in range(30)]
for objects in myList:
cm.xform('%s.cv[0]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[1]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[2]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[3]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[4]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[5]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[6]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[7]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[8]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[9]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
cm.xform('%s.cv[10]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
this is a part of my script if i can do increment i don't wanna write this line for exampla 100 time, i can make intField for that and determine how many do i need
i wanna do something like this but i don't know how
import maya.cmds as cm
import random as rand
myList = cm.ls(sl =True)
cvList = []
randomList = []
def Lister():
for i in myList:
cvList.append(cm.ls('%s.cv[:]'%i , flatten = True))
return cvList
def randomize():
x = 0
randomList.append([rand.uniform(-1,1)for items in range(10)])
for i in cvList:
cm.setKeyframe(at = "%s.cv[x].xValue"%i , t=5, v=rand.choice(randoms))
x+=1
myCurve = cm.ls(sl=1) # Will return [u'curve1']
# Since it returned the object in a list in the next command you have to subscript it.
myCVs = cm.ls(myCurve[0] + ".cv[:]", fl=1)
# Result: [u'curve1.cv[0]',
u'curve1.cv[1]',
u'curve1.cv[2]',
u'curve1.cv[3]',
u'curve1.cv[4]',
u'curve1.cv[5]',
u'curve1.cv[6]',
u'curve1.cv[7]',
u'curve1.cv[8]',
u'curve1.cv[9]',
u'curve1.cv[10]'] #
# We do not need to increment through these, flatten takes care of that. We need only iterate through the list of cvs and concatenate the attribute to the name.
for i in myCVs:
cm.setAttr(i + ".xValue", random.uniform(-1, 1))
print cm.getAttr(i + ".xValue")
# This will move all the cvs on that curve by a random value between -1 and 1.
-0.233699187896
0.353531892619
0.736778238244
-0.767007983153
0.672321920729
0.0556060597496
-0.340945469966
0.252136119281
-0.396852920849
-0.683451384587
0.926232431375
Well first of all you're not even set up for animation. You need something to drive it. Right now if you set your keyframe all this will be happening on one frame. You could do something like:
time = cm.currentTime(q=1)
for i in range(10):
cm.setAttr(myCVs[0]+".xValue", random.choice(test))
cm.setKeyframe(myCurve, shape=1, controlPoints=1)
time = cm.currentTime(time + 1)
Or you could plug i into the time flag of cm.setKeyframe. This way it keyframes the whole curve, the transform, shape, and control points. And you wouldn't have to worry about iterating through all of them. Set the value first then set the keyframe, just like you would if you were actually animating.
I have a bunch of files of the following order (tab separated):
h local average
1 4654 4654
2 5564 5564
3 6846 6846
... ... ...
I read the file in a loop (attached below) and store them in a two dimensional list. I then convert the list to array and apply std to it. This results with:
Traceback (most recent call last):
File "plot2.py", line 56, in <module>
e0028 = np.std(ar, axis=0)
File "/usr/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2467, in std
return std(axis, dtype, out, ddof)
TypeError: unsupported operand type(s) for /: 'list' and 'float'
Which baffles me. I tried to find an element in the array which is not float and nothing popped.
import numpy as np
import matplotlib.pyplot as plt
from math import fabs, sqrt, pow, pi
h0028 = []
p0028 = []
headLines = 2
fig=plt.figure()
ax1 = fig.add_subplot(1,1,1)
for i in range (0,24):
n = 0
j = i + 560
p = []
f = open('0028/'+str(j)+'.0028/ionsDist.dat')
for line in f:
if n < headLines:
n += 1
continue
words = line.split()
p.append (float(words[1]))
if i == 0:
h0028.append (fabs(int(words[0])))
n += 1
print (n)
p0028.append(p)
f.close()
ar = np.array(p0028)
for a in ar:
for b in a:
if not isinstance(b,float):
print type(a)
e0028 = np.std(ar, axis=0)
p0028 = np.mean(ar, axis=0)
h0028 = np.array(h0028)/10 -2.6
p0028 /= max(p0028)
e0028 /= (sum(p0028)*sqrt(23))
ax1.errorbar(h0028 , p0028, yerr=e0028, color = 'red')
ax1.set_xlim(-0.1,10)
plt.show()
plt.savefig('plot2.png', format='png')
I can't figure out, why your code does not work, but maybe this will help you.
You can read the file like this:
>>>a = np.loadtxt("p0028.csv",dtype="float",skiprows = 1)
>>> a
array([[ 1.00000000e+00, 4.65400000e+03, 4.65400000e+03],
[ 2.00000000e+00, 5.56400000e+03, 5.56400000e+03],
[ 3.00000000e+00, 6.84600000e+03, 6.84600000e+03]])
Now you can get the std of e.g. the column local like this:
>>>a_std = np.std(a[:1])
2193.4452352406706
When you loop over several files, you can use the vstack method to collect the data together, that way you do not depend on the number of rows in the file:
>>>a = np.loadtxt("p0028.csv",dtype="float",skiprows = 1)
>>> a
array([[ 1.00000000e+00, 4.65400000e+03, 4.65400000e+03],
[ 2.00000000e+00, 5.56400000e+03, 5.56400000e+03],
[ 3.00000000e+00, 6.84600000e+03, 6.84600000e+03]])
>>>b = np.loadtxt("p0028.csv",dtype="float",skiprows = 1)
>>> np.vstack((a,b))
array([[ 1, 4654, 4654],
[ 2, 5564, 5564],
[ 3, 6846, 6846],
[ 1, 4654, 4654],
[ 2, 5564, 5564],
[ 3, 6846, 6846]])
I have found the error, my file were not all of the same length. This caused a situation where I accessed empty element. I have added a loop that add zeros at the end of each list till I get the same length. Schuh, noted that adding zero at the end might result in getting wrong std. This is not the case in my data but this should be noted.
I have set of value in float (always less than 0). Which I want to bin into histogram,
i,e. each bar in histogram contain range of value [0,0.150)
The data I have looks like this:
0.000
0.005
0.124
0.000
0.004
0.000
0.111
0.112
Whith my code below I expect to get result that looks like
[0, 0.005) 5
[0.005, 0.011) 0
...etc..
I tried to do do such binning with this code of mine.
But it doesn't seem to work. What's the right way to do it?
#! /usr/bin/env python
import fileinput, math
log2 = math.log(2)
def getBin(x):
return int(math.log(x+1)/log2)
diffCounts = [0] * 5
for line in fileinput.input():
words = line.split()
diff = float(words[0]) * 1000;
diffCounts[ str(getBin(diff)) ] += 1
maxdiff = [i for i, c in enumerate(diffCounts) if c > 0][-1]
print maxdiff
maxBin = max(maxdiff)
for i in range(maxBin+1):
lo = 2**i - 1
hi = 2**(i+1) - 1
binStr = '[' + str(lo) + ',' + str(hi) + ')'
print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))
~
When possible, don't reinvent the wheel. NumPy has everything you need:
#!/usr/bin/env python
import numpy as np
a = np.fromfile(open('file', 'r'), sep='\n')
# [ 0. 0.005 0.124 0. 0.004 0. 0.111 0.112]
# You can set arbitrary bin edges:
bins = [0, 0.150]
hist, bin_edges = np.histogram(a, bins=bins)
# hist: [8]
# bin_edges: [ 0. 0.15]
# Or, if bin is an integer, you can set the number of bins:
bins = 4
hist, bin_edges = np.histogram(a, bins=bins)
# hist: [5 0 0 3]
# bin_edges: [ 0. 0.031 0.062 0.093 0.124]
from pylab import *
data = []
inf = open('pulse_data.txt')
for line in inf:
data.append(float(line))
inf.close()
#binning
B = 50
minv = min(data)
maxv = max(data)
bincounts = []
for i in range(B+1):
bincounts.append(0)
for d in data:
b = int((d - minv) / (maxv - minv) * B)
bincounts[b] += 1
# plot histogram
plot(bincounts,'o')
show()
The first error is:
Traceback (most recent call last):
File "C:\foo\foo.py", line 17, in <module>
diffCounts[ str(getBin(diff)) ] += 1
TypeError: list indices must be integers
Why are you converting an int to a str when a str is needed? Fix that, then we get:
Traceback (most recent call last):
File "C:\foo\foo.py", line 17, in <module>
diffCounts[ getBin(diff) ] += 1
IndexError: list index out of range
because you've only made 5 buckets. I don't understand your bucketing scheme, but let's make it 50 buckets and see what happens:
6
Traceback (most recent call last):
File "C:\foo\foo.py", line 21, in <module>
maxBin = max(maxdiff)
TypeError: 'int' object is not iterable
maxdiff is a single value out of your list of ints, so what is max doing here? Remove it, now we get:
6
Traceback (most recent call last):
File "C:\foo\foo.py", line 28, in <module>
print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))
TypeError: argument 2 to map() must support iteration
Sure enough, you're using a single value as the second argument to map. Let's simplify the last two lines from this:
binStr = '[' + str(lo) + ',' + str(hi) + ')'
print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))
to this:
print "[%f, %f)\t%r" % (lo, hi, diffCounts[i])
Now it prints:
6
[0.000000, 1.000000) 3
[1.000000, 3.000000) 0
[3.000000, 7.000000) 2
[7.000000, 15.000000) 0
[15.000000, 31.000000) 0
[31.000000, 63.000000) 0
[63.000000, 127.000000) 3
I'm not sure what else to do here, since I don't really understand the bucketing you are hoping to use. It seems to involve binary powers, but isn't making sense to me...