在Python中获取我的sklearn多项式回归模型的系数

时间:2018-10-23 08:30:59

标签: python scikit-learn

我想在Python中获取我的sklearn多项式回归模型的系数,以便可以在其他地方编写该方程式。即ax1 ^ 2 + ax + bx2 ^ 2 + bx2 + c

我已经在其他地方查看了答案,但似乎无法获得解决方案,除非我只是不知道自己在看什么。

from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 2)
X_poly = poly_reg.fit_transform(X_train)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly,y_train)

lin_reg_2.coef_

2 个答案:

答案 0 :(得分:0)

支持向量回归:

from sklearn.svm import SVR
import numpy as np
n_samples, n_features = 10, 5
np.random.seed(0)
y = np.random.randn(n_samples)
X = np.random.randn(n_samples, n_features)
clf = SVR(kernel="poly",degree=3,gamma="scale",C=0.8)
clf.fit(X, y) 
clf.predict(X)

sklearn.svm.SVR类的定义:

class sklearn.svm.SVR(kernel=’rbf’, degree=3, gamma=’auto_deprecated’, coef0=0.0, tol=0.001, C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False, max_iter=-1)

答案 1 :(得分:0)

此代码段应该有效,它取自我自己的脚本:

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=2)
poly_x_inliers = poly_reg.fit_transform(x_inliers)

regressor = LinearRegression()
regressor.fit(poly_x_inliers, y_inliers)

reg_label = "Inliers coef:%s - b:%0.2f" % \
            (np.array2string(regressor.coef_,
                             formatter={'float_kind': lambda fk: "%.3f" % fk}),
            regressor.intercept_)
print(reg_label)

您应检查:https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html