from IPython.display import HTML
HTML('<iframe src=http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data width=300 height=200></iframe>')

"""
Explanation: Training a machine learning model with scikit-learn
Agenda

What is the K-nearest neighbors classification model?
What is the four steps for model training and prediction in scikit learn?
How can I apply this pattern to  other machine learning models?

Reviewing the iris dataset
End of explanation
"""


# import load_iris function from datasets module
from sklearn.datasets import load_iris

# save "bunch" object containing iris dataset and its attributes
iris = load_iris()

# store feature matrix in "X"
X = iris.data

# store response vector in "y"
y = iris.target

# print the shapes of X and y
print X.shape
print y.shape

"""
Explanation: 150 observations
4 features (sepal length, sepal width, petal length, petal width)
Response variable is the iris species
Classification problem since response is categorical
More information in the UCI Machine Learning Repository

K-nearest neighbors (KNN) classification

Pick a value for K.
Search for the K observations in the training data that are "nearest" to the measurements of the unknown iris.
Use the most popular response value from the K nearest neighbors as the predicted response value for the unknown iris.

Loading the data
End of explanation
"""


from sklearn.neighbors import KNeighborsClassifier

"""
Explanation: scikit-learn 4-step modeling pattern
 Step 1:Import the class you plan to use
End of explanation
"""


knn = KNeighborsClassifier(n_neighbors=1)

"""
Explanation: Step 2: "Instantiate" the "estimator"
* "Estimator" is scikit-learn's term for model
* "Instantiate" means "make an instance of"
End of explanation
"""


print knn

"""
Explanation: Name of the object does not matter
Can specify tuning parameters (aka "hyperparameters") during this step
All parameters not specified are set to their defaults
End of explanation
"""


knn.fit(X,y)

"""
Explanation: Step 3: Fit the model with data (aka "model training")
* Model is learning the relationship between X and y
* Occurs in-place
End of explanation
"""


knn.predict([3,5,4,2])

"""
Explanation: Step 4: Predict the response for a new observation
* New observations are called "out-of-sample" data
* Uses the information it learned during the model training process
End of explanation
"""


X_new = [[3, 5, 4, 2], [5, 4, 3, 2]]
knn.predict(X_new)

"""
Explanation: Returns a NumPy array
Can predict for multiple observations at once
End of explanation
"""


# instantiate the model (using the value K=5)
knn = KNeighborsClassifier(n_neighbors=5)

# fit the model with data
knn.fit(X, y)

# predict the response for new observations
knn.predict(X_new)

"""
Explanation: Using a different value for K
End of explanation
"""


# import the class
from sklearn.linear_model import LogisticRegression

# instantiate the model (using the default parameters)
logreg = LogisticRegression()

# fit the model with data
logreg.fit(X, y)

# predict the response for new observations
logreg.predict(X_new)

"""
Explanation: Using a different classification model
End of explanation
"""


from IPython.core.display import HTML
def css_styling():
    styles = open("styles/custom.css", "r").read()
    return HTML(styles)
css_styling()

"""
Explanation: Resources

Nearest Neighbors (user guide), KNeighborsClassifier (class documentation)
Logistic Regression (user guide [LogisticRegression](http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html (class documentation)
Videos from An Introduction to Statistical Learning
Classification Problems and K-Nearest Neighbors (Chapter 2)
Introduction to Classification (Chapter 4)
Logistic Regression and Maximum Likelihood (Chapter 4)
End of explanation
"""


