【问题标题】:Tune parameters SVM调整参数 SVM
【发布时间】:2016-11-20 12:12:14
【问题描述】:

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

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

【问题讨论】:

    标签: python machine-learning scikit-learn classification svm


    【解决方案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 项目使用基因编程,希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-06-10
      • 2014-11-28
      • 2021-10-12
      • 1970-01-01
      • 2013-01-12
      • 2015-06-20
      • 2020-08-07
      • 2017-09-28
      • 2015-07-12
      相关资源
      最近更新 更多