svm_kernel.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- encoding:utf-8 -*-
  2. from sklearn import svm
  3. from sklearn import datasets
  4. from sklearn.model_selection import train_test_split as ts
  5. '''
  6. ‘linear’:线性核函数
  7. ‘poly’:多项式核函数
  8. ‘rbf’:径像核函数/高斯核
  9. ‘sigmod’:sigmod核函数
  10. ‘precomputed’:核矩阵
  11. '''
  12. #import our data
  13. iris = datasets.load_iris()
  14. X = iris.data
  15. y = iris.target
  16. #split the data to 7:3
  17. X_train,X_test,y_train,y_test = ts(X,y,test_size=0.3)
  18. print y_test
  19. # select different type of kernel function and compare the score
  20. # kernel = 'rbf'
  21. clf_rbf = svm.SVC(kernel='rbf')
  22. clf_rbf.fit(X_train,y_train)
  23. score_rbf = clf_rbf.score(X_test,y_test)
  24. print("The score of rbf is : %f"%score_rbf)
  25. # kernel = 'linear'
  26. clf_linear = svm.SVC(kernel='linear')
  27. clf_linear.fit(X_train,y_train)
  28. score_linear = clf_linear.score(X_test,y_test)
  29. print("The score of linear is : %f"%score_linear)
  30. # kernel = 'poly'
  31. clf_poly = svm.SVC(kernel='poly')
  32. clf_poly.fit(X_train,y_train)
  33. score_poly = clf_poly.score(X_test,y_test)
  34. print("The score of poly is : %f"%score_poly)