西瓜数据集3.0α
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoderplt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.close('all')def main():
data = pd.read_table('watermelon30a.txt',delimiter=',') x = pd.DataFrame({'密度':data['密度'],'含糖率':data['含糖率']})x = x.values.tolist() encoder = LabelEncoder()y = encoder.fit_transform(data['好瓜']).tolist()x,y = np.array(x),np.array(y)
linear_svm = svm.SVC(C=0.5, kernel='linear')linear_svm.fit(x,y)y_pred = linear_svm.predict(x)print('**linear_svm的准确率**: %s' %(accuracy_score(y_pred=y_pred, y_true=y)))
gauss_svm = svm.SVC(C=0.5,kernel='rbf')gauss_svm.fit(x,y)y_pred2 = gauss_svm.predict(x)print('**gauss_svm的准确率**: %s' %(accuracy_score(y_pred=y_pred2, y_true=y))) class_method = {'线性核':linear_svm,'高斯核':gauss_svm}visual(data,class_method)
def visual(data,class_method):colormap = dict(zip(data['好瓜'].value_counts().index.tolist(),['blue','green']))die = data.groupby('好瓜') plt.figure()for species,klass in die:plt.scatter(klass['密度'],klass['含糖率'],color = colormap[species],label = species)for name,model in class_method.items():sv = model.support_vectors_plt.plot(sv[:,0],sv[:,1],label=str(name)+'_supported_vector') plt.legend(frameon=True, title='好瓜',loc="upper left") plt.title('SVC')plt.show()if __name__=="__main__":main()
结果表明,使用线性核和高斯训练核的支持向量实际是一样的(两条线重合):