Get function given a list of values

Question:

Is there a way that I can give python a list of values like [ 1, 3, 4.5, 1] and obtain a function that relates to those values? like y = 3x+4 or something like that?

I don’t want to plot it or anything, I just want to substitute values in that function and see what the result would be.

edit: is there a way that python can calculate how the data is related? like if I give it the list containing thousands of values and it returns me the function that was adjusted to those values.

Asked By: bb2

||

Answers:

Yes, the function is called map().

def y(x):
    return 3*x+4

map(y, [1,3,4.5,1])

The map() function applies the function to every item and returns a list of the results.

Answered By: David Heffernan

Based on your comments to David Heffernan’s answer,

I want is to know what the relation between the values is, I have thousands of values stored in a list and I want to know if python can tell me how they are related..

it seems like you are trying do a regression analysis (probably a linear regression) and fit the values.

You can use NumPy for linear regression analysis in Python. Here a sample from the NumPy cookbook.

Answered By: Praveen Gollakota

I assume you want to find out if the sequences [1, 2, 3, 4] and [ 1, 3, 4.5, 1] (or else the pairs [(1, 1), (2, 3), (3, 4.5), (4, 1)] are related with a (linear) function or not.

Try to plot these and see if they form somethign that looks like a (straight) line or not.

You can also look for correlation techniques. Check this site with basic statistic stuff (look down on correlation: Basic Statistics

enter image description here

Answered By: ypercubeᵀᴹ

Based on your revised question, I’m going to go ahead and add an answer. No, there is no such function. I imagine you’re unlikely to find a function that comes close in any programming language. Your definitions aren’t tight enough for anything to be reasonable yet. If we take a simple case with only two input integers you can have all sorts of relationships:

[10, 1]

possible relationships:

def x(y):
  return y ** 0

def x(y):
  return y / 10

def x(y)
  return y % 10 + 1

… … repeat. Admittedly, some of those are arbitrary, but they are valid relationships between the first and second values in the array you passed in. The possibilities for “solutions” become even more absurd as you ask for a relationship between 10, 15, or 35 numbers.

Answered By: g.d.d.c

What you’re looking for is called “statistical regression”. There are many methods by which you might do this; here’s a site that might help: Least Squares Regression but ultimately, this is a field to which many books have been devoted. There’s polynomial regressions, trig regressions, logarithmic…you’ll have to know something about your data before you decide which model you apply; if you don’t have any knowledge of what the dataset will look like before you process it, I’d suggest comparing the residuals of whatever you get and choosing the one with the lowest sum.

Short answer: No, no function.

Answered By: jimf
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.