from sklearn import svm, model_selection
import pandas as pd
import numpy as np
#支持向量机主要调节的参数有:C、kernel、degree、gamma、coef0。
#C 默认 = 1.0正则化参数。
#kernel {‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’}, default =’rbf’
#degreeint, default = 3 多项式核函数 ('poly') 的次数 浮点数
#gamma{‘scale’, ‘auto’} or float, default=’scale’
#coef0 default=0.0 核函数中的独立项。它仅在“poly”和“sigmoid”中有意义。
from sklearn.model_selection import GridSearchCV# 设置随机种子
random_state_model = 40
feature_number = 45
filepath = r"xls"
ExcelData = pd.read_excel(filepath) # reading file
ExcelData = np.array(ExcelData)
classifypointdata = ExcelData[:, 0:feature_number]
classifypointlabel = ExcelData[:, -1]
# 切分数据,固定随机种子(random_state)时,同样的代码,得到的训练集数据相同。
# 此处运行完的结果可能是string类型之后用于计算时记得类型转换
train_data, test_data, train_label, test_label = model_selection.train_test_split(classifypointdata, classifypointlabel, random_state=random_state_model,train_size=0.8)
SVM_classifier = svm.SVC()
#kernel核心函数
# SVM_params = [{'kernel': ['linear'], 'C': [1, 10, 100]},
# {'kernel': ['poly'], 'C': [1,10], 'degree': [1,3,5]},
# {'kernel': ['rbf'], 'C': [1, 10], 'gamma': [0.01,0.00001]}]
SVM_params = {'kernel': ['linear'], 'C': [1, 10]}SVM_GSC = GridSearchCV(SVM_classifier, SVM_params, cv=3)
SVM_GSC.fit(train_data,train_label)
#获取网格搜索每个参数组合
SVM_GSC.cv_results_['params']
#获取网格搜索每个参数组合所对应的平均测试分值
SVM_GSC.cv_results_['mean_test_score']
#获取最好的参数
print(SVM_GSC.best_params_)
print(SVM_GSC.best_score_)
print(SVM_GSC.best_estimator_)