#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np import matplotlib.pyplot as plt import sklearn.datasets as dt import sklearn.model_selection as ms import sklearn.svm as sv # In[8]: IRIS = dt.load_iris() x = IRIS.data y = IRIS.target xtr, xte, ytr, yte, = ms.train_test_split(x, y, train_size = 0.75, random_state = 55) # In[9]: #Different Kernels: trACC = [] teACC = [] kernel = [] for i in ["linear", "poly", "rbf", "sigmoid"]: clsfr = sv.SVC(kernel = i, degree = 2) clsfr.fit(xtr, ytr) trACC.append(clsfr.score(xtr, ytr)) teACC.append(clsfr.score(xte, yte)) kernel.append(i) # In[10]: plt.plot(trACC, label = "Train", c = "#00FFFF") plt.plot(teACC, label = "Test", c = "#FF4040") plt.xticks([0, 1, 2, 3], kernel) plt.xlabel("Kernel") plt.ylabel("Accuracy") plt.legend() plt.show() # In[11]: #Different degrees: trACC = [] teACC = [] degree = [] for i in range (1, 11): clsfr = sv.SVC(kernel = "poly", degree = i) clsfr.fit(xtr, ytr) trACC.append(clsfr.score(xtr, ytr)) teACC.append(clsfr.score(xte, yte)) degree.append(i) # In[12]: plt.plot(trACC, label = "Train", c = "#00FFFF") plt.plot(teACC, label = "Test", c = "#FF4040") plt.xlabel("Degree") plt.ylabel("Accuracy") plt.legend() plt.show() # In[ ]: