调整参数 SVM

Tune parameters SVM

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

为此,我尝试使用 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)

这是我得到的结果:

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

另外,我不太明白垂直(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. Finally, there are also other interesting tools like Tpot 使用遗传编程的项目,希望对您有所帮助!