<jupyter_start><jupyter_text>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<jupyter_code>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>')<jupyter_output><empty_output><jupyter_text>* 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](http://archive.ics.uci.edu/ml/) K-nearest neighbors (KNN) classification1. Pick a value for K.1. Search for the K observations in the training data that are "nearest" to the measurements of the unknown iris.1. Use the most popular response value from the K nearest neighbors as the predicted response value for the unknown iris. Loading the data<jupyter_code># 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<jupyter_output>(150, 4)
(150,)<jupyter_text>scikit-learn 4-step modeling pattern*** Step 1:***Import the class you plan to use<jupyter_code>from sklearn.neighbors import KNeighborsClassifier<jupyter_output><empty_output><jupyter_text>***Step 2:*** "Instantiate" the "estimator"* "Estimator" is scikit-learn's term for model* "Instantiate" means "make an instance of"<jupyter_code>knn = KNeighborsClassifier(n_neighbors=1)<jupyter_output><empty_output><jupyter_text>* 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<jupyter_code>print knn<jupyter_output>KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
           metric_params=None, n_jobs=1, n_neighbors=1, p=2,
           weights='uniform')<jupyter_text>***Step 3:*** Fit the model with data (aka "model training")* Model is learning the relationship between X and y* Occurs in-place<jupyter_code>knn.fit(X,y)<jupyter_output><empty_output><jupyter_text>***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<jupyter_code>knn.predict([3,5,4,2])<jupyter_output>//anaconda/lib/python2.7/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)<jupyter_text>* Returns a NumPy array* Can predict for multiple observations at once<jupyter_code>X_new = [[3, 5, 4, 2], [5, 4, 3, 2]]
knn.predict(X_new)<jupyter_output><empty_output><jupyter_text>Using a different value for K<jupyter_code># 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)<jupyter_output><empty_output><jupyter_text>Using a different classification model<jupyter_code># 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)<jupyter_output><empty_output><jupyter_text>Resources* [Nearest Neighbors](http://scikit-learn.org/stable/modules/neighbors.html) (user guide), [KNeighborsClassifier](http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html) (class documentation)* [Logistic Regression](http://scikit-learn.org/stable/modules/linear_model.htmllogistic-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](http://www.dataschool.io/15-hours-of-expert-machine-learning-videos/)  * Classification Problems and K-Nearest Neighbors (Chapter 2)  * Introduction to Classification (Chapter 4)  * Logistic Regression and Maximum Likelihood (Chapter 4)<jupyter_code>from IPython.core.display import HTML
def css_styling():
    styles = open("styles/custom.css", "r").read()
    return HTML(styles)
css_styling()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10275380189066996, 'fraction_numerical': 0.013563501849568433, 'mean_word_length': 6.476190476190476, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '440210', 'license': 'MIT', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1507, 'n_words': 461, 'path': 'practice/03.TrainingModelWithSciKit.ipynb', 'repo_name': 'awadalaa/DataSciencePractice', 'size': '4866'}