【问题标题】:how to do fine tune SVM hyperparameter with Kfold如何使用 Kfold 微调 SVM 超参数
【发布时间】:2021-11-10 16:35:41
【问题描述】:

我想在代码中使用 Gridsearch 来微调我的 SVM 模型,我已经从其他 github 复制了这段代码,它在我的交叉折叠中运行得非常好。

X = Corpus.drop(['text','ManipulativeTag','compound'],axis=1).values  # !!! this drops compund because of Naive Bayes
y = Corpus['ManipulativeTag'].values

kf = KFold(n_splits=5, shuffle=True, random_state=1)
# Create splits
splits = kf.split(X)


# Access the training and validation indices of splits
kfold_accuracy = {}
kfold_precision = {}
kfold_f = {}
kfold_recall = {}


for i, (train_index, val_index) in enumerate(splits):
  print("Split n°: ", i)
  # Setup the training and validation data
  X_train, y_train = X[train_index], y[train_index]
  # print("training:", train_index, "validations:", val_index)
  X_val,y_val= X[val_index], y[val_index]

  SVM = svm.SVC(C=1.0, kernel='linear', random_state=1111, probability=True) ### the base estimator

  SVM.fit(X_train, y_train)

  # predict the labels on validation dataset
  predictions = SVM.predict(X_val)

  # Use accuracy_score function to get the accuracy
  kfold_accuracy[i] = accuracy_score(y_val, predictions)
  kfold_precision[i] = precision_score(y_val, predictions)
  kfold_f[i] = f1_score(y_val,predictions)
  kfold_recall[i] = recall_score(y_val,predictions)
  

但是,当我尝试实现 Gridsearch 时,我遇到的大多数文章都使用 train_test_split() 而不是我的 kf.split(),我无法找到合适的位置来推动 GridSearchCV() 行:

GridSearchCV(estimator=classifier,
                     param_grid=grid_param,
                     scoring='accuracy',
                     cv=5,
                     n_jobs=-1)

【问题讨论】:

    标签: python svm grid-search k-fold


    【解决方案1】:

    我在这里找到了我的解决方案:Grid search and cross validation SVM

    我从帖子中复制了这个:

    tuned_parameters =  [{'kernel': ['rbf'], 'gamma': [1e-2, 1e-3, 1e-4, 1e-5],
                     'C': [0.001, 0.10, 0.1, 10, 25, 50, 100, 1000]},
                    {'kernel': ['sigmoid'], 'gamma': [1e-2, 1e-3, 1e-4, 1e-5],
                     'C': [0.001, 0.10, 0.1, 10, 25, 50, 100, 1000] },{'kernel': ['linear'], 'C': [0.001, 0.10, 0.1, 10, 25, 50, 100, 1000]}]              
    

    我保留了代码中的所有内容,只在循环中添加了 Gridsearch() 来对循环进行更改:

    for i, (train_index, val_index) in enumerate(splits):
      print("Split n°: ", i)
    
      # Setup the training and validation data
      X_train, y_train = X[train_index], y[train_index]
      X_val,y_val= X[val_index], y[val_index]
    
      # this is where I put GridSearch()
      # here cv cannot be 1, so I put 2 instead  
      SVM = GridSearchCV(SVC(), tuned_parameters, cv=2, scoring='accuracy')
      SVM.fit(X_train, y_train) 
    
      print("Best parameters set found on development set:")
      print()
      print(SVM.best_params_)
    

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 2020-11-17
      • 2021-10-23
      • 1970-01-01
      • 2015-07-12
      • 2016-11-20
      • 2018-12-17
      • 2013-01-12
      • 2020-08-07
      相关资源
      最近更新 更多