小编all*_*ang的帖子

sklearn.ensemble.AdaBoostClassifier不能将SVM作为base_estimator加入?

我正在做一个文本分类任务.现在我想ensemble.AdaBoostClassifierLinearSVCas base_estimator.但是,当我尝试运行代码时

clf = AdaBoostClassifier(svm.LinearSVC(),n_estimators=50, learning_rate=1.0,    algorithm='SAMME.R')
clf.fit(X, y)
Run Code Online (Sandbox Code Playgroud)

发生错误. TypeError: AdaBoostClassifier with algorithm='SAMME.R' requires that the weak learner supports the calculation of class probabilities with a predict_proba method

第一个问题是无法svm.LinearSVC()计算类概率?如何计算概率?

然后我更改参数algorithm并再次运行代码.

clf = AdaBoostClassifier(svm.LinearSVC(),n_estimators=50, learning_rate=1.0, algorithm='SAMME')
clf.fit(X, y)
Run Code Online (Sandbox Code Playgroud)

这一次TypeError: fit() got an unexpected keyword argument 'sample_weight'发生了.正如在AdaBoostClassifier中所说,Sample weights. If None, the sample weights are initialized to 1 / n_samples.即使我分配了一个整数n_samples,也会发生错误.

第二个问题是什么n_samples意思?如何解决这个问题呢? …

python machine-learning scikit-learn ensemble-learning

14
推荐指数
1
解决办法
1万
查看次数

Python NLP:TypeError:并非在字符串格式化期间转换所有参数

我尝试了"使用python进行自然语言处理"的代码,但发生了类型错误.

import nltk
from nltk.corpus import brown

suffix_fdist = nltk.FreqDist()
for word in brown.words():
    word = word.lower()
    suffix_fdist.inc(word[-1:])
    suffix_fdist.inc(word[-2:])
    suffix_fdist.inc(word[-3:])
common_suffixes = suffix_fdist.items()[:100]

def pos_features(word):
    features = {}
    for suffix in common_suffixes:
        features['endswith(%s)' % suffix] = word.lower().endswith(suffix)
    return features
pos_features('people')
Run Code Online (Sandbox Code Playgroud)

错误如下:

Traceback (most recent call last):
  File "/home/wanglan/javadevelop/TestPython/src/FirstModule.py", line 323, in <module>
    pos_features('people')
  File "/home/wanglan/javadevelop/TestPython/src/FirstModule.py", line 321, in pos_features
    features['endswith(%s)' % suffix] = word.lower().endswith(suffix)
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)

有谁能帮助我找出我错在哪里?

python nlp typeerror

6
推荐指数
1
解决办法
6660
查看次数

python词典的更新方法不起作用

我有两本词典.

a = {"ab":3, "bd":4}
b = {"cd":3, "ed":5}` 
Run Code Online (Sandbox Code Playgroud)

我想把它们结合起来{'bd': 4, 'ab': 3, 'ed': 5, 'cd': 3}.

由于说,a.update(b)可以完成它.但是当我尝试时,我得到:

type(a.update(b)) #--> type 'NoneType'
Run Code Online (Sandbox Code Playgroud)

有谁愿意向我解释为什么我不能获得字典类型?

我也试过这个,它做得很好:

type(dict(a,**b)) #-->type 'dict'
Run Code Online (Sandbox Code Playgroud)

这两种方法有什么区别,为什么第一种方法不起作用?

python dictionary

1
推荐指数
2
解决办法
3068
查看次数