I have three 2D arrays SandArray,ClayArray, and SiltArray. I also have a function described here. Below is my code, when I run the script I get a ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
def TextureClass(sand, clay, silt):
#if sand + clay > 100 or sand < 0 or clay < 0:
# raise Exception('Inputs adds over 100% or are negative')
if silt + 1.5*clay < 15:
textural_class = 'sand'
elif silt + 1.5*clay >= 15 and silt + 2*clay < 30:
textural_class = 'loamy sand'
elif (clay >= 7 and clay < 20 and sand > 52 and silt + 2*clay >= 30) or (clay < 7 and silt < 50 and silt + 2*clay >= 30):
textural_class = 'sandy loam'
elif clay >= 7 and clay < 27 and silt >= 28 and silt < 50 and sand <= 52:
textural_class = 'loam'
elif (silt >= 50 and clay >= 12 and clay < 27) or (silt >= 50 and silt < 80 and clay < 12):
textural_class = 'silt loam'
elif silt >= 80 and clay < 12:
textural_class = 'silt'
elif clay >= 20 and clay < 35 and silt < 28 and sand > 45:
textural_class = 'sandy clay loam'
elif clay >= 27 and clay < 40 and sand > 20 and sand <= 45:
textural_class = 'clay loam'
elif clay >= 27 and clay < 40 and sand <= 20:
textural_class = 'silty clay loam'
elif clay >= 35 and sand > 45:
textural_class = 'sandy clay'
elif clay >= 40 and silt >= 40:
textural_class = 'silty clay'
elif clay >= 40 and sand <= 45 and silt < 40:
textural_class = 'clay'
else:
textural_class = 'na'
return textural_class
Texture = TextureClass(SandArray,ClayArray,SiltArray)
Texture should be an array with the same shape as SandArray, ClayArray, and SiltArray but with the textural_class str as its values.
Is it possible to have an output array of text from a function having conditions and using arrays as its input arguments and if so, what am I missing?
Edit:
Having tried texture = np.array(list(map(TextureClass,SandArray,ClayArray,SiltArray))) I still get the same ValueError
I'm not really that knowledgeable in numpy, but, from what I searched in other questions, you could use vectorize to wrap your function.
Here's an example: How to apply a function / map values of each element in a 2d numpy array/matrix?
Using my previous approach of mostly built-in python-code:
You could zip the three arrays (either inside or outside your function, but I'd do it outside), then loop over the zipped arrays.
The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.
If the passed iterators have different lengths, the iterator with the least items decides the length of the new iterator.
Emphasis on the fact that it's assumed the three arrays have the same length. And, in this case, you'd need to bi-dimensionally zip:
So, instead of Texture = TextureClass(SandArray,ClayArray,SiltArray), you could use:
soilCompositions = (zip(sands, clays, silts) for sands, clays, silts in zip(SandArray, ClayArray, SiltArray))
Textures = ((TextureClass(sand, clay, silt) for sand, clay, silt in soilCompositionRow) for soilCompositionRow in soilCompositions)
Notice that I used generator comprehension, but you could just as easy use list comprehension instead:
soilCompositions = (zip(sands, clays, silts) for sands, clays, silts in zip(SandArray, ClayArray, SiltArray))
Textures = [[TextureClass(sand, clay, silt) for sand, clay, silt in soilCompositionRow] for soilCompositionRow in soilCompositions]
Applying a function to each element of a matrix requires np.vectorize. The documentation is available here. An example of a similar questions can be found here:How to apply a function / map values of each element in a 2d numpy array/matrix?.
I think this question is unique in that it shows the range of functions that np.vectorize works on. My original issue was whether or not np.vectorize would work for a conditional function like the one in my question.
def TextureClass(sand, clay, silt):
#if sand + clay > 100 or sand < 0 or clay < 0:
# raise Exception('Inputs adds over 100% or are negative')
if silt + 1.5*clay < 15:
textural_class = 'sand'
elif silt + 1.5*clay >= 15 and silt + 2*clay < 30:
textural_class = 'loamy sand'
elif (clay >= 7 and clay < 20 and sand > 52 and silt + 2*clay >= 30) or (clay < 7 and silt < 50 and silt + 2*clay >= 30):
textural_class = 'sandy loam'
elif clay >= 7 and clay < 27 and silt >= 28 and silt < 50 and sand <= 52:
textural_class = 'loam'
elif (silt >= 50 and clay >= 12 and clay < 27) or (silt >= 50 and silt < 80 and clay < 12):
textural_class = 'silt loam'
elif silt >= 80 and clay < 12:
textural_class = 'silt'
elif clay >= 20 and clay < 35 and silt < 28 and sand > 45:
textural_class = 'sandy clay loam'
elif clay >= 27 and clay < 40 and sand > 20 and sand <= 45:
textural_class = 'clay loam'
elif clay >= 27 and clay < 40 and sand <= 20:
textural_class = 'silty clay loam'
elif clay >= 35 and sand > 45:
textural_class = 'sandy clay'
elif clay >= 40 and silt >= 40:
textural_class = 'silty clay'
elif clay >= 40 and sand <= 45 and silt < 40:
textural_class = 'clay'
else:
textural_class = 'na'
return textural_class
vector_func = np.vectorize(TextureClass)
textures = vector_func(SandArray, ClayArray, SiltArray)
Related
I'm trying to code the question in the image above but I only know how to use if else and for, while loop.
Can't figure out how to use a loop for this so I tried if else, but ended up with 100+ lines just doing height < 155.
Can it be done using a loop?
This is my code, I stopped at height<155 due to the length:
height = float(input('Enter height measurement (cm): '))
chest = float(input('Enter Chest Measurement (cm): '))
waist = float(input('Enter waist measurement (cm): '))
if height < 155:
if chest < 80:
if waist < 70:
print('You are size 1 (best fit)')
elif chest >= 80 and chest < 88:
if waist <70:
print('You are size 2 (Regular fit)')
elif waist >= 76 and waist < 84:
print('You are size 3 (Relaxed fit)')
elif waist >= 84 and waist < 92:
print('You are size 4 (Relaxed fit)')
elif waist >= 92 and waist < 100:
print('You are size 5 (Relaxed fit)')
elif waist >= 100 and waist < 108:
print('You are size 6 (Relaxed fit)')
elif waist >= 108 and waist < 116:
print('You are size 7 (Relaxed fit)')
elif waist >= 116:
print('You are size 8 (Relaxed fit)')
elif chest >= 88 and chest < 96:
if waist < 70:
print('You are size 3 (Regular fit)')
elif waist >= 84 and waist < 92:
print('You are size 4 (Relaxed fit)')
elif waist >= 92 and waist < 100:
print('You are size 5 (Relaxed fit)')
elif waist >= 100 and waist < 108:
print('You are size 6 (Relaxed fit)')
elif waist >= 108 and waist < 116:
print('You are size 7 (Relaxed fit)')
elif waist >= 116:
print('You are size 8 (Relaxed fit)')
elif chest >= 96 and chest < 104:
if waist < 70:
print('You are size 4 (Regular fit)')
elif waist >= 92 and waist < 100:
print('You are size 5 (Relaxed fit)')
elif waist >= 100 and waist < 108:
print('You are size 6 (Relaxed fit)')
elif waist >= 108 and waist < 116:
print('You are size 7 (Relaxed fit)')
elif waist >= 116:
print('You are size 8 (Relaxed fit)')
elif chest >= 104 and chest < 112:
if waist < 70:
print('You are size 5 (Regular fit)')
elif waist >= 100 and waist < 108:
print('You are size 6 (Relaxed fit)')
elif waist >= 108 and waist < 116:
print('You are size 7 (Relaxed fit)')
elif waist >= 116:
print('You are size 8 (Relaxed fit)')
elif chest >= 112 and chest < 120:
if waist < 70:
print('You are size 6 (Regular fit)')
elif waist >= 108 and waist < 116:
print('You are size 7 (Relaxed fit)')
elif waist >= 116:
print('You are size 8 (Relaxed fit)')
elif chest >= 120 and chest < 128:
if waist < 70:
print('You are size 7 (Regular fit)')
elif waist >= 116:
print('You are size 8 (Relaxed fit)')
elif chest >= 128:
if waist >=116:
print('You are size 8 (Regular fit)')
else:
print('You are size 8 (Relaxed fit)')
When you look at the table, you can see a regular pattern. For instance, the height ranges are all of size 5 (155-160, 160-165, ...), except the "catch-all" at either extreme.
So you can calculate the corresponding size with a formula. For instance for height, we can first subtract 145 and then divide by 5. Then convert anything that is less than 1 to 1, and anything that is greater than 8 to 8.
For the chest measure, the steps are 8 wide. Similarly for the waist measure, the steps are 8 wide, but there is one exception: the waist range for size 2 is 70-76 and thus only spans 6 units, not 8. It would have been nice if that range had been 68-76. So there is special care needed to deal with that irregularity, so that 68 and 69 will translate to waist size 1 and not to size 2.
Secondly, the fit can be derived as follows:
Sort the three sizes (for height, chest and waist) and get the greatest value: that is the size to report. Then find the first occurrence of that size in the sorted list of sizes. If it is found at index 0, then the fitting is "best"; if it is found at index 1, the fitting is "regular", and if found at index 2, then it is "relaxed".
Here is the code for it:
height = float(input('Enter height measurement (cm): '))
chest = float(input('Enter Chest Measurement (cm): '))
waist = float(input('Enter waist measurement (cm): '))
height_size = min(8, max(1, int(height - 145) // 5))
chest_size = min(8, max(1, int(chest - 64) // 8))
waist_size = min(8, max(1, int(waist - 52) // 8))
if waist < 70:
waist_size = 0 # Deal with an irregularity for waist measures 69 and 68.
sizes = sorted([height_size, chest_size, waist_size])
size = sizes[-1] # Greatest
fitting = ["best", "regular", "relaxed"][sizes.index(size)]
print(f"You are size {size} ({fitting} fit)")
I am trying to build a cost model using mostly if statements but I keep getting the error:
NameError: name 'spread_1' is not defined.
I am a beginner with python so I don't know if I've done something incorrect. Please help.
#water depth of project site in metres
water_depth = 100
#platform wells require jackups
pl = 1
#1-2 complexity required LWIV
ss_simple = 1
#3-4 complexity requires rig
ss_complex = 0
#day rates
vjackup = 75000
jackup = 90000
vsemi = 170000
semi = 300000
lwiv = 200000
#determining vessel spread for platform wells
if pl >= 1:
if water_depth == range(0, 50):
spread_1 = vjackup * 24.1
elif water_depth == range(51, 150):
spread_1 = jackup * 24.1
elif pl == 0:
spread_1 = 0
You should replace the == with in at the
if water_depth == range(0, 50):
and
elif water_depth == range(51, 150):
making your block of code:
if pl >= 1:
if water_depth == range(0, 50):
spread_1 = vjackup * 24.1
elif water_depth == range(51, 150):
spread_1 = jackup * 24.1
elif pl == 0:
spread_1 = 0
into
if pl >= 1:
if water_depth in range(0, 50):
spread_1 = vjackup * 24.1
elif water_depth in range(51, 150):
spread_1 = jackup * 24.1
elif pl == 0:
spread_1 = 0
But it would be more practical to use extreme equality operators in your case:
if pl >= 1:
if 0 <= water_depth < 50:
spread_1 = vjackup * 24.1
elif 51 <= water_depth < 150:
spread_1 = jackup * 24.1
elif pl == 0:
spread_1 = 0
Do note that the range() function omits the end value, so range(0, 50)'s last value would be 49, not 50.
why does my code return wrong ticket prices? I am supposed to add a time factor as well, but can't get even this to work. This is what I am supposed to do:
"""
Price of one bus ticket
time 6-17, price 2.7, age 16-64
time 18-22, price 3.5, age 16-64
time 23 and 0-5, price 4, age 16-64
for ages 0-2 ticket is free at all times
time 6-17, price 1.7, ages 3-15 and 65 -->
time 18-22, price 2.5, ages 3-15 and 65 -->
time 23 and 0-5, price 3.0, ages 3-15 and 65 -->
"""
def calculate_ticket_price(age):
ticket_price = 0
while True:
if age >= 0 or age <= 2:
ticket_price = 1.0
if age <= 15 or age >= 3 or age >= 65:
ticket_price = 1.5
if age > 15 or age < 65:
ticket_price = 2.7
return float(ticket_price)
def main():
age = 5
price = calculate_ticket_price(age)
print(price)
if __name__ == '__main__':
main()
I think it’ll return the wrong price cause you’re using or where you need an and.
Your first if statement should be:
if ((age >= 0) and (age <= 2)):
Your second if statement should be:
if (((age <= 15) and (age >= 3)) or (age >= 65)):
Then your third one:
if ((age > 15) and (age < 65)):
def calculate_ticket_price(age, time):
while True:
if time >= 6 or time <= 17 and age > 15 or age < 65:
ticket_price = 2.7
elif time >= 18 or time <= 22 and age > 15 or age < 65:
ticket_price = 3.5
elif time >= 23 or time >= 0 or time <= 5 and age > 15 or age < 65:
ticket_price = 4.0
elif time >= 6 or time <= 17 and age <= 15 or age >= 3 or age >= 65:
ticket_price = 1.7
elif time >= 18 or time <= 22 and age <= 15 or age >= 3 or age >= 65:
ticket_price = 2.5
elif time >= 23 or time >= 0 or time <= 5 and age <= 15 or age >= 3 or age >= 65:
ticket_price = 3.0
else:
ticket_price = 0.0
return float(ticket_price)
def main():
age = 5
time = 12
price = calculate_ticket_price(age, time)
print(price)
if __name__ == '__main__':
main()
Made these edits. Should there be and between every >=, <= etc..?
You're using or when I think you want to be using and. For example, this condition:
if age >= 0 or age <= 2:
is going to be true for any positive number, since the first part will always match.
You also want to be using elif so that only one of these blocks will happen. Your last condition:
if age > 15 or age < 65:
ticket_price = 2.7
is going to happen any time the age is under 65 or over 15 (which is going to be every number), so I'd expect that your function just always returns 2.7.
A simpler way to write this function that follows the simple age-only rules you're trying to implement would be:
def calculate_ticket_price(age: int) -> float:
if age <= 2:
return 0.0 # infant price
elif age <= 15:
return 1.5 # youth price
elif age <= 65:
return 2.7 # adult price
else:
return 1.5 # senior price
In this very simple example, only the first condition that matches will return a value, so testing both sides of the range isn't necessary.
You can also check for an age to be within a particular range by writing an expression like 2 < age <= 15, or age > 2 and age < 15, or even age in range(2, 16).
Note that putting everything inside a while loop serves no purpose at all -- avoid having lines of code that don't do anything useful, since they're just one more place for bugs to appear. :)
As far as having the function account for both age and time, I notice that the fare table amounts to giving youth/seniors the same $1 discount regardless of what time it is, so I might simplify it down like this rather than have a different condition for each age/time combination:
def calculate_ticket_price(time: int, age: int) -> float:
# Infants ride free
if age <= 2:
return 0.0
# Youth and seniors get a $1.00 discount
discount = 1.0 if age <= 15 or age >= 65 else 0.0
if 6 <= time <= 17:
return 2.7 - discount
if 18 <= time <= 22:
return 3.5 - discount
if 0 <= time <= 5 or time == 23:
return 4.0 - discount
raise ValueError(f"invalid time {time}!")
I need to group by pandas with weight
Name weight(kg)
Person1 4.44
Person2 37.3
Person3 36.38
Person4 39.52
Person5 81.57
Person6 43.55
Person7 91.11
Person8 5
Person9 36.48
Person10 38.25
My code is below. Need to group by according to if condition. My code is below. But I am getting 0 to 20 for all the rows.
if 0 <= data_file['weight(kg)'].all() < 20:
data_file['target'] = "0 to 20%"
if 20 < data_file['weight(kg)'].all() < 40:
data_file['target'] = "20 to 40%"
if 40 < data_file['weight(kg)'].all() < 60:
data_file['target'] = "40 to 60%"
if 60 < data_file['weight(kg)'].all() < 80:
data_file['target'] = "60 to 80%"
if 80 < data_file['weight(kg)'].all() <= 100:
data_file['target'] = "80 to 100%"
Expected out
Name weight(kg) Target
Person1 4.44 0 to 20
Person2 37.3 20 to 40
Person3 36.38 20 to 40
Person4 39.52 20 to 40
Person5 81.57 80 to 100
Person6 43.55 40 to 60
Person7 91.11 80 to 100
Person8 5 0 to 20
Person9 36.48 20 to 40
Person10 38.25 20 to 40
Use pd.cut
df.assign(bins = pd.cut(df["weight(kg)"], [0,20,40,60,80,100],
labels=['0 to 20', '20 to 40', '40 to 60', '60 to 80', '80 to 100']))
result
Name weight(kg) bins
0 Person1 4.44 0 to 20
1 Person2 37.30 20 to 40
2 Person3 36.38 20 to 40
3 Person4 39.52 20 to 40
4 Person5 81.57 80 to 100
5 Person6 43.55 40 to 60
6 Person7 91.11 80 to 100
7 Person8 5.00 0 to 20
8 Person9 36.48 20 to 40
9 Person10 38.25 20 to 40
Very simple, just try using apply in pandas and lambda function:
def classify(x):
if 0 <= x < 20:
y = "0 to 20%"
if 20 < x < 40:
y = "20 to 40%"
if 40 < x < 60:
y = "40 to 60%"
if 60 < x < 80:
y = "60 to 80%"
if 80 < x <= 100:
y = "80 to 100%"
return y
the assuming your Dataframe has two columns 'Name' and 'Weight' we should type:
df['Target'] = df['weight'].apply(lambda x: classify(x))
I hope it helps
Extra:
if you want progress bar you can add these lines:
from tqdm import tqdm
tqdm.pandas()
df['Target'] = df['weight'].progress_apply(lambda x: classify(x))
You can do it using np.select
conditions = [
(0 <= df['weight(kg)']) & (df['weight(kg)'] < 20)
, (20 < df['weight(kg)']) & (df['weight(kg)'] < 40)
, (40 < df['weight(kg)']) & (df['weight(kg)'] < 60)
, (60 < df['weight(kg)']) & (df['weight(kg)'] < 80)
, (80 < df['weight(kg)']) & (df['weight(kg)'] <= 100)
]
results = [
"0 to 20%"
,"20 to 40%"
,"40 to 60%"
,"60 to 80%"
,"80 to 100%"
]
df['Target'] = np.select(conditions, results)
output:
Name weight(kg) Target
0 Person1 4.44 0 to 20%
1 Person2 37.30 20 to 40%
2 Person3 36.38 20 to 40%
3 Person4 39.52 20 to 40%
4 Person5 81.57 80 to 100%
I'm currently running Python 3.5 on Mac and I can't seem to find a way to be more efficient with my code.
In all I'm currently making a wheel of fortune game (or basically hangman), and I have it so that when the user clicks on a letter it checks if the letter is in the word etc.
Is there a better way than writing 26 if statements and checking coordinates each time? Is there a more efficient way of checking for coordinates using if statements?
while True:
mouse = win.getMouse()
if 35 < mouse.x < 65 and 85 < mouse.y < 115:
ans = "a"
break
elif 65 < mouse.x < 95 and 85 < mouse.y < 115:
ans = "b"
break
elif 95 < mouse.x < 125 and 85 < mouse.y < 115:
ans = "c"
break
elif 125 < mouse.x < 155 and 85 < mouse.y < 115:
ans = "d"
break
elif 155 < mouse.x < 185 and 85 < mouse.y < 115:
ans = "e"
break
elif 185 < mouse.x < 215 and 85 < mouse.y < 115:
ans = "f"
break
elif 215 < mouse.x < 245 and 85 < mouse.y < 115:
ans = "g"
break
elif 245 < mouse.x < 275 and 85 < mouse.y < 115:
ans = "h"
break
elif 275 < mouse.x < 305 and 85 < mouse.y < 115:
ans = "i"
break
elif 305 < mouse.x < 335 and 85 < mouse.y < 115:
ans = "j"
break
elif 35 < mouse.x < 65 and 115 < mouse.y < 145:
ans = "k"
break
elif 65 < mouse.x < 95 and 115< mouse.y < 145:
ans = "l"
break
elif 95 < mouse.x < 125 and 115 < mouse.y < 145:
ans = "m"
break
elif 125 < mouse.x < 155 and 115 < mouse.y < 145:
ans = "n"
break
elif 155 < mouse.x < 185 and 115 < mouse.y < 145:
ans = "o"
break
elif 185 < mouse.x < 215 and 115 < mouse.y < 145:
ans = "p"
break
elif 215 < mouse.x < 245 and 115 < mouse.y < 145:
ans = "q"
break
elif 245 < mouse.x < 275 and 115 < mouse.y < 145:
ans = "r"
break
elif 275 < mouse.x < 305 and 115 < mouse.y < 145:
ans = "s"
break
elif 305 < mouse.x < 335 and 115 < mouse.y < 145:
ans = "t"
break
elif 35 < mouse.x < 65 and 145 < mouse.y < 175:
ans = "u"
break
elif 65 < mouse.x < 95 and 145 < mouse.y < 175:
ans = "v"
break
elif 95 < mouse.x < 125 and 145 < mouse.y < 175:
ans = "w"
break
elif 125 < mouse.x < 155 and 145 < mouse.y < 175:
ans = "x"
break
elif 155 < mouse.x < 185 and 145 < mouse.y < 175:
ans = "y"
break
elif 185 < mouse.x < 215 and 145 < mouse.y < 175:
ans = "z"
break
As simple solution using a calculation vs. multiple if statements:
import string
while True:
mouse = win.getMouse()
x, xr = divmod(mouse.x-35, 30)
y, yr = divmod(mouse.y-85, 30)
if not(0 <= x < 10 and 0 <= y < 3): # Guard values
continue
if xr == 0 or yr == 0: # Gridlines?
continue
try:
ans = string.ascii_lowercase[10*y+x]
break
except IndexError:
pass
e.g. mouse(x=102, y=143) would return 'm'
You could use the following code to avoid the big if statement.
def check_pos(x, y):
table = 'abcdefghijklmnopqrstuvwxyz'
idx = (x - (35-30)) / 30
idy = (y - (85-30)) / 30
# each row has 10 cells
offset = idx + (idy -1) * 10
if 1 <= offset <= 26:
return table[offset-1]
import string
alpha=string.ascii_lowercase
mouse = win.getMouse()
for idx,i in enumerate(range(35,335,30)):
if mouse.x>i and mouse.x<i+30:
if mouse.y>85 and mouse.y<115:
ans=alpha[idx]
break
elif mouse.y>115 and mouse.y<145:
ans=alpha[idx + 10]
break
elif mouse.y>145 and mouse.y<175:
ans=alpha[idx+20]
break