Created
April 13, 2021 05:38
-
-
Save shedoesdatascience/2cc0604d456937d2ffb32462b575e48f 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 roc_curve, auc | |
# 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)] | |
from sklearn.model_selection import train_test_split | |
from sklearn.svm import SVC | |
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) | |
svc = SVC(kernel='rbf', C=1).fit(X_train, y_train) | |
y_pred = svc.predict(X_test) | |
#importing confusion matrix | |
from sklearn.metrics import confusion_matrix | |
confusion = confusion_matrix(y_test, y_pred) | |
print('Confusion Matrix\n') | |
print(confusion) | |
#importing accuracy_score, precision_score, recall_score, f1_score | |
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score | |
print('\nAccuracy: {:.2f}\n'.format(accuracy_score(y_test, y_pred))) | |
print('Micro Precision: {:.2f}'.format(precision_score(y_test, y_pred, average='micro'))) | |
print('Micro Recall: {:.2f}'.format(recall_score(y_test, y_pred, average='micro'))) | |
print('Micro F1-score: {:.2f}\n'.format(f1_score(y_test, y_pred, average='micro'))) | |
print('Macro Precision: {:.2f}'.format(precision_score(y_test, y_pred, average='macro'))) | |
print('Macro Recall: {:.2f}'.format(recall_score(y_test, y_pred, average='macro'))) | |
print('Macro F1-score: {:.2f}\n'.format(f1_score(y_test, y_pred, average='macro'))) | |
print('Weighted Precision: {:.2f}'.format(precision_score(y_test, y_pred, average='weighted'))) | |
print('Weighted Recall: {:.2f}'.format(recall_score(y_test, y_pred, average='weighted'))) | |
print('Weighted F1-score: {:.2f}'.format(f1_score(y_test, y_pred, average='weighted'))) | |
from sklearn.metrics import classification_report | |
print('\nClassification Report\n') | |
print(classification_report(y_test, y_pred, target_names=['Class 1', 'Class 2', 'Class 3'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment