In [49]:
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import numpy as np
import pandas as pd
iris = sns.load_dataset("iris")
In [50]:
iris = sns.load_dataset("iris")
print(iris.head())
In [51]:
features = iris[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
print (features.head(5))
target = iris['species']
print (target.head(5))
In [52]:
# we are traning our model wth 75% data and leaving rest 25% for testing
from sklearn.model_selection import train_test_split
features_train, features_test,target_train, target_test = train_test_split(features,target, test_size=0.25, stratify=target)
In [53]:
# we are importng svm model and fitting the model with training data
from sklearn import svm
clf = svm.SVC()
clf.fit(features_train,target_train)
Out[53]:
In [54]:
# Making predictions on test data
expected = clf.predict(features_test)
print(expected)
In [55]:
# actual labels of test data
actual = target_test.as_matrix()
print(actual)
In [56]:
# comparing expected and actual data
df_results = pd.DataFrame({'actual':actual, 'expected':expected})
df_results['Test_Result'] = np.where(df_results['actual'] == df_results['expected'], 'Pass', 'Fail')
print(df_results)
In [57]:
from sklearn.metrics import accuracy_score
accuracy_score(actual, expected)
Out[57]:
This means our SVM classifer predicted 97.36% of test data accurately after being trained on training samples.
In [58]:
plt.figure(figsize=(10,6))
plt.legend(bbox_to_anchor=(1, 1), loc=2)
g= sns.swarmplot(x="actual", y=df_results.index, hue="Test_Result",data=df_results)
Our model failed to predict just one sample where actual value was versicolor but predicted value was virginica.
Thanks for sharing...Helpful indeed
ReplyDelete