我正在尝试计算预测概率。我编写了一个程序来进行计算,但速度非常慢,处理大数据集时需要很长时间。
目标是使用LinearSVC
和OneVsRestClassifier
来计算SVM模型中的每个预测概率,但遇到了错误
AttributeError: 'LinearSVC' object has no attribute 'predict_proba'
由于上述错误,我尝试了以下方法
代码
from sklearn import svmmodel_1 = svm.SVC(kernel='linear', probability=True)from sklearn.preprocessing import LabelEncoderX_1 = df["Property Address"]lb = LabelEncoder()X_2 = lb.fit_transform(X_1)y_1 = df["Location_Name"]y_2 = lb.fit_transform(y_1)test_1 = test["Property Address"]lb = LabelEncoder()test_1 = lb.fit_transform(test_1)X_2= X_2.reshape(-1, 1)y_2= y_2.reshape(-1, 1)test_1 = test_1.reshape(-1, 1)model_1.fit(X_2, y_2)results = model_1.predict_proba(test_1)[0]# gets a dictionary of {'class_name': probability}prob_per_class_dictionary = dict(zip(model.classes_, results))
有没有其他方法可以完成相同的任务?请提出建议
回答:
如果你需要使用predict_proba
方法,可以使用sklearn的CalibratedClassifierCV。
或者你可以使用逻辑回归。
如果你的问题与速度有关,尝试考虑使用sklearn.svm
中的LinearSVC
来代替SVC(kernel='linear')
。它更快。