我有两个m维度的列表,并希望对它们进行独立的回归:
给出两个清单:
l = [[l1, l2, l3, l4, l5],[l6, l7, l8, l9, l10]...]
&安培;
n = [[n1, n2, n3, n4, n5], [n6, n7, n8, n9, n10] ...]
我想[l1, l2, l3, l4, l5]
[n1, n2, n3, n4, n5]
和[l6, l7, l8, l9, l10]
[n6, n7, n8, n9, n10]
(...),并将测试版值保存到空列表中。
我原本试图简单地使用:
regression.linear_model.OLS(l, sm.add_constant(n)).fit()
但它似乎没有表现出所需的行为。
做
[regression.linear_model.OLS(l[x], sm.add_constant(n[x]).fit() for x in range(0, len(l)]
然而运行时间太长,因为我有超过80000次回归运行。
答案 0 :(得分:1)
这看起来像是你自负,是吗?这似乎对我来说相当快。
import numpy as np
from scipy import stats
#Simulating your data
f = lambda x: 2*x+3 + np.random.normal(0,0.5)
X = [np.random.rand(5) for i in range(80000)]
Y = [f(x) for x in X]
#Store coefficients here
models = []
#Loop through the data
for x,y in zip(X,Y):
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
#Add the coefficient and the intercept to the list
models.append([slope,intercept])
np.array(models[:5])
>>>array([[ 2. , 3.47],
[ 2. , 2.66],
[ 2. , 2.94],
[ 2. , 3.01],
[ 2. , 2.75]])