老师要用pipeline实现多分类(比如用ovo)含多项式项的逻辑回归模型,下面这两种写法哪个对呢,我运行了一下都是可以运行的
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.multiclass import OneVsOneClassifier
from sklearn.linear_model import LogisticRegression
import numpy as np
X = np.random.normal(0, 1, size=(200, 2))
y = np.array((X[:,0]**2+X[:,1]**2)<1.5, dtype=‘int’)
1、
log_reg = LogisticRegression()
def PolynomialLogisticRegression(degree):
return Pipeline([
(‘poly’, PolynomialFeatures(degree=degree)),
(‘std_scaler’, StandardScaler()),
(‘log_reg’, OneVsOneClassifier(log_reg))
])
poly_log_reg = PolynomialLogisticRegression(degree=2)
poly_log_reg.fit(X, y)
print(poly_log_reg.score(X, y))
2、
def PolynomialLogisticRegression(degree):
return Pipeline([
(‘poly’, PolynomialFeatures(degree=degree)),
(‘std_scaler’, StandardScaler()),
(‘log_reg’, LogisticRegression())
])
poly_log_reg = PolynomialLogisticRegression(degree=2)
poly_log_reg=OneVsOneClassifier(poly_log_reg)
poly_log_reg.fit(X, y)
print(poly_log_reg.score(X, y))