我的代码功能正常,但我正在重复一个块几次,以改变多项式变量degree。我认为这可以而且应该循环以允许更快的迭代,但我不确定如何做到这一点。在下面的代码之前,我生成了我保留用于绘图的train_测试分割
经过几次迭代后,我在y_预测上使用np.vstack来创建单个数组
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
### degree 1 ####
poly1 = PolynomialFeatures(degree=1)
x1_poly = poly1.fit_transform(X_train)
linreg1 = LinearRegression().fit(x1_poly, y_train)
pred_1= poly1.transform(x_prediction_data)
y1_poly_pred=linreg1.predict(pred_1)
### degree 3 #####
poly3 = PolynomialFeatures(degree=3)
x3_poly = poly3.fit_transform(X_train)
linreg3 = LinearRegression().fit(x3_poly, y_train)
pred_3= poly3.transform(x_prediction_data)
y3_poly_pred=linreg3.predict(pred_3)
#### ect... will make several other degree = 6, 9 ...
我建议你在字典里收集答案,但为了简单起见,我创建了一个列表
代码在i上迭代,i是多项式的阶数。训练模型等,然后收集其答案
相关问题 更多 >
编程相关推荐