<jupyter_start><jupyter_text>My First Machine Learning ModelThis template will help you create your fist machine learning model in 5 minutes.   0. SetupWe provide the initial setup of the notebook. In this section we import the necessary libraries so you can build your model.<jupyter_code>import numpy as np
import pandas as pd

from matplotlib import pyplot as plt

import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix

from joblib import dump, load<jupyter_output><empty_output><jupyter_text>1. Load the dataThe first step is to load the necessary data. Use the command read_csv from pandas library to load the Iris dataset. After loading the data into a dataframe, show the top of the dataset. The dataset file URL is https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data.<jupyter_code># load the data
cols = ['sepal_length', ' sepal_width', 'petal_length', 'petal_width', 'class']
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', names=cols)
df.head()<jupyter_output><empty_output><jupyter_text>2. Explore and visualize the data After loading the dataset into a dataframe in memory, the next step is to perform an exploratory data analysis. The objective of the EDA is to discover as much information as possible about the dataset. The describe() method is a good starting point. The describe() method prints statistics of the dataset, like mean, standard deviation, etc.<jupyter_code>df.describe()<jupyter_output><empty_output><jupyter_text>A very important tool in exploratory data analysis is data visualization, which helps us to gain insights about the dataset. The plot below shows the relationship between the attributes of the dataset.<jupyter_code>sns.pairplot(df, hue='class');<jupyter_output><empty_output><jupyter_text>Another interesting case of data visualization is use a heatmap to visualize the correlation matrix of the dataset.<jupyter_code>sns.heatmap(df.corr(), annot=True)<jupyter_output><empty_output><jupyter_text>3. Preprocess the data Frequently, the dataset collected from databases, files or scraping the internet is not readyto be consumed by a machine learning algorithm. In most cases, the dataset needs some kind of preparation or preprocessing before being used as input to a machine learning algorithm. In this case, we convert the string values of the class column to integer numbers because the algorithm we are going to use does not process string values.<jupyter_code>df['class'].unique()
df['class_encod'] = df['class'].apply(lambda x: 0 if x == 'Iris-setosa' else 1 if x == 'Iris-versicolor' else 2)
df['class_encod'].unique()<jupyter_output><empty_output><jupyter_text>4. Select an algorithm and train the model After exploring and preprocessing our data we can build our machine learning model to classify Iris specimens. So, the first step is to split our dataframe in input attributes and target attributes.<jupyter_code>y = df[['class_encod']] # target attributes 
X = df.iloc[:, 0:4] # input attributes
X.head()
y.head()<jupyter_output><empty_output><jupyter_text>If in the previous step we splited the dataframe by separating columns, in this step we split the data by rows. The method train_test_split() will split the X and y dataframes in training data and test data.<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
                                                    random_state=0, stratify=y)

np.shape(y_train)<jupyter_output><empty_output><jupyter_text>Then we use the datasets X_train and y_train to build a KNN classifier, using the KNeighborsClassifier class provided by scikit-learn. Because the machine learning algorithm is already implemented by the library, all we have to do is call the method fit() passing the X_train and y_train datasets as arguments.<jupyter_code>m = KNeighborsClassifier()
m.fit(X_train, np.ravel(y_train))<jupyter_output><empty_output><jupyter_text>Once the model is built, we can use the predict() method to calculate the predicted category of a instance. In this case, we want to predict the class of the first 10 lines of the X_test dataset. The return is an array containing the estimated categories.<jupyter_code>m.predict(X_test.iloc[0:10])
y_test[0:10]<jupyter_output><empty_output><jupyter_text>We can use methods like score() and confusion_matrix() to measure the performance of our model. We see that the accuracy of our model is 1.0 (100%), which means that the model predicted correctly all cases of the test dataset.<jupyter_code>m.score(X_test, y_test)
confusion_matrix(y_test, m.predict(X_test))<jupyter_output><empty_output><jupyter_text>5. Save the model for later use Finally, we want to save our model for later use. For example, we could embed our model into a webservice or mobile application. So we use the method dump() from the joblib package to save the model to a file.<jupyter_code>dump(m, 'iris-classifier.dmp')
ic = load('iris-classifier.dmp')
confusion_matrix(y_test, ic.predict(X_test))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07346384958325257, 'fraction_numerical': 0.005427408412483039, 'mean_word_length': 5.62387676508344, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '269170', 'license': 'Unlicense', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1461, 'n_words': 613, 'path': 'iris_classifier.ipynb', 'repo_name': 'rodmsmendes/reinforcermentlearning4fun', 'size': '5159'}