模型回归Sklearn

时间:2018-08-14 12:08:20

标签: python

这是我的程序

 X = np.array([[1, 2, 4],[2, 3, 9]]).T    
 print(X)
 y = np.array([1, 4, 16])
 X_poly = PolynomialFeatures(degree=2)
 X_poly_return = X_poly.fit_transform(X)
 print(X_poly)
 model = LinearRegression(fit_intercept = False)
 model.fit(X_poly_return,y)
 print('Coefficients: \n', model.coef_)
 print('Others: \n', model.intercept_)
 print(X_poly.powers_)
 X_predict = np.array([[3,3]])
 print ('prediction: \n') 
 print(model.predict(X_poly.transform(X_predict)))

输出:

 [-0.10133796 0.1456888 -0.01660059 0.54831516 0.45019822 -0.11496531] 
 powers : [[0 0] [1 0] [0 1] [2 0] [1 1] [0 2]] 
 prediction of [3,3] is [8.23785927] 

因此,方程式(模型为):

y = -0.11496531 * x *x + 0.45019822 * z *z     ?

我的问题是我们如何具有预测值8.23785927?

1 个答案:

答案 0 :(得分:2)

考虑powers,不是吗?

y = (-0.10133796)*(x^0)*(z^0) + (0.1456888)*(x^1)*(z^0) + 
    (-0.01660059)*(x^0)*(z^1) + (0.54831516)*(x^2)*(z^0) + 
    (0.45019822)*(x^1)*(z^1)  + (-0.11496531)*(x^0)*(z^2)

对于x=z=3,其变为8.2378593