In [1]:
import iris
In [2]:
X=iris.dataset.data
y=iris.dataset.target
iris.dataset.feature_names
iris.dataset.target_names
Out[2]:
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
In [3]:
import pandas as pd
import seaborn as sns
In [4]:
df = pd.DataFrame(iris.dataset.data,
columns=iris.dataset.feature_names)
df['species'] = iris.dataset.target
df['species'] = df['species'].map({i: species for i,
species in enumerate(iris.dataset.target_names)})
df
Out[4]:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
In [5]:
sns.pairplot(df, hue='species').add_legend()
Out[5]:
<seaborn.axisgrid.PairGrid at 0x173603cb0>
In [6]:
# from sklearn.model_selection import train_test_split
X_train, X_test = iris.X_train, iris.X_test
y_train, y_test = iris.y_train, iris.y_test
In [7]:
import xgboost as xgb
model = xgb.XGBClassifier()
model.fit(X_train,y_train)
print()
In [8]:
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay
import matplotlib.pyplot as plt
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print( f"{accuracy*100:.2f}%")
cm = confusion_matrix(y_test, y_pred)
print(cm)
cm_display = ConfusionMatrixDisplay(confusion_matrix = cm,
display_labels = iris.dataset.target_names)
cm_display.plot()
plt.show()
98.33% [[20 0 0] [ 0 19 1] [ 0 0 20]]