In [8]:
#importing scikit-learn datasets and svm packages
from sklearn import datasets
from sklearn import cross_validation
from sklearn import svm

#reading the iris dataset from the datasets package
iris = datasets.load_iris()
iris.data.shape,iris.target.shape

#cross validation split for train and test sets
X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(iris.data,iris.target,test_size=0.4,random_state=0)

In [6]:
X_train.shape,X_test.shape,Y_train.shape,Y_test.shape


Out[6]:
((90, 4), (60, 4), (90,), (60,))

In [9]:
#calling the 
clf = svm.SVC(kernel='linear', C=1).fit(X_train, Y_train)
clf.score(X_test, Y_test)


Out[9]:
0.96666666666666667

In [11]:
#distance of the samples X_train to the separating hyperplane
clf.decision_function(X_train)


C:\Users\priyu\Anaconda3\lib\site-packages\sklearn\svm\base.py:552: ChangedBehaviorWarning: The decision_function_shape default value will change from 'ovo' to 'ovr' in 0.19. This will change the shape of the decision function returned by SVC.
  "SVC.", ChangedBehaviorWarning)
Out[11]:
array([[-2.30901106, -0.78524627,  0.92386904],
       [ 1.14875641,  1.14918044,  8.01659071],
       [-3.40562114, -1.15901686, -1.00018455],
       [-2.21515718, -0.54426261,  1.5704362 ],
       [-1.5091051 , -0.22950843,  2.84684701],
       [-2.1141523 , -0.4377052 ,  1.88084539],
       [-2.5295763 , -0.82786923,  1.0004791 ],
       [-4.06614901, -1.7770498 , -1.66238231],
       [ 1.53861323,  1.15573782,  8.67407952],
       [ 1.66770904,  1.49344282,  8.82766377],
       [-3.63399212, -1.34918084, -0.9963918 ],
       [-2.01702707, -0.52295113,  1.83223904],
       [ 1.17703385,  1.02459025,  8.2785279 ],
       [ 1.79208317,  1.08852469,  8.78975487],
       [-2.86180328, -1.11475455,  0.81247933],
       [ 1.25831464,  1.22950833,  8.08434612],
       [-3.20749102, -1.13770538, -0.73838171],
       [-0.99978079,  0.17213102,  3.95495918],
       [ 1.46855039,  1.27377064,  8.20142496],
       [-2.89723172, -0.8524594 ,  0.99989636],
       [-4.28288181, -1.95737771, -2.7887865 ],
       [-2.11555464, -0.54918064,  1.67414496],
       [ 1.3436616 ,  1.26721326,  8.55474576],
       [-4.20468585, -1.78524652, -1.98222888],
       [-3.98341944, -1.68688584, -1.95548872],
       [-4.12303125, -1.66721371, -2.3341462 ],
       [-3.9756137 , -1.53934482, -1.88267142],
       [ 1.49967894,  1.13934437,  8.34474547],
       [ 1.30510111,  1.16393454,  8.06767617],
       [-3.22385012, -1.25901688, -0.56854523],
       [-3.82922468, -1.60983664, -1.79563261],
       [ 1.00021745,  1.28524605,  8.15151879],
       [-3.54588768, -1.42131201, -1.7878173 ],
       [ 1.29262168,  1.23114767,  8.48333195],
       [-3.45133264, -1.12459062, -1.03789993],
       [-3.64179786, -1.49672186, -1.0692091 ],
       [ 1.52029149,  1.36557394,  8.37618899],
       [ 1.60068382,  1.24590178,  8.65515466],
       [-3.87694605, -1.64262354, -1.00019015],
       [ 1.49752899,  1.20491816,  8.56671236],
       [ 1.41606169,  1.35573787,  8.34213201],
       [ 1.34478301,  1.00655745,  8.08153914],
       [-2.2183821 , -0.44754127,  1.84678842],
       [-4.17939961, -1.77377111, -2.43925844],
       [-4.92994814, -2.21475483, -3.08338277],
       [ 1.54394166,  1.22622964,  8.70777795],
       [ 1.29084553,  1.2065575 ,  8.43436706],
       [ 1.4871995 ,  1.2065575 ,  8.76040125],
       [-2.46858068, -0.70655773,  1.03593956],
       [-2.35210517, -0.80000038,  1.76285595],
       [ 1.59283165,  1.32786901,  9.00115865],
       [ 0.99956271,  1.00000007,  7.62934728],
       [-2.24824917, -0.70327904,  1.95464847],
       [ 1.28481593,  1.08360665,  8.41051465],
       [-5.53677148, -2.44426308, -3.98515304],
       [-1.29713964,  0.06557361,  3.3185158 ],
       [-4.27989061, -2.09672201, -2.18301599],
       [-2.30461752, -0.80983644,  1.84953622],
       [ 1.79811277,  1.21147554,  8.81360728],
       [-2.91120714, -1.00000043, -0.04755301],
       [ 1.97273278,  1.24918047,  9.69460081],
       [-5.09269625, -2.2688532 , -3.11378134],
       [ 1.55356998,  1.16885258,  8.41073885],
       [ 1.20549857,  1.16885258,  7.96396742],
       [-4.43057952, -1.89836131, -1.93931726],
       [ 1.44433912,  1.23114767,  8.6040692 ],
       [-3.79753504, -1.55901696, -2.27334906],
       [-2.06736583, -0.50327899,  1.86417545],
       [-1.87708789, -0.40000028,  2.47198228],
       [-1.43053534, -0.14426251,  3.49566909],
       [-3.55906829, -1.40983659, -1.47551174],
       [-3.57687616, -1.41311528, -1.09355439],
       [-3.14672913, -1.05573814,  0.29239771],
       [-3.09779271, -1.18360703,  0.16695712],
       [ 1.08738699,  1.1147542 ,  8.13886579],
       [-1.72611805, -0.22622974,  2.90819061],
       [-3.97238878, -1.63278747, -2.04582739],
       [-3.40562114, -1.15901686, -1.00018455],
       [ 1.39152305,  1.17049192,  8.48369049],
       [-2.55841482, -0.8852463 ,  1.47277512],
       [-1.95850877, -0.47868882,  1.82858064],
       [-2.80235008, -0.97541026, -0.08314782],
       [-2.65661503, -0.76885283,  1.57576679],
       [ 1.62573634,  1.21803292,  9.19344406],
       [ 1.45504241,  1.14262306,  8.25264476],
       [ 1.29084553,  1.2065575 ,  8.43436706],
       [-3.77963353, -1.45573824, -1.51209793],
       [-1.98486998, -0.45901668,  2.33999553],
       [-4.66437257, -2.26229582, -3.22562766],
       [ 1.41073326,  1.28524605,  8.30843358]])

In [12]:
#fit the SVM model according to the given training data
clf.fit(X_train,Y_train)


Out[12]:
SVC(C=1, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto', kernel='linear',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

In [15]:
#get parameters for the estimator
clf.get_params()


Out[15]:
{'C': 1,
 'cache_size': 200,
 'class_weight': None,
 'coef0': 0.0,
 'decision_function_shape': None,
 'degree': 3,
 'gamma': 'auto',
 'kernel': 'linear',
 'max_iter': -1,
 'probability': False,
 'random_state': None,
 'shrinking': True,
 'tol': 0.001,
 'verbose': False}

In [16]:
#perform classification on samples in X_train
clf.predict(X_train)


Out[16]:
array([1, 0, 2, 1, 1, 1, 1, 2, 0, 0, 2, 1, 0, 0, 1, 0, 2, 1, 0, 1, 2, 1, 0,
       2, 2, 2, 2, 0, 0, 2, 2, 0, 2, 0, 2, 2, 0, 0, 2, 0, 0, 0, 1, 2, 2, 0,
       0, 0, 1, 1, 0, 0, 1, 0, 2, 1, 2, 1, 0, 2, 0, 2, 0, 0, 2, 0, 2, 1, 1,
       1, 2, 2, 1, 1, 0, 1, 2, 2, 0, 1, 1, 2, 1, 0, 0, 0, 2, 1, 2, 0])