# The Iris Setosa
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Image(url,width=300, height=300)
# The Iris Versicolor
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/4/41/Iris_versicolor_3.jpg'
Image(url,width=300, height=300)
# The Iris Virginica
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/9/9f/Iris_virginica.jpg'
Image(url,width=300, height=300)
The iris dataset contains measurements for 150 iris flowers from three different species.
The three classes in the Iris dataset:
Iris-setosa (n=50)
Iris-versicolor (n=50)
Iris-virginica (n=50)
The four features of the Iris dataset:
sepal length in cm
sepal width in cm
petal length in cm
petal width in cm
import seaborn as sns
iris = sns.load_dataset('iris')
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# Setosa is the most separable.
sns.pairplot(iris,hue='species',palette='Dark2')
C:\Users\ACER\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead. with pd.option_context('mode.use_inf_as_na', True): C:\Users\ACER\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead. with pd.option_context('mode.use_inf_as_na', True): C:\Users\ACER\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead. with pd.option_context('mode.use_inf_as_na', True): C:\Users\ACER\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead. with pd.option_context('mode.use_inf_as_na', True): C:\Users\ACER\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning: The figure layout has changed to tight self._figure.tight_layout(*args, **kwargs)
<seaborn.axisgrid.PairGrid at 0x14af6028e10>
import seaborn as sns
import matplotlib.pyplot as plt
# Assuming you have loaded the Iris dataset into the 'iris' DataFrame
# Select the setosa species from the Iris dataset
setosa = iris[iris['species'] == 'setosa']
# Create a bivariate kernel density plot
sns.kdeplot(x=setosa['sepal_width'], y=setosa['sepal_length'], cmap="plasma", shade=True, thresh=0, shade_lowest=False)
# Add labels and title
plt.xlabel('Sepal Width')
plt.ylabel('Sepal Length')
plt.title('Kernel Density Plot for Setosa Species')
# Show the plot
plt.show()
C:\Users\ACER\AppData\Local\Temp\ipykernel_11384\1974457279.py:10: UserWarning: `shade_lowest` has been replaced by `thresh`; setting `thresh=0. This will become an error in seaborn v0.13.0; please update your code. sns.kdeplot(x=setosa['sepal_width'], y=setosa['sepal_length'], cmap="plasma", shade=True, thresh=0, shade_lowest=False) C:\Users\ACER\AppData\Local\Temp\ipykernel_11384\1974457279.py:10: FutureWarning: `shade` is now deprecated in favor of `fill`; setting `fill=True`. This will become an error in seaborn v0.14.0; please update your code. sns.kdeplot(x=setosa['sepal_width'], y=setosa['sepal_length'], cmap="plasma", shade=True, thresh=0, shade_lowest=False) C:\Users\ACER\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead. with pd.option_context('mode.use_inf_as_na', True): C:\Users\ACER\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead. with pd.option_context('mode.use_inf_as_na', True):
from sklearn.model_selection import train_test_split
X = iris.drop('species',axis=1)
y = iris['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
from sklearn.svm import SVC
svc_model = SVC()
svc_model.fit(X_train,y_train)
SVC()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
SVC()
predictions = svc_model.predict(X_test)
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,predictions))
[[13 0 0] [ 0 17 1] [ 0 0 14]]
print(classification_report(y_test,predictions))
precision recall f1-score support setosa 1.00 1.00 1.00 13 versicolor 1.00 0.94 0.97 18 virginica 0.93 1.00 0.97 14 accuracy 0.98 45 macro avg 0.98 0.98 0.98 45 weighted avg 0.98 0.98 0.98 45
from sklearn.model_selection import GridSearchCV
param_grid = {'C': [0.1,1, 10, 100], 'gamma': [1,0.1,0.01,0.001]}
grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=2)
grid.fit(X_train,y_train)
Fitting 5 folds for each of 16 candidates, totalling 80 fits [CV] END .....................................C=0.1, gamma=1; total time= 0.0s [CV] END .....................................C=0.1, gamma=1; total time= 0.0s [CV] END .....................................C=0.1, gamma=1; total time= 0.0s [CV] END .....................................C=0.1, gamma=1; total time= 0.0s [CV] END .....................................C=0.1, gamma=1; total time= 0.0s [CV] END ...................................C=0.1, gamma=0.1; total time= 0.0s [CV] END ...................................C=0.1, gamma=0.1; total time= 0.0s [CV] END ...................................C=0.1, gamma=0.1; total time= 0.0s [CV] END ...................................C=0.1, gamma=0.1; total time= 0.0s [CV] END ...................................C=0.1, gamma=0.1; total time= 0.0s [CV] END ..................................C=0.1, gamma=0.01; total time= 0.0s [CV] END ..................................C=0.1, gamma=0.01; total time= 0.0s [CV] END ..................................C=0.1, gamma=0.01; total time= 0.0s [CV] END ..................................C=0.1, gamma=0.01; total time= 0.0s [CV] END ..................................C=0.1, gamma=0.01; total time= 0.0s [CV] END .................................C=0.1, gamma=0.001; total time= 0.0s [CV] END .................................C=0.1, gamma=0.001; total time= 0.0s [CV] END .................................C=0.1, gamma=0.001; total time= 0.0s [CV] END .................................C=0.1, gamma=0.001; total time= 0.0s [CV] END .................................C=0.1, gamma=0.001; total time= 0.0s [CV] END .......................................C=1, gamma=1; total time= 0.0s [CV] END .......................................C=1, gamma=1; total time= 0.0s [CV] END .......................................C=1, gamma=1; total time= 0.0s [CV] END .......................................C=1, gamma=1; total time= 0.0s [CV] END .......................................C=1, gamma=1; total time= 0.0s [CV] END .....................................C=1, gamma=0.1; total time= 0.0s [CV] END .....................................C=1, gamma=0.1; total time= 0.0s [CV] END .....................................C=1, gamma=0.1; total time= 0.0s [CV] END .....................................C=1, gamma=0.1; total time= 0.0s [CV] END .....................................C=1, gamma=0.1; total time= 0.0s [CV] END ....................................C=1, gamma=0.01; total time= 0.0s [CV] END ....................................C=1, gamma=0.01; total time= 0.0s [CV] END ....................................C=1, gamma=0.01; total time= 0.0s [CV] END ....................................C=1, gamma=0.01; total time= 0.0s [CV] END ....................................C=1, gamma=0.01; total time= 0.0s [CV] END ...................................C=1, gamma=0.001; total time= 0.0s [CV] END ...................................C=1, gamma=0.001; total time= 0.0s [CV] END ...................................C=1, gamma=0.001; total time= 0.0s [CV] END ...................................C=1, gamma=0.001; total time= 0.0s [CV] END ...................................C=1, gamma=0.001; total time= 0.0s [CV] END ......................................C=10, gamma=1; total time= 0.0s [CV] END ......................................C=10, gamma=1; total time= 0.0s [CV] END ......................................C=10, gamma=1; total time= 0.0s [CV] END ......................................C=10, gamma=1; total time= 0.0s [CV] END ......................................C=10, gamma=1; total time= 0.0s [CV] END ....................................C=10, gamma=0.1; total time= 0.0s [CV] END ....................................C=10, gamma=0.1; total time= 0.0s [CV] END ....................................C=10, gamma=0.1; total time= 0.0s [CV] END ....................................C=10, gamma=0.1; total time= 0.0s [CV] END ....................................C=10, gamma=0.1; total time= 0.0s [CV] END ...................................C=10, gamma=0.01; total time= 0.0s [CV] END ...................................C=10, gamma=0.01; total time= 0.0s [CV] END ...................................C=10, gamma=0.01; total time= 0.0s [CV] END ...................................C=10, gamma=0.01; total time= 0.0s [CV] END ...................................C=10, gamma=0.01; total time= 0.0s [CV] END ..................................C=10, gamma=0.001; total time= 0.0s [CV] END ..................................C=10, gamma=0.001; total time= 0.0s [CV] END ..................................C=10, gamma=0.001; total time= 0.0s [CV] END ..................................C=10, gamma=0.001; total time= 0.0s [CV] END ..................................C=10, gamma=0.001; total time= 0.0s [CV] END .....................................C=100, gamma=1; total time= 0.0s [CV] END .....................................C=100, gamma=1; total time= 0.0s [CV] END .....................................C=100, gamma=1; total time= 0.0s [CV] END .....................................C=100, gamma=1; total time= 0.0s [CV] END .....................................C=100, gamma=1; total time= 0.0s [CV] END ...................................C=100, gamma=0.1; total time= 0.0s [CV] END ...................................C=100, gamma=0.1; total time= 0.0s [CV] END ...................................C=100, gamma=0.1; total time= 0.0s [CV] END ...................................C=100, gamma=0.1; total time= 0.0s [CV] END ...................................C=100, gamma=0.1; total time= 0.0s [CV] END ..................................C=100, gamma=0.01; total time= 0.0s [CV] END ..................................C=100, gamma=0.01; total time= 0.0s [CV] END ..................................C=100, gamma=0.01; total time= 0.0s [CV] END ..................................C=100, gamma=0.01; total time= 0.0s [CV] END ..................................C=100, gamma=0.01; total time= 0.0s [CV] END .................................C=100, gamma=0.001; total time= 0.0s [CV] END .................................C=100, gamma=0.001; total time= 0.0s [CV] END .................................C=100, gamma=0.001; total time= 0.0s [CV] END .................................C=100, gamma=0.001; total time= 0.0s [CV] END .................................C=100, gamma=0.001; total time= 0.0s
GridSearchCV(estimator=SVC(), param_grid={'C': [0.1, 1, 10, 100], 'gamma': [1, 0.1, 0.01, 0.001]}, verbose=2)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
GridSearchCV(estimator=SVC(), param_grid={'C': [0.1, 1, 10, 100], 'gamma': [1, 0.1, 0.01, 0.001]}, verbose=2)
SVC()
SVC()
grid_predictions = grid.predict(X_test)
print(confusion_matrix(y_test,grid_predictions))
[[13 0 0] [ 0 17 1] [ 0 0 14]]
print(classification_report(y_test,grid_predictions))
precision recall f1-score support setosa 1.00 1.00 1.00 13 versicolor 1.00 0.94 0.97 18 virginica 0.93 1.00 0.97 14 accuracy 0.98 45 macro avg 0.98 0.98 0.98 45 weighted avg 0.98 0.98 0.98 45