【发布时间】:2016-11-03 14:48:18
【问题描述】:
我正在尝试拟合多个数据集,这些数据集应该有一些变量在数据集之间共享,而其他变量则不是。但是我不确定我需要采取哪些步骤来做到这一点。下面我展示了我正在尝试使用的方法(从“问题从这里开始”不起作用,它只是为了说明目的)。
In this answer 有人能够共享参数arcoss 数据集是否有某种方法可以调整,以便我也可以拥有一些非共享参数?
有没有人知道我该如何实现这一目标,或者有人可以建议一种更好的方法来实现相同的结果?谢谢。
import numpy as np
from scipy.stats import gamma
import matplotlib.pyplot as plt
import pandas as pd
from lmfit import minimize, Minimizer, Parameters, Parameter, report_fit, Model
# Create datasets to fit
a = 1.99
start = gamma.ppf(0.001, a)
stop = gamma.ppf(.99, a)
xvals = np.linspace(start, stop, 100)
yvals = gamma.pdf(xvals, a)
data_dict = {}
for dataset in range(4):
name = 'dataset_' + str(dataset)
rand_offset = np.random.uniform(-.1, .1)
noise = np.random.uniform(-.05, .05,len(yvals)) + rand_offset
data_dict[name] = yvals + noise
df = pd.DataFrame(data_dict)
# Create some non-shared parameters
non_shared_param1 = np.random.uniform(0.009, .21, 4)
non_shared_param2 = np.random.uniform(0.01, .51, 4)
# Create the independent variable
ind_var = np.linspace(.001,100,100)
# Create a model
def model_func(time, Qi, at, vw, R, rhob_cb, al, NSP1, NSP2):
Dt = at * vw
Dl = al * vw
t = time
first_bot = 8 * np.pi * t * rhob_cb
sec_bot = np.sqrt((np.pi * (Dl * R) * t))
exp_top = R * np.power((NSP1 - ((t * vw)/R)), 2)
exp_bot = 4 * Dl * t
exp_top2 = R * np.power(NSP2, 2)
exp_bot2 = 4 * Dt * t
return (Qi / first_bot * sec_bot) * np.exp(- (exp_top / exp_bot) - (exp_top2 / exp_bot2))
model = Model(model_func)
### Issues begin here ###
all_results = {}
index = 0
for col in df:
# This block assigns the correct non-shared parameter for the particular fit
nsp1 = non_shared_param1[index]
nsp2 = non_shared_param2[index]
index += 1
params = Parameters()
at = 0.1
al = 0.15
vw = 10**-4
Dt = at * vw
Dl = al * vw
# Non-shared parameters
model.set_param_hint('NSP1', value = nsp1)
model.set_param_hint('NSP2', value = nsp2)
# Shared and varying parameters
model.set_param_hint('vw', value =10**-4, min=10**-10)
model.set_param_hint('at', value =0.1)
model.set_param_hint('al', value =0.15)
# Shared and fixed parameters
model.set_param_hint('Qi', value = 1000, vary = True)
model.set_param_hint('R', value = 1.7, vary = True)
model.set_param_hint('rhob_cb', value =2895, vary = True)
# One set of parameters should be returned
result = model.fit(df[col], time = ind_var)
all_results[index] = result
【问题讨论】:
-
如果您想预测另一个数据集的样本类别,您必须将该数据集的特征集合投影到训练分类器的数据集的特征集合之上。换句话说,您不能在训练某些特征后对其他不同的特征进行测试!
-
@MMF 对不起,我不认为我关注。我的不同数据集是从不同空间位置对同一事件的观察。然后,我将使用考虑距离的相同公式来拟合数据。所以我需要有一些非共享参数(空间),但每个观察点的“未知”参数应该是相同的。本质上,我想尝试非共享参数的每种组合并返回在所有观察中产生最小残差的组合。我在问题中概述的方法是否完全错误?感谢您的帮助!