Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function with array input
#1
Hello, I have one (I think easy) question about including arrays as inputs in a function. I need to do a maximization problem of an objective function with thousands of variables. I could write the objective function manually but I hope there is an alternative. Especifically, I would like to do something like this:

def my_function(x):

return x[1]*0.8 + x[2]*0,5 + ... + x[n]*b_n

where x is a vector of n variables, and b is a vector of n parameters. Is there any way to do something like this?:

def my_function(x):

return x*b

where we have previously defined the vector of parameters b.

Thanks
Reply
#2
Hi,

I have done this with other languages, but i'm ashamed to say not in Python. Shocked
But i know its possible.
Seach for "Python convolution matrix", and probably you'll find what you need.

Paul
Reply
#3
Something like this?
import random

def calc(variables, params):
    '''return sum of params[i] * variables[i]'''
    result = 0
    for x, y in zip(params, variables):
        result += x * y
    return result

params= [1, 1, 2, 3, 5, 8]
print(calc([1]*6), params) # Should print 20
values = [random.sample(range(100), 6) for _ in range(100)]
results = [calc(value, params) for value in values]
Reply
#4
This sounds like some quite heavy numerical work. Is there a reason you're not using NumPy and SciPy if appropriate? Since you're mentioning maximisation, I wonder if anything in scipy.optimize is relevant.
Reply
#5
Thank you for your answer. I have an error running this code which says:

"function() missing 1 required positional argument: 'params'"

Do you know why?
Reply
#6
(Nov-30-2020, 05:23 PM)ndc85430 Wrote: This sounds like some quite heavy numerical work. Is there a reason you're not using NumPy and SciPy if appropriate? Since you're mentioning maximisation, I wonder if anything in scipy.optimize is relevant.

Thank you for your answer. I did not mention my objective. I am trying to carry on a land-use spatial optimization problem where each variable x is a binary decision about using land "j" on parcel "h". The algorithm, due to the complexity is heuristic and is based on genetics and not always leads to an optimal solution, but aproximated. I think scipy is more for traditional simple optimization problems, no?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 929 Feb-21-2024, 08:02 PM
Last Post: bterwijn
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Showing an empty chart, then input data via function kgall89 0 946 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,064 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  input function question barryjo 12 2,637 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,009 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Problem with input after function luilong 10 4,021 Dec-04-2021, 12:16 AM
Last Post: luilong
  Exit function from nested function based on user input Turtle 5 2,859 Oct-10-2021, 12:55 AM
Last Post: Turtle
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,932 Oct-06-2021, 09:39 AM
Last Post: menator01
  Input function cutting off commands at spaces. throwaway34 3 2,155 May-12-2021, 06:40 AM
Last Post: throwaway34

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020