我正在尝试使用SGDClassifier(loss ='hinge')对文本进行分类,为此我也希望获得其概率。由于对于SGDClassifier(loss ='hinge')没有predict_proba(),我经历了this帖子,并知道可以通过CalibratedClassifierCV来实现
我有一个带有小文本及其对应类的数据框
import pandas as pd
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.calibration import CalibratedClassifierCV
df = pd.DataFrame({'Name':['An Apple a day','Apple is a fruit','Oranges are grown in asia','Chalk and duster','Cheese is very nice','Duster is used to wipe from chalk','I like Oranges'],'Class':['apple','apple','oranges','duster','cheese','duster','oranges']})
sgd = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, random_state=42, max_iter=5)),
])
sgd = CalibratedClassifierCV(sgd, cv=3, method='sigmoid')
sgd.fit(df['Name'], df['Class']) #--> This line gives me error
出现错误
Traceback (most recent call last):
File "<ipython-input-27-bf5d0ceadb82>", line 1, in <module>
calibrated_clf.fit(df['Name'], df['Class'])
File "C:\Users\20300975\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\calibration.py", line 133, in fit
force_all_finite=False)
File "C:\Users\20300975\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 756, in check_X_y
estimator=estimator)
File "C:\Users\20300975\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 527, in check_array
array = np.asarray(array, dtype=dtype, order=order)
File "C:\Users\20300975\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'An Apple a day'
有人对此有任何想法吗?
答案 0 :(得分:0)