我正在努力在 pymc3 中实现具有自定义可能性的线性回归。
我之前在 CrossValidated 上发布过这个问题,建议在此处发布,因为该问题更面向代码(此处已关闭帖子)
假设您有两个自变量 x1、x2 和一个目标变量 y,以及一个称为 delta 的指示变量。
观察到的数据的示例片段:
x_1 x_2 observed_target
10 1 0 100
20 2 0 50
5 -1 1 200
10 -2 1 100
Run Code Online (Sandbox Code Playgroud)
有谁知道如何在 pymc3 中实现这一点?作为起点...
model = pm.Model()
with model as ttf_model:
intercept = pm.Normal('param_intercept', mu=0, sd=5)
beta_0 = pm.Normal('param_x1', mu=0, sd=5)
beta_1 = pm.Normal('param_x2', mu=0, sd=5)
std = pm.HalfNormal('param_std', beta = 0.5)
x_1 = pm.Data('var_x1', df['x1'])
x_2 …Run Code Online (Sandbox Code Playgroud) 考虑一对简单的泛型类:
T = TypeVar("T", str, int)
class Base(Generic[T]):
def __init__(self, v: T):
self.v: T = v
@property
def value(self) -> T:
return self.v
class Child(Base[T]):
def __init__(self, v: T):
super().__init__(v)
x = Child(123)
reveal_type(x.value)
Run Code Online (Sandbox Code Playgroud)
使用时T = TypeVar("T")效果符合预期。如图所示的限制TypeVar会产生以下错误:
error: Argument 1 to "__init__" of "Base" has incompatible type "str"; expected "T"
error: Argument 1 to "__init__" of "Base" has incompatible type "int"; expected "T"
note: Revealed type is 'builtins.int*'
Run Code Online (Sandbox Code Playgroud)
请注意,这reveal_type仍然有效。
另一个区别是受限制的TypeVar需要类型注释来进行self.v赋值,而不受限制的则不需要。 …
使用joblib.Memory我可以缓存某些函数的结果,但我想知道是否有一种有效的方法来找出有缓存值的参数列表。
例如,假设该函数slow_func是针对 的某些值进行计算和缓存的x,我可以找出我缓存了哪些值吗?
from joblib import Memory
mem = Memory(location='cache')
@mem.cache
def slow_func(x):
return x
Run Code Online (Sandbox Code Playgroud)