Skip to content

Instantly share code, notes, and snippets.

@rebeccabilbro
Last active June 8, 2020 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebeccabilbro/797521107bbe5070f511aa6c5a290e0c to your computer and use it in GitHub Desktop.
Save rebeccabilbro/797521107bbe5070f511aa6c5a290e0c to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
from yellowbrick.datasets import load_occupancy
from yellowbrick.model_selection import CVScores
from yellowbrick.classifier import ConfusionMatrix
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import train_test_split as tts
# Load a sample dataset and make train and test splits
X, y = load_occupancy()
X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2, shuffle=True)
# Create 2 side-by-side subplots
fig, axes = plt.subplots(ncols=2, figsize=(8, 4))
# Instantiate the ConfusionMatrix visualizer for an SVM model
svm_confusion_matrix = ConfusionMatrix(
SVC(),
classes=["Occupied", "Vacant"],
cmap="GnBu",
ax=axes[0]
)
svm_confusion_matrix.fit(X_train, y_train)
svm_confusion_matrix.score(X_test, y_test)
# Create a cross-validation strategy
strategy = StratifiedKFold(n_splits=12, shuffle=True)
# Instantiate the CVScores visualizer for an SVM model
svm_cv_scores = CVScores(
SVC(),
scoring="f1_weighted",
cv=strategy,
ax=axes[1]
)
svm_cv_scores.fit(X, y)
fig.suptitle("Performance of an SVM model on the occupancy dataset")
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment