<jupyter_start><jupyter_text>Iris Dataset Notebook![image](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Machine+Learning+R/iris-machinelearning.png) ___ What is the Iris Dataset?The Iris flower dataset was introduced by <NAME> (a British statistician and biologist) in 1936. He used linear discriminant analysis to find a linear cobination of features that separates a multitude of classes, by taking multiple measurements which is an example of such analysis in taxonomic problems. --- [More information about the content of the dataset](https://medium.com/codebagng/basic-analysis-of-the-iris-data-set-using-python-2995618a6342)In this dataset alone, 50 of Iris flowers from each of the 3 species were collected and measured for accurate and non-bias results. Although because 2 of the 3 species were collected from the same location (Gaspé Peninsula) on the same pasture and day and because they were measured at the same time by the same person with the same apparatus, the end results might have become slightly biased which is why the visualisation, as you will see later on, shows 2 species being more alike.* 150 iris flowers of 3 species were identified and used for this dataset    1. setosa    2. virginica    3. versicolor* There are 4 columns included of the flowers' measurements    1. sepal length    2. sepal width    3. petals length    4. petals width* The 5th column in the dataset is the flower species - named as class in this case---  PackagesFirst and foremost we need to import the packages require for this code to work.<jupyter_code># Load data sets - data procesing
import pandas as pd

# Encoding categorical variables.
import sklearn.preprocessing as pre

# Splitting into training and test sets.
import sklearn.model_selection as mod

# Find a number of training samples
import sklearn.neighbors as nei

# Compute subset accuracy
from sklearn.metrics import accuracy_score

# See the relation between each pair of features
import seaborn as sns

# Linear algebra
import numpy as np

# Picks a random element
import random<jupyter_output><empty_output><jupyter_text>Show the dataset  Read the iris datasetTake the dataset from a csv (excel) file and read it<jupyter_code># Loading the dataset
dataset = pd.read_csv("https://raw.githubusercontent.com/ianmcloughlin/datasets/master/iris.csv")

# Displays the dataset in a user-friendly manner
dataset<jupyter_output><empty_output><jupyter_text>--- Dataset informationGather additional information from the dataset that might be useful in coding<jupyter_code># Source code adapted from: 
# - https://medium.com/codebagng/basic-analysis-of-the-iris-data-set-using-python-2995618a6342
# Shows the start of the dataset
# the first 5 rows
dataset.head()
# Information about the dataset
# - how many entries/objects were used in this dataset
# - the value of those entries (null or not)
# - the type of those entries (integer, float, object, etc)
dataset.info() 
# Statistics summarizing dispersion & shape of dataset’s distribution
dataset.describe()
# Outputs the amount of rows and columns of dataset
dataset.shape
# Object containing the amount of objects present in each class or species
dataset['class'].value_counts()<jupyter_output><empty_output><jupyter_text>Visualise  Display the measurements of the iris species<jupyter_code># Source code adapted from:
# - https://www.kaggle.com/adityabhat24/iris-data-analysis-and-machine-learning-python
# This graph plots on the graph the sepal length in each species
sns.violinplot(data=dataset, x="class", y="sepal_length")
# This graph plots on the graph the sepal width in each species
sns.violinplot(data=dataset, x="class", y="sepal_width")
# This graph plots on the graph the petal length in each species
sns.violinplot(data=dataset, x="class", y="petal_length")
# This graph plots on the graph the petal width in each species
sns.violinplot(data=dataset, x="class", y="petal_width")<jupyter_output><empty_output><jupyter_text>Plot the Iris dataset which includes the distinguished species of the iris<jupyter_code># Source code adapted from:
# - https://www.kaggle.com/jchen2186/machine-learning-with-iris-dataset
# Show the iris dataset using pairplot
sns.pairplot(dataset, hue='class', markers='o')<jupyter_output><empty_output><jupyter_text>Why is it so difficult to separate the three species of Iris?**Setosa** is the species of Iris flower that is very different from the other 2 and thus makes it very easy to separate it from them. On the other hand, **versicolor** and **virginica** are very similar to each other which is why it's so difficult to separate the two. As you can see in the diagram, they are very close to each other and it's very difficult to draw a line between the 2 of them that completely separates one from the other. Someway or another there will still be a few left from one species on the side of the other species. --- Train the dataset To train and test the Iris dataset we need follow the following steps:1. Inputs/Outputs    * Get the inputs/features        - **Features:**            1. sepal length in cm            2. sepal width in cm            3. petal length in cm            4. petal width in cm    * Get the desired output/targets        - **Targets:**            1. Iris Setosa            2. Iris Versicolour            3. Iris Virginica2. Split     * The dataset is separated into 'training' & 'testing'3. Train     * The model is trained using the 'fit' method on the 'training'data.4. Predict     * The outputs for the 'test data' is predicted5. Print & plot     * OutputsThis information was found [here](https://mclguide.readthedocs.io/en/latest/sklearn/multiclass.htmliris-dataset)  InputsThese represent the features or measurements taken from the Iris flowers that can be found in the Iris dataset.<jupyter_code># Check column names to find out what names the features were given
dataset.columns<jupyter_output><empty_output><jupyter_text>[In relation to the iris dataset features](https://www.ritchieng.com/machine-learning-iris-dataset/)* Each row is an observation, or record* Each column is a feature or an independent variable<jupyter_code># Source code adapted from: 
# - https://stackoverflow.com/questions/41130856/keyerror-petal-length-not-in-index
# dataset['sepal_length (cm)', 'sepal_width (cm)', 'petal_length (cm)', 'petal_width (cm)']
inputs = np.array(dataset[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']])
inputs<jupyter_output><empty_output><jupyter_text>Outputs**Get the targets of the Iris dataset which are the species of the Iris that were included in this dataset**<jupyter_code>outputs = np.array(dataset['class'], dtype='<U10')
outputs<jupyter_output><empty_output><jupyter_text>Classify<jupyter_code>knn = nei.KNeighborsClassifier(n_neighbors=5)<jupyter_output><empty_output><jupyter_text>Fit<jupyter_code>knn.fit(inputs, outputs)<jupyter_output><empty_output><jupyter_text>Train<jupyter_code># Train the dataset to predict the input and output tests 
# with a set size for the maximum amount that can be tested
inputs_train, inputs_test, outputs_train, outputs_test = mod.train_test_split(inputs, outputs, test_size=0.5)<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code># Source code adapted from:
# - https://github.com/ianmcloughlin/jupyter-teaching-notebooks/blob/master/knn-iris.ipynb
# Predict the species from the inputs 
prediction1 = knn.predict([[5.1, 3.5, 1.4, 0.2]])
prediction2 = knn.predict([[5.9, 3. , 5.1, 1.8]])
# This should be setosa as it is the first input in the list of inputs
prediction1
# This should be virginica as it is the last input in the list of inputs
prediction2
# Source code adapted from: 
# - https://stackoverflow.com/questions/1058712/how-do-i-select-a-random-element-from-an-array-in-python
# Takes a random array of inputs from the iris' measurements
rand = random.choice(inputs)
rand
# Predicts the species that matches the random features
prediction3 = knn.predict([rand])
# This should predict the species of the iris according to the random measurements chosen
prediction3<jupyter_output><empty_output><jupyter_text>Accuracy<jupyter_code># Source code adapted from:
# - https://kevinzakka.github.io/2016/07/13/k-nearest-neighbor/
# Predicts the species from the input tests in the train
pred = knn.predict(inputs_test)
pred
# It shows how accurate it is in determining the species of iris in %
perc = (accuracy_score(outputs_test, pred)) * 100
print(perc,"%")<jupyter_output>96.0 %
{'fraction_non_alphanumeric': 0.0823210284231631, 'fraction_numerical': 0.014388489208633094, 'mean_word_length': 4.901183020180932, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 53, 'https://': 12, 'lorem ipsum': 0, 'www.': 3, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '244573', 'license': 'Apache-2.0', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2453, 'n_words': 963, 'path': '2.Iris-Dataset-Notebook/iris-dataset-notebook.ipynb', 'repo_name': 'cristina1997/Emerging_Technologies_Project', 'size': '8486'}