# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# # Iris Dataset - Logistic Regression
#
# In this Jupyter notebook we'll look at the classic Iris dataset and use it to train a logistic regression classifier using Scikit-learn.

# import libaries
import sklearn
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
# %matplotlib inline

# load and describe Iris data
iris = datasets.load_iris()
print(iris.DESCR)
print('Iris dataset keys: {}'.format(list(iris.keys())))
#print('Target/class names: {}'.format(iris.target_names))
#print('Feature names: {}'.format(iris.feature_names))

iris['data'][:,2:3] 

# assign features and labels
X = iris["data"][:,3:] # petal length
Y = (iris["target"] == 1).astype(np.int) # Iris Versicolour = 1
print('X shape: {}'.format(X.shape))
print('Y shape: {}'.format(Y.shape))
print(X,Y)

# Logistic regression using scikit-learn
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit(X,Y)

y_proba = log_reg.predict_proba(X_new)
y_proba

# +
# Predict model's estimated probabilities for flowers with petal lengths 1-7 cm
X_new = np.linspace(1,7,1000).reshape(-1,1) # 1000x1 matrix of length 1-7
y_proba = log_reg.predict_proba(X_new)
print(y_proba[:,1])
decision_boundary = X_new[y_proba[:, 1] >= 0.5][0]

decision_boundary = X_new[y_proba[:, 1] >= 0.5]
plt.plot(X_new, y_proba[:, 1], "g-", linewidth=2, label="Iris-Versicolour")
plt.plot(X_new, y_proba[:, 0], "b--", linewidth=2, label="Not Iris-Versicolour")
plt.plot(X_new[0],decision_boundary)
#plt.plot([decision_boundary, decision_boundary], "k:", linewidth=2)
plt.legend()

# -

print(y_pred)
print(Y.shape)

# +
X = iris["data"][:,2:].reshape(-1,1)  # petal width
print(X.shape)
y = (iris["target"] == 2).astype(np.int)  # 1 if Iris-Virginica, else 0
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression(random_state=42)
log_reg.fit(X, y)
X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)

plt.plot(X_new, y_proba[:, 1], "g-", linewidth=2, label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", linewidth=2, label="Not Iris-Virginica")
X.shape
X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)
decision_boundary = X_new[y_proba[:, 1] >= 0.5][0]

plt.figure(figsize=(8, 3))
plt.plot(X[y==0], y[y==0], "bs")
plt.plot(X[y==1], y[y==1], "g^")
plt.plot([decision_boundary, decision_boundary], [-1, 2], "k:", linewidth=2)
plt.plot(X_new, y_proba[:, 1], "g-", linewidth=2, label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", linewidth=2, label="Not Iris-Virginica")
plt.text(decision_boundary+0.02, 0.15, "Decision  boundary", fontsize=14, color="k", ha="center")
plt.arrow(decision_boundary, 0.08, -0.3, 0, head_width=0.05, head_length=0.1, fc='b', ec='b')
plt.arrow(decision_boundary, 0.92, 0.3, 0, head_width=0.05, head_length=0.1, fc='g', ec='g')
plt.xlabel("Petal width (cm)", fontsize=14)
plt.ylabel("Probability", fontsize=14)
plt.legend(loc="center left", fontsize=14)
plt.axis([0, 3, -0.02, 1.02])
plt.show()
# -

# ### References
# Hands on ML"



StarcoderdataJupyterScriptsDedupFiltered