Created
April 8, 2021 05:32
-
-
Save shedoesdatascience/8b6e08f7e26a985fd0212e550c6513b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn import svm, datasets | |
from sklearn.model_selection import train_test_split | |
import numpy as np | |
from sklearn.metrics import precision_recall_curve | |
# from sklearn.metrics import plot_precision_recall_curve | |
import matplotlib.pyplot as plt | |
iris = datasets.load_iris() | |
X = iris.data | |
y = iris.target | |
# Add noisy features | |
random_state = np.random.RandomState(0) | |
n_samples, n_features = X.shape | |
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)] | |
# Limit to the two first classes, and split into training and test | |
X_train, X_test, y_train, y_test = train_test_split(X[y < 2], y[y < 2], | |
test_size=.5, | |
random_state=random_state) | |
# Create a simple classifier | |
classifier = svm.LinearSVC(random_state=random_state) | |
classifier.fit(X_train, y_train) | |
y_score = classifier.decision_function(X_test) | |
from sklearn.metrics import average_precision_score | |
average_precision = average_precision_score(y_test, y_score) | |
print('Average precision-recall score: {0:0.2f}'.format( | |
average_precision)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment