Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:42:26+00:00 2026-05-11T09:42:26+00:00

I am trying to teach myself Python by working through some problems I came

  • 0

I am trying to teach myself Python by working through some problems I came up with, and I need some help understanding how to pass functions.

Let’s say I am trying to predict tomorrow’s temperature based on today’s and yesterday’s temperature, and I have written the following function:

def predict_temp(temp_today, temp_yest, k1, k2):     return k1*temp_today + k2*temp_yest 

And I have also written an error function to compare a list of predicted temperatures with actual temperatures and return the mean absolute error:

def mean_abs_error(predictions, expected):     return sum([abs(x - y) for (x,y) in zip(predictions,expected)]) / float(len(predictions)) 

Now if I have a list of daily temperatures for some interval in the past, I can see how my prediction function would have done with specific k1 and k2 parameters like this:

>>> past_temps = [41, 35, 37, 42, 48, 30, 39, 42, 33] >>> pred_temps = [predict_temp(past_temps[i-1],past_temps[i-2],0.5,0.5) for i in xrange(2,len(past_temps))] >>> print pred_temps [38.0, 36.0, 39.5, 45.0, 39.0, 34.5, 40.5] >>> print mean_abs_error(pred_temps, past_temps[2:]) 6.5 

But how do I design a function to minimize my parameters k1 and k2 of my predict_temp function given an error function and my past_temps data?

Specifically I would like to write a function minimize(args*) that takes a prediction function, an error function, some training data, and that uses some search/optimization method (gradient descent for example) to estimate and return the values of k1 and k2 that minimize my error given the data?

I am not asking how to implement the optimization method. Assume I can do that. Rather, I would just like to know how to pass my predict and error functions (and my data) to my minimize function, and how to tell my minimize function that it should optimize the parameters k1 and k2, so that my minimize function can automatically search a bunch of different settings of k1 and k2, applying my prediction function with those parameters each time to the data and computing error (like I did manually for k1=0.5 and k2=0.5 above) and then return the best results.

I would like to be able to pass these functions so I can easily swap in different prediction and error functions (differing by more than just parameter settings that is). Each prediction function might have a different number of free parameters.

My minimize function should look something like this, but I don’t know how to proceed:

def minimize(prediction_function, which_args_to_optimize, error_function, data):     # 1: guess initial parameters     # 2: apply prediction function with current parameters to data to compute predictions     # 3: use error function to compute error between predictions and data     # 4: if stopping criterion is met, return parameters     # 5: update parameters     # 6: GOTO 2 

Edit: It’s that easy?? This is no fun. I am going back to Java.

On a more serious note, I think I was also getting hung up on how to use different prediction functions with different numbers of parameters to tune. If I just take all the free parameters in as one tuple I can keep the form of the function the same so it easy to pass and use.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:42:27+00:00Added an answer on May 11, 2026 at 9:42 am

    Here is an example of how to pass a function into another function. apply_func_to will take a function f and a number num as parameters and return f(num).

    def my_func(x):     return x*x  def apply_func_to(f, num):     return f(num)  >>>apply_func_to(my_func, 2) 4 

    If you wanna be clever you can use lambda (anonymous functions too). These allow you to pass functions ‘on the fly’ without having to define them separately

    >>>apply_func_to(lambda x:x*x, 3) 9 

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm pretty sure the above won't compile. DoSomething(j) is an… May 12, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer Based on what i learnt from net/book/ and provided answers.… May 12, 2026 at 12:24 am
  • Editorial Team
    Editorial Team added an answer I think it is a readability thing and I much… May 12, 2026 at 12:24 am

Related Questions

[Please note that this is a different question from the already answered How to
So i'm toying around with stackless python, writing a very simple webserver to teach
I am trying to teach myself Smalltalk. A tutorial has this example of a
I'm trying to teach myself Ruby's Treetop grammar generator. I am finding that not

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.