多变量数据拟合

时间:2016-10-06 13:44:06

标签: python matlab numpy scipy

我有一个文件设置,每行看起来像这样:

y x1 x2 x3 x4 x5 x6 name

我正在尝试找到一个适合x(1-6)到y的函数。

我发现this scipy method这样做了(我相信,虽然它可能只是单变量),但我不确定语法应该如何/如何为我的情况设置它。

我正在使用这种方法将我的文件读入一个numpy数组(Python 3.5):

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

任何帮助操作此方法的人都会非常感激,或者如果你知道解决这个问题的另一种方法也会很棒 - 它不一定是python。

我看到this question的答案有助于单变量拟合,而this question听起来很相似,但很少与它讨论。

我确实在下面有这个测试功能,但我不是我得到的结果的忠实粉丝 - 他们只是不太准确(我改变了增量,我怀疑数据是线性的但我不知道如何手动使脚本适合其他数据样式),脚本很慢。

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)

0 个答案:

没有答案