Answers:
您仍然可以使用sklearn.linear_model.LinearRegression。只需使输出y
成为具有与因变量一样多的列的矩阵。如果您想要非线性的东西,可以尝试使用不同的基函数,使用多项式特征或使用不同的回归方法(例如NN)。
获取元组的代码是:
def generate_all_tuples_for_monomials(N,D):
if D == 0:
seq0 = N*[0]
sequences_degree_0 = [seq0]
S_0 = {0:sequences_degree_0}
return S_0
else:
# S_all = [ k->S_D ] ~ [ k->[seq0,...,seqK]]
S_all = generate_all_tuples_for_monomials(N,D-1)# S^* = (S^*_D-1) U S_D
print(S_all)
#
S_D_current = []
# for every prev set of degree tuples
#for d in range(len(S_all.items())): # d \in [0,...,D_current]
d = D-1
d_new = D - d # get new valid degree number
# for each sequences, create the new valid degree tuple
S_all_seq_for_deg_d = S_all[d]
for seq in S_all[d]:
for pos in range(N):
seq_new = seq[:]
seq_new[pos] = seq_new[pos] + d_new # seq elements dd to D
if seq_new not in S_D_current:
S_D_current.append(seq_new)
S_all[D] = S_D_current
return S_all
如果您知道线性代数,那么进行回归应该很容易。
c = pseudo_inverse(X_poly)*y
例。如果您对泛化感兴趣,则最好进行正则化线性回归。