Reputation: 13
I have a sample which gives values in the form [p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p12 p13 p14 p15 p16]
print(sample)
[[ 1.43945312 4.29003906 9.55566406 ... 61.11035156 66.15917969
69.03417969]
[ 2.98242188 4.29003906 9.55566406 ... 61.11035156 66.15917969
69.03417969]
[ 1.43945312 6.17675781 9.55566406 ... 61.11035156 66.15917969
69.03417969]
...
[ 1.92480469 5.76220703 9.69482422 ... 61.23486328 66.62939453
69.31982422]
[ 1.92480469 5.76220703 9.69482422 ... 61.23486328 65.97900391
68.15966797]
[ 1.78027344 4.68701172 10.25439453 ... 62.68505859 66.62939453
68.15966797]]
I want to iterate through this sample using a function which takes 16 inputs and gives one value as an output.
How can I do this? For example, take the function model(p1, p2, p3, p4, p5, p6, .... p16) which requires 16 inputs I want to iterate through the sample such that it will do
model(1.43945312, 4.29003906, 9.55566406, ... 61.11035156, 66.15917969, 69.03417969)
(Which is the first sample) and then save the output
and then it will move onto the second sample and do
model(2.98242188, 4.29003906, 9.55566406, ... 61.11035156, 66.15917969, 69.03417969)
and save the output.. so on so forth.
Does this make sense? Apologies as I am new to Python and do not know the exact terminology yet How can I do this
Upvotes: 1
Views: 80
Reputation: 5992
Use a for loop and iterable unpacking:
def model(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16):
pass # do something
output = []
for sample in data: # iterate over samples
print(len(sample))
result = model(*sample)
output.append(result)
Upvotes: 2
Reputation: 31
The answer by @Programmer works fine One think that you might want to use, is *args in function declaration. This enables you to have variable amount of arguments and refer them as a list so you don't need to specify all 16 arguments.
def method(*args):
p1 = args[0]
...
p16 = args[15]
# Or whatever you want to do with them.
However this could lead to IndexError
if you don't supply the method with enough arguments. It depends on what you want to do with your function.
Upvotes: 0