python - 调整参数 SVM

标签 python machine-learning scikit-learn classification svm

我想对图像中显示的数据进行分类:

enter image description here

为此,我尝试使用 SVM:

X =  df[['score','word_lenght']].values
Y = df['is_correct'].values
clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,Y)
clf.coef_
clf = svm.SVC(kernel='linear')
clf.fit(X, Y)

这是我得到的结果:

enter image description here

但我想要一个更灵活的模型,比如红色模型,或者如果可能的话,比如蓝线。 我可以使用哪些参数来播放以更接近所需的响应?

enter image description here

另外,我不太明白垂直(yy)轴的比例是如何创建的,它太大了。

w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(0.85, 1)
yy =   (a * xx - (clf.intercept_[0]) / w[1])*1

最佳答案

首先,如果数据大小合理,您可以尝试执行 GridSearch ,由于显然您正在处理文本,请考虑以下示例::

def main():
    pipeline = Pipeline([
        ('vect', TfidfVectorizer(ngram_range=(2,2), min_df=1)),
        ('clf',SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
                     gamma=1e-3, kernel='rbf', max_iter=-1, probability=False, random_state=None,
                     shrinking=True, tol=0.001, verbose=False))
    ])


    parameters = {
        'vect__max_df': (0.25, 0.5),
        'vect__use_idf': (True, False),
        'clf__C': [1, 10, 100, 1000],

    }


    X, y = X, Y.as_matrix()
    X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.5)
    grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1, verbose=1, scoring='accuracy')
    grid_search.fit(X_train, y_train)
    print 'Best score: %0.3f' % grid_search.best_score_
    print 'Best parameters set:'
    best_parameters = grid_search.best_estimator_.get_params()
    for param_name in sorted(parameters.keys()):
        print '\t%s: %r' % (param_name, best_parameters[param_name])


if __name__ == '__main__':
main()

请注意,我使用 tf-idf 对数据(文本)进行了矢量化。 scikit-learn 项目还实现了 RandomizedSearchCV 。最后,还有其他有趣的工具,例如 Tpot使用遗传编程的项目,希望这有帮助!

关于python - 调整参数 SVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38427358/

相关文章:

python - 使用 TextBlob : every instance predicted as negative when adding more sample size 的朴素贝叶斯文本分类

python - 如何避免重新训练机器学习模型

classification - sklearn 是否支持成本矩阵?

python - matplotlib scatter 在 ipython 笔记本中有效,但在控制台中无效

python - Logits 的形状错误

python - Facebook 连接 : redirect to white page with only the facebook logo on it (amazon ec2)

python - 使用 Python 中的列名列表在 MariaDB 中创建表

machine-learning - 网格上的 CNN 回归 - 卷积神经网络的局限性?

python - 0 和 1 之间的余弦相似度

python - 从 Scikit 学习的预测中抑制控制台消息