Python 学习如何获取LinearSVC分类器的决策概率
我正在使用scikit learn的linearSVC分类器进行文本挖掘。我将y值作为标签0/1,将X值作为文本文档的TFIDFvectorier 我使用下面这样的管道Python 学习如何获取LinearSVC分类器的决策概率,python,machine-learning,scikit-learn,svm,Python,Machine Learning,Scikit Learn,Svm,我正在使用scikit learn的linearSVC分类器进行文本挖掘。我将y值作为标签0/1,将X值作为文本文档的TFIDFvectorier 我使用下面这样的管道 pipeline = Pipeline([ ('count_vectorizer', TfidfVectorizer(ngram_range=(1, 2))), ('classifier', LinearSVC()) ]) 对于预测,我想得到一个数据点被分类为 1在范围(0,1) 我目
pipeline = Pipeline([
('count_vectorizer', TfidfVectorizer(ngram_range=(1, 2))),
('classifier', LinearSVC())
])
对于预测,我想得到一个数据点被分类为
1在范围(0,1)
我目前正在使用决策功能功能
pipeline.decision_function(test_X)
但是,它返回的正值和负值似乎表示信心。我也不太清楚他们的意思
但是,是否有办法获得0-1范围内的值
例如,这里是一些数据点的决策函数的输出
-0.40671879072078421,
-0.40671879072078421,
-0.64549376401063352,
-0.40610652684648957,
-0.40610652684648957,
-0.64549376401063352,
-0.64549376401063352,
-0.5468745098794594,
-0.33976011539714374,
0.36781572474117097,
-0.094943829974515004,
0.37728641897721765,
0.2856211778200019,
0.11775493140003235,
0.19387473663623439,
-0.062620918785563556,
-0.17080866610522819,
0.61791016307670399,
0.33631340372946961,
0.87081276844501176,
1.026991628346146,
0.092097790098391641,
-0.3266704728249083,
0.050368652422013376,
-0.046834129250376291,
你不能。
但是,您可以将sklearn.svm.SVC
与kernel='linear'
和probability=True
它可能会运行更长的时间,但您可以使用predict\u proba
方法从该分类器获取概率
clf=sklearn.svm.SVC(kernel='linear',probability=True)
clf.fit(X,y)
clf.predict_proba(X_test)
你不能。
但是,您可以将sklearn.svm.SVC
与kernel='linear'
和probability=True
它可能会运行更长的时间,但您可以使用predict\u proba
方法从该分类器获取概率
clf=sklearn.svm.SVC(kernel='linear',probability=True)
clf.fit(X,y)
clf.predict_proba(X_test)
如果您坚持使用LinearSVC类,您可以将其包装在对象中并适合校准的分类器,这将为您提供一个概率分类器
from sklearn.svm import LinearSVC
from sklearn.calibration import CalibratedClassifierCV
from sklearn import datasets
#Load iris dataset
iris = datasets.load_iris()
X = iris.data[:, :2] # Using only two features
y = iris.target #3 classes: 0, 1, 2
linear_svc = LinearSVC() #The base estimator
# This is the calibrated classifier which can give probabilistic classifier
calibrated_svc = CalibratedClassifierCV(linear_svc,
method='sigmoid', #sigmoid will use Platt's scaling. Refer to documentation for other methods.
cv=3)
calibrated_svc.fit(X, y)
# predict
prediction_data = [[2.3, 5],
[4, 7]]
predicted_probs = calibrated_svc.predict_proba(prediction_data) #important to use predict_proba
print predicted_probs
以下是输出:
[[ 9.98626760e-01 1.27594869e-03 9.72912751e-05]
[ 9.99578199e-01 1.79053170e-05 4.03895759e-04]]
它显示每个数据点的每个类的概率。如果您坚持使用LinearSVC类,您可以将其包装在一个对象中,并安装校准的分类器,该分类器将为您提供概率分类器
from sklearn.svm import LinearSVC
from sklearn.calibration import CalibratedClassifierCV
from sklearn import datasets
#Load iris dataset
iris = datasets.load_iris()
X = iris.data[:, :2] # Using only two features
y = iris.target #3 classes: 0, 1, 2
linear_svc = LinearSVC() #The base estimator
# This is the calibrated classifier which can give probabilistic classifier
calibrated_svc = CalibratedClassifierCV(linear_svc,
method='sigmoid', #sigmoid will use Platt's scaling. Refer to documentation for other methods.
cv=3)
calibrated_svc.fit(X, y)
# predict
prediction_data = [[2.3, 5],
[4, 7]]
predicted_probs = calibrated_svc.predict_proba(prediction_data) #important to use predict_proba
print predicted_probs
以下是输出:
[[ 9.98626760e-01 1.27594869e-03 9.72912751e-05]
[ 9.99578199e-01 1.79053170e-05 4.03895759e-04]]
它显示了每个数据点的每个类的概率。SVC与LinearSVC不同吗?这会导致不同的结果吗?SVC中0和1之间的概率是否也能预测概率?是的。预测概率返回概率。SVC使用表示定理进行训练(krenel技巧),所以它可能运行更长时间,但结果应该非常相似(可能有点不同,取决于实现)@萨基布:太好了!我会试试这个,但看起来这就是我需要的。不过我还有一个问题。决策函数和预测概率之间的区别是什么?决策函数是你的假设(模型)。预测概率只是假设返回的概率。但这是一个非常宽泛的定义@SakibIs SVC与LinearSVC不同?这会导致不同的结果吗?SVC中0和1之间的概率是否也能预测概率?是的。预测概率返回概率。SVC使用表示定理进行训练(krenel技巧),所以它可能运行更长时间,但结果应该非常相似(可能有点不同,取决于实现)@萨基布:太好了!我会试试这个,但看起来这就是我需要的。不过我还有一个问题。决策函数和预测概率之间的区别是什么?决策函数是你的假设(模型)。预测概率只是假设返回的概率。但这是一个非常宽泛的定义@Sakibhey@javad,你们能提供一个使用
CalibredClassifiedRCV
的工作示例吗?嘿@javad,你们能提供一个使用CalibredClassifiedRCV
的工作示例吗?