调整SVM参数

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

enter image description here

为此,我尝试使用SVM:

X =  df[['score','word_lenght']].valuesY = df['is_correct'].valuesclf = 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

回答:

首先,如果数据量合理,你可以尝试进行网格搜索。由于你似乎在处理文本数据,请考虑以下示例:

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项目还实现了随机搜索。最后,还有一些其他有趣的工具,比如使用遗传编程的Tpot项目,希望这些对你有帮助!

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注