在下面的代码中,名估计器是做什么的?

2024-09-30 01:26:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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在这段代码中做了什么


Tags: keynameselfvaluerandomstatevoteclf
2条回答

您可以在交互模式下运行:

from sklearn.pipeline import _name_estimators

estimators = ['a', 'a', 'b' ]
_name_estimators(estimators)
# >>> [('a-1', 'a'), ('a-2', 'a'), ('b', 'b')]

所以基本上它返回元组,带有唯一的键。每个元组包含估计量+如果估计量是重复的,则包含它的出现和原始估计量值。你知道吗

你给函数名估计器一个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'>


相关问题 更多 >

    热门问题