fonction in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aliassaf

    #1

    fonction in python


    Hello,

    If we write = x^2 and if I give to the program the values of x, it will
    going to calculate the values of y, and also for x.

    But it is possible ? that is if I give to the program the values of X and Y,
    it will indicate to me the relation between the two variables, in the other
    hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
    that f (t)!!!

    Thanks

    --
    View this message in context: http://www.nabble.com/fonction-in-py....html#a5164997
    Sent from the Python - python-list forum at Nabble.com.

  • Georg Brandl

    #2
    Re: fonction in python

    aliassaf wrote:
    Hello,
    >
    If we write = x^2 and if I give to the program the values of x, it will
    going to calculate the values of y, and also for x.
    >
    But it is possible ? that is if I give to the program the values of X and Y,
    it will indicate to me the relation between the two variables, in the other
    hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
    that f (t)!!!
    That is not possible at all. There are too many possible functions mapping
    2 to 4, 3 to 9 etc.

    You can, however, create a list of candidate functions and check your input
    pairs (x,y) against each of those functions to see if any of them matches.

    Short, unoptimized example:

    functions = [
    lambda x: x,
    lambda x: x+1,
    lambda x: x*x,
    lambda x: x**3,
    ]

    input = [(1,1), (2,2)]

    for function in functions:
    for x,y in input:
    if function(x) != y:
    break
    else:
    print "function", function, "matches"

    Georg

    Comment

    • Steven D'Aprano

      #3
      Re: fonction in python

      On Tue, 04 Jul 2006 03:06:37 -0700, aliassaf wrote:
      >
      Hello,
      >
      If we write = x^2 and if I give to the program the values of x, it will
      going to calculate the values of y, and also for x.
      >
      But it is possible ? that is if I give to the program the values of X and Y,
      it will indicate to me the relation between the two variables, in the other
      hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
      that f (t)!!!
      You are asking for curve-fitting. There is a HUGE amount of work on
      curve-fitting in computer science and statistics.

      Generally, you start with some data points (x, y). You generally have some
      idea of what sort of function you expect -- is it a straight line? A
      curve? What sort of curve? A polynomial, an exponential, a sine curve, a
      cubic spline, a Bezier curve?

      You might like to google on "least squares curve fitting" and "linear
      regression". That's just two methods out of many.

      Some curve-fitting methods also estimate the error between the predicted
      curve and the data points; you could then try all of the methods and pick
      the one with the least error.



      --
      Steven.

      Comment

      • Grant Edwards

        #4
        Re: fonction in python

        On 2006-07-04, aliassaf <assafal@ensiet a.frwrote:
        But it is possible ? that is if I give to the program the values of X and Y,
        it will indicate to me the relation between the two variables, in the other
        hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
        that f (t)!!!
        Sort of. There are a number of curve-fitting modules available
        for Python as part of packages like ScyPy http://www.scipy.org/
        and Scientific Python http://sourcesup.cru.fr/projects/scientific-py/

        You generally have to provide the fitter with a function
        "template" for which it can find the coefficients. For
        example, you tell the fitter that you want a polynomial of the
        form f(y) = Ax^2 + Bx + C, and the fitter will find the values
        of A, B, C that best fit the data.

        There are also commercial products that have lists of hundreds
        of "templates" and will crunch through thme to find the ones
        that provide the best fits.

        --
        Grant Edwards
        grante@visi.com

        Comment

        • mensanator@aol.com

          #5
          Re: fonction in python


          aliassaf wrote:
          Hello,
          >
          If we write = x^2 and if I give to the program the values of x, it will
          going to calculate the values of y, and also for x.
          >
          But it is possible ? that is if I give to the program the values of X and Y,
          it will indicate to me the relation between the two variables, in the other
          hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
          that f (t)!!!
          You can use the GMPY module to determine square & power relationships:
          >>import gmpy
          >>for n in range(20):
          print n,
          if gmpy.is_square( n):
          print True,
          else:
          print False,
          if gmpy.is_power(n ):
          print True
          else:
          print False


          0 True True
          1 True True
          2 False False
          3 False False
          4 True True
          5 False False
          6 False False
          7 False False
          8 False True
          9 True True
          10 False False
          11 False False
          12 False False
          13 False False
          14 False False
          15 False False
          16 True True
          17 False False
          18 False False
          19 False False

          9 is both a power and a square whereas 8 is a power but not a square.

          >
          Thanks
          >
          --
          View this message in context: http://www.nabble.com/fonction-in-py....html#a5164997
          Sent from the Python - python-list forum at Nabble.com.

          Comment

          • Duncan Smith

            #6
            Re: fonction in python

            Steven D'Aprano wrote:
            On Tue, 04 Jul 2006 03:06:37 -0700, aliassaf wrote:
            >
            >
            >>Hello,
            >>
            >>If we write = x^2 and if I give to the program the values of x, it will
            >>going to calculate the values of y, and also for x.
            >>
            >>But it is possible ? that is if I give to the program the values of X and Y,
            >>it will indicate to me the relation between the two variables, in the other
            >>hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
            >>that f (t)!!!
            >
            >
            You are asking for curve-fitting. There is a HUGE amount of work on
            curve-fitting in computer science and statistics.
            >
            Generally, you start with some data points (x, y). You generally have some
            idea of what sort of function you expect -- is it a straight line? A
            curve? What sort of curve? A polynomial, an exponential, a sine curve, a
            cubic spline, a Bezier curve?
            >
            You might like to google on "least squares curve fitting" and "linear
            regression". That's just two methods out of many.
            >
            Some curve-fitting methods also estimate the error between the predicted
            curve and the data points; you could then try all of the methods and pick
            the one with the least error.
            >
            The problem being that complex enough models will fit the data
            arbitrarily closely (i.e. over-fit). The OP should take into account
            any prior expectations over the type of function (as you indicate) and
            apply Occam's razor (find a relatively simple model that gives a
            reasonable fit to the data).

            Duncan

            Comment

            • John Machin

              #7
              Re: fonction in python

              On 4/07/2006 8:06 PM, aliassaf wrote:
              Hello,
              >
              If we write = x^2 and if I give to the program the values of x, it will
              going to calculate the values of y, and also for x.
              >
              But it is possible ? that is if I give to the program the values of X and Y,
              it will indicate to me the relation between the two variables, in the other
              hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
              that f (t)!!!
              Please pardon me for introducing Python-related subject matter into a
              thread devoted to curve-fitting :-)

              Consider the following:

              |>[x ^ 2 for x in range(10)]
              [2, 3, 0, 1, 6, 7, 4, 5, 10, 11]

              Not what you wanted? Try this:

              |>[x ** 2 for x in range(10)]
              [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

              Cheers,
              John

              Comment

              Working...