【问题标题】:How to improve speed of the program for large data in python如何提高python中大数据程序的速度
【发布时间】:2018-10-30 12:10:24
【问题描述】:

我正在尝试计算预测概率。我写了一个程序,它正在计算,但速度非常慢,并且需要大量时间来处理大型数据集。

目的是使用LinearSVCOneVsRestClassifier计算SVM模型中的每个预测概率但得到错误

AttributeError: 'LinearSVC' object has no attribute 'predict_proba'

由于上述错误,我在下面尝试了

代码

from sklearn import svm

model_1 = svm.SVC(kernel='linear', probability=True)

from sklearn.preprocessing import LabelEncoder

X_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))

还有其他方法可以完成相同的任务吗?请建议

【问题讨论】:

  • 我不确定我是否理解。您的代码是慢还是会引发错误?两个截然不同的问题。
  • 我已经发布了一个帖子,但没有得到解决方案 [stackoverflow.com/questions/53040262/….我写了一个非常慢的代码。我在上面的代码中没有收到任何错误。

标签: python python-3.x machine-learning stanford-nlp


【解决方案1】:

如果您需要使用 predict_proba 方法,可以使用 sklearns CalibratedClassifierCV

或者您可以使用Logistic Regression

如果您的问题与速度有关,请尝试考虑在sklearn.svm 中使用LinearSVC 而不是SVC(kernel='linear')。它更快。

【讨论】:

  • 如何使用LinearSVC计算预测概率?
  • 您尝试过例如:calibrated_svc = CalibratedClassifierCV(LinearSVC(), method='sigmoid', cv=3) 吗?然后拟合它,并使用 predict_proba
  • 是的,出现错误:ValueError: Requesting 3-fold cross-validation but provided less than 3 examples for at least one class. - 代码:calibrated_svc.fit(X_2, y_2)
  • 您了解错误吗?如果您不想使用 calibrated_svc,只需使用 sklearn.linear_model.LogisticRegression 作为您的模型。
【解决方案2】:

正如另一个答案中所建议的,LinearSVCSVC(kernel='linear') 快。

关于概率,SVC 没有predict_proba()。相反,您必须将其probability 超参数设置为TrueLink

提示: SVM 更适合小型数据集,因此更喜欢使用其他算法来处理大型数据集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2022-12-20
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多