Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I get an error 'list index out of range' but after checking the code I can't understand what's wrong.
Here are the data:
import numpy as np
proba_ex=[np.array([[0.00639649, 0.00251385, 0.00729689, 0.007919488, 0.00368546,
0.00068663],
[0.08320138, 0.04170561, 0.04755181, 0.42204733, 0.15220323,
0.10432409]]),np.array([[0.14774831, 0.09566049, 0.30208335, 0.05015277, 0.10008666,
0.06229035],
[0.0573518 , 0.095365 , 0.11942169, 0.12236523, 0.21965485,
0.11497026]])]
pred_data_ex=[np.array(['class3','class4']),np.array(['class1','class7'])]
Below is the code, what it does is:
If the condition with the comment below gives a result less than 0.1 it should replace the pred_data_ex with the word 'UNK'.
for x in proba_ex:
pt=0.1
for z in pred_data_ex:
sproba=[]
for i,p in enumerate(z):
xa=-np.sort(-proba_ex[i])
if xa[0][0]-xa[0][1]<pt: #here is the condition
pred_data_ex[i]=u'<UNK>'
When I run it I get:
IndexError Traceback (most recent call last)
<ipython-input-21-fb2d5516c0db> in <module>
6
7 for i,p in enumerate(z):
----> 8 xa=-np.sort(-proba_ex[i])
9 print(xa)
10 # sproba.append(xa)
IndexError: list index out of range
EDIT:
The end result should look like this for example:
The output of pred_data_ex should be something like:
[np.array(['UNK','class4']),np.array(['class1','UNK'])]
as you can see it replaces based on the condition described, the word 'UNK' when needed.
EDIT 2
explanation
#Yossi from your code the result is the following:
xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
[0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
[0.21965485 0.12236523 0.11942169 0.11497026 0.095365 0.0573518 ]]
xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
[0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
[0.21965485 0.12236523 0.11942169 0.11497026 0.095365 0.0573518 ]]
xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
[0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
[0.21965485 0.12236523 0.11942169 0.11497026 0.095365 0.0573518 ]]
xa: [[0.00791949 0.00729689 0.00639649 0.00368546 0.00251385 0.00068663]
[0.42204733 0.15220323 0.10432409 0.08320138 0.04755181 0.04170561]]
xa: [[0.30208335 0.14774831 0.10008666 0.09566049 0.06229035 0.05015277]
[0.21965485 0.12236523 0.11942169 0.11497026 0.095365 0.0573518 ]]
[array(['<UNK>', 'class4'], dtype='<U6'), array(['<UNK>', 'class7'], dtype='<U6')]
It looks that the result is incorrect because if you check the calculations it should give this:
[np.array(['UNK','class4']),np.array(['class1','UNK'])]
because:
0.00791949 -0.00729689<0.1
True (which makes it take the 'UNK' value)
0.42204733 -0.15220323<0.1
False
0.30208335 -0.14774831<0.1
False
0.21965485- 0.12236523<0.1
True (which makes it take the 'UNK' value)
therefore it should be:
[np.array(['UNK','class4']),np.array(['class1','UNK'])]
your result gives:
[array(['<UNK>', 'class4'], dtype='<U6'), array(['<UNK>', 'class7'], dtype='<U6')]
What could be the issue?
after some debugging I found it, You change the variable pred_data_ex inside the inner loop, such that in some point z is larger than 2 elements (You actually loop over a string with more than 2 characters, take a look at p).
That's why you access the proba_ex over the list size.
here is the additions that made me figure it out (several prints), take a look at the value of z, p and i along the loop's iterations:
import numpy as np
proba_ex=[np.array([[0.00639649, 0.00251385, 0.00729689, 0.007919488, 0.00368546,
0.00068663],
[0.08320138, 0.04170561, 0.04755181, 0.42204733, 0.15220323,
0.10432409]]),np.array([[0.14774831, 0.09566049, 0.30208335, 0.05015277, 0.10008666,
0.06229035],
[0.0573518 , 0.095365 , 0.11942169, 0.12236523, 0.21965485,
0.11497026]])]
pred_data_ex=[np.array(['class3','class4']),np.array(['class1','class7'])]
for x in proba_ex:
pt=0.1
for j,z in enumerate(pred_data_ex):
sproba=[]
if not isinstance(z,str):
for i,p in enumerate(z):
xa=-np.sort(-proba_ex[i])
print("xa: " + str(xa))
if xa[0][0]-xa[0][1]<pt: #here is the condition
pred_data_ex[j][i]=u'<UNK>'
print(pred_data_ex)
the difference between first and second element after sorting proba_ex is larger than 0.1 for the second array and smaller for the first array. which correspondly to your code mean that the first element of each dual elements in pred-data_ex will replaced to '' as you wish, not as you said that expected.
my output:
[array(['<UNK>', 'class4'], dtype='<U6'), array(['<UNK>', 'class7'], dtype='<U6')]
which make sense as long as the first array feet the condition and the second don't
Related
I am new in using python. I tried to write a code for my optimization research model. Could you help me please? I don't know what's wrong with the code. I am using python 2 by the way. Thank you.
for i in range(len(lift)):
prob+=lpSum(dec_var[i])<=1
#constraints
col_con=[1,0,0,2,2,3,1,1]
dec_var=np.array(dec_var)
col_data=[]
for j in range(len(brands)):
col_data.append(list(zip(*dec_var)[j]))
prob+=lpSum(col_data[j])<=col_con[j]
#problem
prob.writeLP("SO.lp")
#solve the problem
prob.solve()
print("The maximum Total lift obtained is:",value(prob.objective)) # print the output
#print the decision variable output matrix
Matrix=[[0 for X in range(len(lift[0]))] for y in range(len(lift))]
for v in prob.variables():
Matrix[int(v.name.split("_")[2])][int(v.name.split("_")[3])]=v.varValue
matrix=np.int_(Matrix)
print ("The decision variable matrix is:")
print(matrix)
the error was :
TypeError Traceback (most recent call
last) in
13 for j in range(len(brands)):
14
---> 15 col_data.append(list(zip(*dec_var)[j]))
16
17 prob+=lpSum(col_data[j])<=col_con[j]
TypeError: 'zip' object is not subscriptable
Your code breaks in this line:
col_data.append(list(zip(*dec_var)[j]))
Lets go through it step by step:
dec_var is an array, probably with multiple dimensions. Something like this:
dec_var=np.array([[1,2,3,4],[5,6,7,8]])
dec_var
#array([[1, 2, 3, 4],
# [5, 6, 7, 8]])
The star operator (*) breaks the array into 'variables'. Something more or less like this:
a = [1,2,3,4], b = [5,6,7,8].
('a' and 'b' don't really exist, just trying to paint a picture).
Next, you apply zip(), which allows you to iterate two iterables together. You would usually use it like this:
for i,j in zip([1,2],[3,4]):
print(i,j)
#1,3
#2,4
However, zip itself is not subscriptable, that is the error you are getting.
To make it subscriptable, you can apply list on it.
list(zip([1,2],[3,4]))[0]
#(1,3)
In other words.. The solution to your problem most likely is changing the position of the [j] subscript:
From:
col_data.append(list(zip(*dec_var)[j]))
To:
col_data.append(list(zip(*dec_var))[j])
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to understanding what is happening with my code but I couldn't obtain a solution.
Here is my code:
multipolygon = ((
(-58.89198482 -13.38147202, -58.89189251 -13.38147271, -58.89189321 -13.38156309, -58.89198552 -13.3815624,
-58.89207783 -13.38156171, -58.89217014 -13.38156102, -58.89226245 -13.38156032, -58.89235476 -13.38155963,
-58.89244706 -13.38155894, -58.89253937 -13.38155825, -58.89263168 -13.38155756, -58.89263097 -13.38146719,
-58.89253867 -13.38146788, -58.89244636 -13.38146857, -58.89235405 -13.38146926,
-58.89226174 -13.38146995, -58.89216943 -13.38147064, -58.89207712 -13.38147133,
-58.89198482 -13.38147202)
),
(
(-58.89484849 -13.38172171, -58.89484919 -13.38181209, -58.8949415 -13.38181139,
-58.89503381 -13.3818107, -58.89512612 -13.38181001, -58.89521843 -13.38180932,
-58.89521772 -13.38171894, -58.89531003 -13.38171825, -58.89540234 -13.38171756,
-58.89540163 -13.38162718, -58.89540092 -13.38153681, -58.89540022 -13.38144644,
-58.89530791 -13.38144713, -58.8952156 -13.38144782, -58.89512329 -13.38144851,
-58.89503098 -13.3814492, -58.89493868 -13.3814499, -58.89484637 -13.38145059,
-58.89484707 -13.38154096, -58.89484778 -13.38163134, -58.89484849 -13.38172171)
)
)
for poly in multipolygon:
print(poly)
The problem is when I print my multipolygon, the values are changing.
Here is the output:
(-72.27345684, -72.27336522, -72.27345629999999, -72.27354792, -72.27363954, -72.27373116, -72.27382277, -72.27391439, -72.274006, -72.27409762, -72.27418924, -72.27409816, -72.27400655, -72.27391493, -72.27382331, -72.27373169, -72.27364007, -72.27354845, -72.27345684)
(-72.27657020000001, -72.27666128, -72.27675289, -72.27684451, -72.27693613, -72.27702775, -72.27693666, -72.27702828, -72.2771199, -72.27702881, -72.27693773, -72.27684666, -72.27675504, -72.27666342, -72.2765718, -72.27648018000001, -72.27638858, -72.27629696, -72.27638802999999, -72.27647912, -72.27657020000001)
Why this is happening?
It looks like you're missing commas in the tuples which causes some values to be subtracted
From your code:
-58.89484849 -13.38172171
I'm assuming you mean for it to be -58.89484849, -13.38172171?
otherwise -58.89484849 -13.38172171 = 72.2765702 which matches the printed values
It does math since this is what you have inside your data structure
-58.89198482 -13.38147202 = -72.27345684
What is happening is that you are subtracting (-58.89189251 -13.38147271) before printing
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to use multiple conditions inside an if-else expression which is as:
from datetime import date, timedelta as td, datetime
import holidays
st_dt = '1/1/2017'
en_dt = '1/5/2017'
st_year = datetime.strptime(st_dt, "%m/%d/%Y").year
en_year = datetime.strptime(en_dt, "%m/%d/%Y").year
st_mnth = datetime.strptime(st_dt, "%m/%d/%Y").month
en_mnth = datetime.strptime(en_dt, "%m/%d/%Y").month
st_date = datetime.strptime(st_dt, "%m/%d/%Y").day
en_date = datetime.strptime(en_dt, "%m/%d/%Y").day
d1 = datetime(st_year, st_mnth, st_date, 0, 0, 0)
d2 = datetime(en_year, en_mnth, en_date, 0, 0, 0)
AllHours = []
i = 0
while(d1<=d2):
AllHours.append(d1)
d1 = d1 + td(hours=1)
us_holidays = holidays.UnitedStates()
HolidayWorkingDay = ['H' if x in us_holidays else 'W' for x in AllHours]
HE = [x.hour for x in AllHours]
DayDefn = ['Type1' if (x == 'H' and y>=7 and y<=23) else 'Type2' for x in HolidayWorkingDay and for y in HE]
So, in the above I am trying to make sure that if certain location in the lists HolidayWorkingDay and HE meet certain criteria I give them name 'Type1' else they are 'Type2'
But it fails on the last line because of bad syntax. I am not sure what the right way to write multiple expression is
If you want nested loops you should write last list comprehension like this (without and):
DayDefn = ['Type1' if (x == 'H' and y>=7 and y<=23) else 'Type2'
for x in HolidayWorkingDay for y in HE]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am solving the following equation:
wf=1110
wt=647
wp=119000
c=300000000
e0=10983849
e1 =e0-(wp**2/(w*2+wt))
And I want "w" as a variable like from 1 to 1000 or whatever I want in a rising scale.
after I want a plot of e1 vs w
does anyone know how to do that?
One way to do this:
def solve_equation(start, end, equation):
for x in xrange(start, end):
yield equation(x)
def my_equation(x):
wt=647
wp=119000
e0=10983849
e1 =e0-(wp**2/(w*2+wt))
return e1
print solve_equation(0, 100, my_equation)
print solve_equation(500, 1000, my_equation)
This splits the solving of the equation for a range from the equation itself.
You could also look into numpy, if you are going to be doing this on a large scale. Then you would create a numpy array of the values for w and calculate them as a vector. This would boil your code down to something like this:
from numpy import arange
wt=647
wp=119000
e0=10983849
w = arange(0, 100)
e = e0-(wp**2/(w*2+wt))
This would set w to array([0, 1, 2, ..., 98, 99]) and e to:
array([-10903322, -10835873, -10768839, -10702215, -10635998, -10570184,
-10504770, -10439751, -10375125, -10310887, -10247035, -10183565,
-10120472, -10057755, -9995410, -9933433, -9871821, -9810570,
-9749679, -9689143, -9628960, -9569126, -9509638, -9450494,
-9391690, -9333224, -9275092, -9217292, -9159820, -9102675,
-9045853, -8989352, -8933169, -8877301, -8821745, -8766499,
-8711561, -8656927, -8602596, -8548564, -8494830, -8441391,
-8388244, -8335387, -8282817, -8230533, -8178532, -8126812,
-8075370, -8024204, -7973312, -7922693, -7872342, -7822259,
-7772442, -7722888, -7673595, -7624560, -7575784, -7527262,
-7478993, -7430975, -7383206, -7335685, -7288409, -7241376,
-7194584, -7148033, -7101719, -7055641, -7009797, -6964186,
-6918805, -6873654, -6828729, -6784030, -6739555, -6695302,
-6651269, -6607455, -6563858, -6520477, -6477310, -6434355,
-6391611, -6349076, -6306749, -6264628, -6222712, -6180999,
-6139488, -6098177, -6057065, -6016151, -5975432, -5934908,
-5894577, -5854438, -5814490, -5774730])
Using the excellent IPython Notebook with pylab, you can then just do:
plot(w, e)
and have a nice graph showing the result:
Plotting up to 10000 will result in a graph like this:
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used this code to generate xyz coordinates.
from random import *
uniqcoord = [7.63, 28.05, 66.36] # my file contains 100 such list of points
for i in range(10):
i = i + 1
x,y,z = uniqcoord
x1,y1,z1 = (uniform(x[0]-3.5,x[0]+3.5), uniform(y[1]-3.5,y[1]+3.5), uniform(z[2]-3.5,z[2]+3.5))
print i, '\t', x1,y1,z1
When i run this program its showing error.
when i run this program with hole numbers it work.
how to resolve????
Your x, y, z are float, not list. So your can't have x[0] and such. You will get a no attribute or typeerror for that.
You're incorrectly using x,y,z in the x1,y1,z1 line. See what I've done below;
from random import *
uniqcoord = [7.63, 28.05, 66.36] # my file contains 100 such list of points
for i in range(10):
i=i+1
x,y,z = uniqcoord
x1,y1,z1 = (uniform(uniqcoord[0]-3.5,uniqcoord[0]+3.5), uniform(uniqcoord[1]-3.5,uniqcoord[1]+3.5), uniform(uniqcoord[2]-3.5,uniqcoord[2]+3.5))
print i, '\t', x1,y1,z1
x[0] does not exist, but uniqcoord[0] = x because uniqcoord = [x,y,z], and so on.
Result:
1 5.86941266341 29.4004245806 67.1323961576
2 6.38143060206 29.7045813689 69.4867869245
3 5.55280335095 29.9472835241 63.7388152633
4 10.5607637875 26.6269381673 69.5256503601
5 7.29826364813 28.5740308696 65.2122276564
6 8.24134391937 30.880058802 69.8445734597
7 10.246919304 27.9240839326 64.9480054046
8 8.26957559527 28.5700768795 63.996117793
9 5.88677020227 30.0621250245 63.7431176092
10 8.98100830174 27.3378753286 63.1329446911
I think this is what you are looking for.