import matplotlib.pyplot as plt标签:SVM,--,PYTHON,train,import,print,new,sklearn,svm From: https://www.cnblogs.com/maxzz/p/16869030.html
from sklearn import svm
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.model_selection import cross_val_score
import warnings
warnings.filterwarnings("ignore")
from sklearn import svm, datasets, feature_selection
from sklearn.feature_selection import SelectPercentile, f_classif
#data = pd.read_excel('XGboost补充.xlsx')
data = pd.read_excel('样本内.xlsx')
X = data.iloc[:, 1:].values
y = data.iloc[:, 0].values
#from sklearn.feature_selection import VarianceThreshold
#vt = VarianceThreshold()
#vt.fit_transform(X)
#var_thd = pd.DataFrame(vt.variances_, columns = ["Variance"])
#var_thd = var_thd.reset_index()
#sorts = var_thd.sort_values('Variance',ascending=0)
#print(sorts)
#sorts_DF = pd.DataFrame(sorts)
#sorts_DF.to_csv(path_or_buf="SVM特征选择.csv",index=False)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=0)
#归一化处理
from sklearn.preprocessing import StandardScaler
StandardScaler = StandardScaler()
StandardScaler.fit(X_train)
X_train = StandardScaler.transform(X_train)
X_test = StandardScaler.transform(X_test)
#调参
from sklearn.model_selection import GridSearchCV
grid = GridSearchCV(SVC(), param_grid={"C": [0.1, 1, 10], "gamma": [1, 0.1, 0.01]}, cv=4) # 总共有9种参数组合的搜索空间
grid.fit(X, y)
print("The best parameters are %s with a score of %0.2f"
% (grid.best_params_, grid.best_score_))
clf_svm = svm.SVC(C=10, gamma=0.01,max_iter=200, class_weight='balanced')
clf_svm.fit(X_train, y_train)
y_svm_pred = clf_svm.predict(X_test)
print("支持向量机预测测试集准确率为:",accuracy_score(y_test,y_svm_pred))
print("支持向量机预测测试集结果与实际结果的混淆矩阵为:\n",confusion_matrix(y_test,y_svm_pred))
print("支持向量机预测结果评估报告为:\n",classification_report(y_test,y_svm_pred))
print("交叉验证的结果为:",cross_val_score(clf_svm,X,y,cv=5))
#Test on Training data
train_result = clf_svm.predict(X_train)
precision = sum(train_result == y_train)/y_train.shape[0]
print('Training precision: ', precision)
#Test on test data
test_result = clf_svm.predict(X_train)
precision = sum(test_result == y_train)/y_train.shape[0]
print('Test precision: ', precision)
#data_new = pd.read_excel('样本外-随机森林.xlsx')
#data_new = pd.read_excel('样本外.xlsx')
data_new = pd.read_excel('样本外--隐含评级.xlsx')
x_new_train = data_new.iloc[:, 1:].values
y_new_train = data_new.iloc[:, 0].values
x_new_train = StandardScaler.transform(x_new_train)
y_new_pred = clf_svm.predict(x_new_train)
print(y_new_pred,y_new_train)
result_new_1 = accuracy_score(y_new_train, y_new_pred)
print("Accuracy:", result_new_1)
result_new_2 = confusion_matrix(y_new_train, y_new_pred)
print("Confusion Matrix:")
print(result_new_2)
result_new_3= classification_report(y_new_train, y_new_pred)
print("Classification Report:", )
print(result_new_3)