1

I've a file setup with each line looking like this:

y x1 x2 x3 x4 x5 x6 name

I'm trying to find a function that fits x(1-6) to y.

I found this scipy method of doing so (I believe, although it could be single-variable only), but I'm not sure how the syntax is supposed to be/how to set it up for my situation.

I'm using this method of reading my file into a numpy array (Python 3.5):

data = np.loadtxt(fh,usecols=(0,1,2,3,4,5,6))

Any help with manipulating this method would be greatly appreciated, or if you know of a different way to solve this problem that would be great too - it doesn't have to be python.

I have seen this question whose answer helps with single variable fitting, and this question, which sounds similar but has little discussion with it.

I did have this test function below, but I'm not a big fan of the results I got - they're just not very accurate (I varied the increments, I doubt the data is linear but I don't know how to manually make a script to fit other data styles), and the script is slow.

result_list = []

num_add = 1

#Read in file of data
file_l = []
for x in fh:
    file_l.append(x)

#Check iterations for fit
for it1 in [x * 0.01 for x in range(-100, 100)]:
    for it2 in [x * 0.01 for x in range(-100, 100)]:
        for it3 in [x * 0.01 for x in range(-100, 100)]:
            for it4 in [x * 0.01 for x in range(-100, 100)]:
                for it5 in [x * 0.01 for x in range(-100, 100)]:
                    for it6 in [x * 0.01 for x in range(-100, 100)]:
                        diff = 0

                        for x in file_l:
                            x  = x.split()

                            hlg = float(x[0])
                            ti1 = int(x[1])
                            ti2 = int(x[2])
                            ti3 = int(x[3])
                            ti4 = int(x[4])
                            ti5 = int(x[5])
                            ti6 = int(x[6])
                            name = x[7]

                            calc_result =  it1*ti1 + it2*ti2 + it3*ti3 + it4*ti4 + it5*ti5 + it6*ti6
                            diff = diff + abs(hlg - calc_result)
                            num_add = num_add + 1

                        diff_per_add = diff / num_add

                        app_l = [diff_per_add,it4,it5,it6]
                        result_list.append(app_l)
                        print(it1,it2,it3,it4,it5,it6,diff,name)


#Deternmine the vars that gave closest fit
minv = 999999999999999999
min_l = []
for lists in result_list:
    val = float(lists[0])
    if val < minv:
        min_l = lists
        minv = val


print(min_l)
Community
  • 1
  • 1
MadisonCooper
  • 236
  • 2
  • 15
  • What kind of function are you trying to fit? If linear functions, have a look at `sklearn.linear_model.LinearRegression` – P. Camilleri Oct 06 '16 at 13:46
  • @P.Camilleri I'll take a look - but something robust that could do x^y polynomials, logs,etc (Some library or something) would be good, if such a thing exists. – MadisonCooper Oct 06 '16 at 13:50
  • for the sake of not posting a link only answer: [This](http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#example-of-solving-a-fitting-problem) may be what you're looking for – Aaron Oct 06 '16 at 14:04
  • unless you don't really have an idea of the functional form... that would make things a bit harder.. although it seems like you only have a simple linear problem from your provided code.. – Aaron Oct 06 '16 at 14:07
  • @Aaron That may be exactly what I needed. Thank you. – MadisonCooper Oct 06 '16 at 14:41
  • @ChildishJack sorry it's not really a full answer, but I thought they had pretty much exactly what you needed in the docs. lemme know if you need any more help using that – Aaron Oct 06 '16 at 14:46
  • @Aaron It seems to be exactly what I was looking for, I just didn't find it in my prior searches for examples. Will do. Thanks again! – MadisonCooper Oct 06 '16 at 14:47

0 Answers0