0Dear all I have this code below: (I do know it's incorrect, by the way):
list1 = ([33, 37], [38, 45], [46, 54], [55, 62], [63, 74], [75, 79], [80, 90], [91, 95], [96, 110], [111, 112], [113, 125], [126, 147], [148, 159], [160, 185])
list2 = [100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 400, 450, 500]
new_dict = {k: v for k, v in zip(list2, list1)}
print(new_dict)
weight = input("enter weight :")
for key, value in new_dict.items():
value = int(new_dict[value])
if weight <= [value] or weight >= [value]:
dose = k
print(dose)
Basically what I am trying to come up with is a program that dose bands a med for you. I'll explain, the drug in question is infliximab and I am using a dose of 3mg/kg, so if I was to have a patient that was 36kg, i'd get a dose of 108mg, which I'd like my program to go through the values in the dictionary (list1) find out that 36 is between 33 and 37 and return a dose of 100mg (key) instead.
I am quite new to python and I do know there is a simple way but I currently lack the wherewithal and would appreciate any help.
Thanks
You are on the right track, but rather than zip the lists together making them a little harder to work with for this problem, let's find the proper range and it's index and use that index to find the dose.
list1 = ([33, 37], [38, 45], [46, 54], [55, 62], [63, 74], [75, 79], [80, 90], [91, 95], [96, 110], [111, 112], [113, 125], [126, 147], [148, 159], [160, 185])
list2 = [100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 400, 450, 500]
weight = float(input("enter weight :"))
dose = "n/a"
for index, bracket in enumerate(list1):
if bracket[0] <= weight <= bracket[1]:
dose = list2[index]
break
print(dose)
Alternatively, if you are keen on using zip() then you might:
list1 = ([33, 37], [38, 45], [46, 54], [55, 62], [63, 74], [75, 79], [80, 90], [91, 95], [96, 110], [111, 112], [113, 125], [126, 147], [148, 159], [160, 185])
list2 = [100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 400, 450, 500]
weight = float(input("enter weight :"))
dose = [amt for bracket, amt in zip(list1, list2) if bracket[0] <= weight <= bracket[1]]
if dose:
print(dose[0])
Related
Hi is there any method for apply trasnformation for certain batch?
It means, I want apply trasnformation for just last batch in every epochs.
What I tried is here
import torch
class test(torch.utils.data.Dataset):
def __init__(self):
self.source = [i for i in range(10)]
def __len__(self):
return len(self.source)
def __getitem__(self, idx):
print(idx)
return self.source[idx]
ds = test()
dl = torch.utils.data.DataLoader(dataset = ds, batch_size = 3,
shuffle = False, num_workers = 5)
for i in dl:
print(i)
because I thought that if I could get idx number, it would be possible to apply for certain batchs.
However If using num_workers outputs are
0
1
2
3
964
57
8
tensor([0, 1, 2])
tensor([3, 4, 5])
tensor([6, 7, 8])
tensor([9])
which are not I thought
without num_worker
0
1
2
tensor([0, 1, 2])
3
4
5
tensor([3, 4, 5])
6
7
8
tensor([6, 7, 8])
9
tensor([9])
So the question is
Why idx works so with num_workers?
How can I apply trasnform for certain batchs (or certain idx)?
When you have num_workers > 1, you have multiple subprocesses doing data loading in parallel. So what is likely happening is that there is a race condition for the print step, and the order you see in the output depends on which subprocess goes first each time.
For most transforms, you can apply them on a specific batch simply by calling the transform after the batch has been loaded. To do this just for the last batch, you could do something like:
for batch_idx, batch_data in dl:
# check if batch is the last batch
if ((batch_idx+1) * batch_size) >= len(ds):
batch_data = transform(batch_data)
I found that
class test_dataset(torch.utils.data.Dataset):
def __init__(self):
self.a = [i for i in range(100)]
def __len__(self):
return len(self.a)
def __getitem__(self, idx):
a = torch.tensor(self.a[idx])
#print(idx)
return idx
a = torch.utils.data.DataLoader(
test_dataset(), batch_size = 10, shuffle = False,
num_workers = 10, pin_memory = True)
for i in a:
print(i)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
tensor([20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
tensor([30, 31, 32, 33, 34, 35, 36, 37, 38, 39])
tensor([40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
tensor([50, 51, 52, 53, 54, 55, 56, 57, 58, 59])
tensor([60, 61, 62, 63, 64, 65, 66, 67, 68, 69])
tensor([70, 71, 72, 73, 74, 75, 76, 77, 78, 79])
tensor([80, 81, 82, 83, 84, 85, 86, 87, 88, 89])
tensor([90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
I have a function that returns a list of lists where the lists have two elements: the start and end of a region of an input list. I have a temporary variable that I append to my output list but was surprised that it changed the whole list each time it was appended. It feels like I must not understand something fundamental to Python lists for this to be true. When I append [pair[0], pair[1]] on line 31, however, it works where just appending pair does not. Sample code and output is below. Can someone please explain what I'm missing?
# Returns list of list indexes with the start and stop index for each flat part of the signal (ignoring the ramps in between)
def edge_detect(input: list, edge: float = 0.5, filter_limit: int = 5) -> list:
last = None
index = 0
output_wrong = []
output_right = []
pair = [0,0]
filter_count = 0
finding = "start"
for value in input:
if finding == "start":
if last == None:
pair[0] = index
else:
# If this point is too different from the last then last wasn't the start of a flat region.
if abs(value - last) > edge:
pair[0] = index
# If this point has settles then last could be the start of a flat region but wait for filter_limit before deciding that
elif filter_count < filter_limit:
filter_count += 1
# Point has settled for filter limit. Start looking for the "stop" point
else:
finding = "stop"
pair[1] = index
elif finding == "stop":
# As soon as value falls outside of flat region record that region and start looking for another flat spot
if abs(value - last) > edge:
finding = "start"
filter_count = 0
output_wrong.append(pair)
output_right.append([pair[0], pair[1]])
else:
pair[1] = index
last = value
index += 1
return output_wrong, output_right
test = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 2.1, 3.3, 4.6, 6, 7.5, 9.1, 10.8, 12.6,
14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5,
17.5, 20.6, 23.8, 27.1, 30.5, 34, 37.6, 41.3, 45.1,
49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
54, 59.1, 64.3, 69.6, 75, 80.5, 86.1, 91.8, 97.6,
103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5,
110.5, 117.6, 124.8, 132.1, 139.5, 147, 154.6, 162.3, 170.1,
178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178,
187, 196.1, 205.3, 214.6, 224, 233.5, 243.1, 252.8, 262.6, 272.5]
steps_wrong, steps_right = edge_detect(test)
print(steps_wrong)
print(steps_right)
step_data = []
for step in steps_wrong:
step_data.append(test[step[0]:step[1]])
print(step_data)
step_data = []
for step in steps_right:
step_data.append(test[step[0]:step[1]])
print(step_data)
This is the sample output:
[[99, 89], [99, 89], [99, 89], [99, 89], [99, 89]]
[[0, 9], [19, 29], [39, 49], [59, 69], [79, 89]]
[[], [], [], [], []]
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5, 14.5], [49, 49, 49, 49, 49, 49, 49, 49, 49, 49], [103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5, 103.5], [178, 178, 178, 178, 178, 178, 178, 178, 178, 178]]
How do I add element 1 in all the lists? After that, add element 2? I need to find the percentage of them.
rows = [["SOBs", 60, 80, 70, 75], ["Test1", 60, 50, 60, 65], ["Test2", 40, 30, 40, 45], ["Test3", 45, 90, 80, 85], ["CW", 40, 80, 70, 75]]
I have tried in this manner:
sum(sum(rows, [2])) - this doesn't work
print(sum(rows[0][1] + [1][1])) - also doesn't work
So the for element 1 in it would be 60+60+40+45+40 = 245
Then I take the 245/500*100 = 49%
You can try with list comprehension easily.
element = 1
rows = [["SOBs", 60, 80, 70, 75], ["Test1", 60, 50, 60, 65], ["Test2", 40, 30, 40, 45], ["Test3", 45, 90, 80, 85], ["CW", 40, 80, 70, 75]]
sum_1= sum([rows[i][element] for i in range(len(rows))])
element = 2
sum_2= sum([rows[i][element] for i in range(len(rows))])
print((sum_1*100)/(sum_1+sum_2))
Consider it like a table, and use comprehensions, to summarize columns.
Try this
rows = [["SOBs", 60, 80, 70, 75], ["Test1", 60, 50, 60, 65], ["Test2", 40, 30, 40, 45], ["Test3", 45, 90, 80, 85],
["CW", 40, 80, 70, 75]]
COL_LENGTH = 4 # set as 4, as you have only 4 colums to evaluate
# Create a list of column values for each column index
selected_cols = [[row[i] for row in rows] for i in range(1, COL_LENGTH)]
# Get sum of every column
sums = [sum(col) for col in selected_cols]
# get avg of every column
avgs = [_sum / 5 for _sum in sums]
I want to swap elements between two array starting from a particular array index value keeping other values prior to the array index intact.
import numpy as np
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])
t = []
for i in range(len(r)):
for j in range(len(p)):
if i >= 3 and j >= 3:
t.append(p[j])
p[j] = r[i]
for k in t:
r[i] = k
The above code does the task but the values are in reverse order.
The value that I want in array p after swapping is:
[70, 80, 90, 40, 50, 60]
and the value that i want in array r after swapping is:
[10, 20, 30, 100, 110, 120]
But in array p I am getting:
[70, 80, 90, 60, 50, 40]
and in array r I am getting:
[10, 20, 30, 120, 110, 100]
I don't know what is wrong with the code.
import numpy as np
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])
for i in range(len(r)):
if (i>=3):
p[i],r[i] = r[i],p[i]
Above code will do the work for you. You don't need to run two for loop and t array if I understand your problem right. All you want is to swap at some indexes. You can just swap at those indexes as above no need of a temporary array t.
You can achieve the same without looping:
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])
i = 3
temp = p[i:].copy()
p[i:] = r[i:]
r[i:] = temp
Now:
>>> p
array([70, 80, 90, 40, 50, 60])
>>> r
array([ 10, 20, 30, 100, 110, 120])
You can copy a slice of one array on to the other:
In [113]: r = np.array([10, 20, 30, 40, 50, 60])
...: p = np.array([70, 80, 90, 100, 110, 120])
...:
In [114]: t = p.copy()
In [115]: t[3:]=r[3:]
In [116]: t
Out[116]: array([70, 80, 90, 40, 50, 60])
You could also join slices:
In [117]: np.concatenate((p[:3], r[3:]))
Out[117]: array([70, 80, 90, 40, 50, 60])
Those answers create a new array. I think that's clearer than doing an inplace swap. But here's how I'd do the swap
In [128]: temp = r[3:].copy()
In [129]: r[3:]=p[3:]
In [130]: p[3:]=temp
In [131]: r
Out[131]: array([ 10, 20, 30, 100, 110, 120])
In [132]: p
Out[132]: array([70, 80, 90, 40, 50, 60])
I use copy in temp because otherwise a slice produces a view, which will get modified in the next copy. That issue has come up recently when swapping rows of a 2d array.
With lists the swapping is easier - because r[3:] makes a copy.
In [139]: r=r.tolist()
In [140]: p=p.tolist()
In [141]: temp = r[3:]
In [142]: r[3:], p[3:] = p[3:], r[3:]
In [143]: r
Out[143]: [10, 20, 30, 100, 110, 120]
In [144]: p
Out[144]: [70, 80, 90, 40, 50, 60]
how i can Pick only the First pick in a List in this example?
List = [[110, 10, 20 , 30], [120, 40, 50, 60], [130, 70, 80, 90]]
The Output should be:
Times = [110, 120, 130]
Thank you
Come on
[l[0] for l in my_list]
Try this new_list = old_list[0].
Wouldnt this just Show the Output
[110, 10, 20, 30]