Last active
April 14, 2021 02:34
-
-
Save shedoesdatascience/7fa366e1234d1d551c9b11cd8435aa77 to your computer and use it in GitHub Desktop.
Logistic regression applied to iris dataset
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
import matplotlib.pyplot as plt | |
from sklearn import datasets | |
import numpy as np | |
import seaborn as sns | |
from sklearn.metrics import precision_recall_curve | |
from sklearn.metrics import roc_curve, auc | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.metrics import classification_report | |
from sklearn.metrics import accuracy_score | |
from sklearn.model_selection import train_test_split | |
import seaborn as sns | |
import pandas as pd | |
#Split the data into 80% training and 20% testing | |
iris = datasets.load_iris() | |
X = iris.data | |
y = iris.target | |
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=100) | |
#Feature Scaling : | |
from sklearn.preprocessing import StandardScaler | |
sc = StandardScaler() | |
x_train = sc.fit_transform(x_train) | |
x_test = sc.transform(x_test) | |
# Fitting Logistic Regression to the Training set | |
classifier = LogisticRegression(random_state = 0, solver='lbfgs', multi_class='auto') | |
classifier.fit(x_train, y_train) | |
# Predicting the Test set results | |
y_pred = classifier.predict(x_test) | |
# Predict probabilities | |
probs_y=classifier.predict_proba(x_test) | |
### Print results | |
probs_y = np.round(probs_y, 2) | |
res = "{:<10} | {:<10} | {:<10} | {:<13} | {:<5}".format("y_test", "y_pred", "Setosa(%)", "versicolor(%)", "virginica(%)\n") | |
res += "-"*65+"\n" | |
res += "\n".join("{:<10} | {:<10} | {:<10} | {:<13} | {:<10}".format(x, y, a, b, c) for x, y, a, b, c in zip(y_test, y_pred, probs_y[:,0], probs_y[:,1], probs_y[:,2])) | |
res += "\n"+"-"*65+"\n" | |
print(res) | |
#Check precision, recall, f1-score | |
print( classification_report(y_test, y_pred) ) | |
print( accuracy_score(y_test, y_pred)) | |
from sklearn.metrics import confusion_matrix | |
cm = confusion_matrix(y_test, y_pred) | |
print(cm) | |
# Plot confusion matrix | |
# confusion matrix sns heatmap | |
ax = plt.axes() | |
df_cm = cm | |
sns.heatmap(df_cm, annot=True, annot_kws={"size": 30}, fmt='d',cmap="Blues", ax = ax ) | |
ax.set_title('Confusion Matrix') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment