from sklearn.pipeline import _name_estimators
class MajorityVoteClassifier(BaseEstimator,ClassifierMixin):
def __init__(self,classifiers,vote='classlabel',weights=None):
self.classifiers = classifiers
self.named_classifiers={key:value for key,value in
_name_estimators(classifiers)}
self.vote=vote
self.weights=weights
clf1=LogisticRegression(penalty='l2',C=0.001,random_state=1)
clf2=DecisionTreeClassifier(max_depth=1,criterion='entropy',
random_state=0)
clf3=KNeighborsClassifier(n_neighbors=1,p=2,metric='minkowski')
pipe1=Pipeline([['sc',StandardScaler()],['clf',clf1]])
pipe3=Pipeline([['sc',StandardScaler()],['clf',clf3]])
mv_clf=MajorityVoteClassifier(classifiers=[pipe1,clf2,pipe3])我无法理解_name_estimators是如何工作的,所以有人能解释一下_name_estimators在这段代码中做了什么吗?
发布于 2019-09-12 20:50:34
您可以在交互模式下运行以下代码:
from sklearn.pipeline import _name_estimators
estimators = ['a', 'a', 'b' ]
_name_estimators(estimators)
# >>> [('a-1', 'a'), ('a-2', 'a'), ('b', 'b')]所以基本上它返回具有唯一键的元组。每个元组包含估计器+,如果估计器是重复的,则它的出现和原始估计器的值。
发布于 2019-09-12 21:02:13
您为函数_name_estimators提供了一个包含n个估计器的列表,它将返回一个包含n个元组的列表。每个元组中的第一个组件是描述估计器名称的字符串,每个元组的第二个组件是估计器对象
from sklearn.linear_model import LinearRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.pipeline import _name_estimators
clf = GaussianNB()
clf2 = LinearRegression()
res = _name_estimators([clf, clf2])
print(res)
print(type(res))
print()
for p in res:
print(type(p[0]))
print(type(p[1]))
#[('gaussiannb', GaussianNB(priors=None, var_smoothing=1e-09)), ('linearregression', LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False))]
#<class 'list'>
#<class 'str'>
#<class 'sklearn.naive_bayes.GaussianNB'>
#<class 'str'>
#<class 'sklearn.linear_model.base.LinearRegression'>https://stackoverflow.com/questions/57906598
复制相似问题