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.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
I have written the following program in Python:
s = []
for e in random_key:
s = str(e)
print(s)
where the list random_key is
random_key = ['0011111011100101', '0000010111111011', '0011100110110100',
'1000010101010010', '0011001011001111', '1101101101110011',
'1100001111111011', '0000100000110100', '0101111010100101',
'1001100101100001']
The output of the program is
1111011010110011
1011000110011100
0011011001100010
0000011100100001
1111111010000100
0110110101100011
1011100011000101
1011101011100010
1101101101001010
1000011110110000
which is not correct. How can I fix the code?
If I am able to read your thoughts (not sure about that ..). Would you like them to 10 based numbers?
random_key = ['0011111011100101', '0000010111111011', '0011100110110100',
'1000010101010010', '0011001011001111', '1101101101110011',
'1100001111111011', '0000100000110100', '0101111010100101',
'1001100101100001']
numbers = [int(x, 2) for x in random_key]
print(numbers)
output
[16101, 1531, 14772, 34130, 13007, 56179, 50171, 2100, 24229, 39265]
Do you mean this?
s = list()
for e in random_key:
s.append(str(e))
print(s)
Returns:
['0011111011100101', '0000010111111011', '0011100110110100', '1000010101010010', '0011001011001111', '1101101101110011', '1100001111111011', '0000100000110100', '0101111010100101', '1001100101100001']
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
the image variable comes from a text file (containing numbers, which i assume that they are a string in python). So what was i'm trying to do is to typecast the "pixel" variable (which is string elements from the text file) to an integer so that I could divide them. but the error in the title above shows. Is there a way to typecast it with it staying in the index? Thanks!
def getHist (image, bins):
hist = [0 for e in range(0,bins)]
interval = 256//bins
for row in image:
for pixel in row:
hist[int(pixel)//interval] += 1
return hist
def printHist (hist):
for i in range(0, len(hist)):
print(i+1,"-", hist[i], "\t- ", end="")
for j in range(0, hist[i]):
print("|", end="")
print()
def readImage (filename):
image = []
fileHandle = open(filename, "r")
for line in fileHandle:
image_row = line[0:-1].split(",")
image_row[-1] = image_row[-1][0:len(line)].split("\n")
image.append(image_row)
fileHandle.close()
return image
image = readImage("image.txt")
hist = getHist("image.txt",8)
printHist(hist)
Here is an example for the image parameter (from a text file):
212,16,142,183,92,211,0,221,54,226
227,56,137,252,140,241,1,3,153,51
157,144,121,99,17,185,125,27,76,129
49,55,81,220,194,8,62,179,96,142
74,178,80,24,2,34,247,177,244,82
93,117,154,152,35,224,38,70,193,52
181,61,45,141,163,222,160,168,203,104
234,114,244,53,252,48,66,7,218,95
49,189,18,31,184,207,53,141,148,188
238,6,104,189,244,132,28,92,147,123
where each row, each element is the pixel
the file loaded by a different function on my program, but yeah. i'm trying to convert each pixel to a int so that i could divide it with another int.
Replace
histogram = getHistogram("image.txt",8)
with
histogram = getHistogram(image, 8)
Otherwise you're iterating over the filename instead of the data from the file!
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
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: