<jupyter_start><jupyter_text>Hello Kagglers!!This is a very basic tutorial to Machine Learning for complete Beginners using the Iris Dataset. You can learn how to implement machine learning on a given dataset by following this notebook. I have explained everything related to the implementation in detail . Hope you find it useful.For a more advanced notebook that covers some more detailed concepts, have a look at [this notebook](https://www.kaggle.com/ash316/ml-from-scratch-part-2/notebook)<jupyter_code>#basado en https://www.kaggle.com/gayatripushpita/ml-using-the-iris-dataset

# ignore all future warnings
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns
import matplotlib.pyplot as plt
iris = pd.read_csv("Iris2.csv") #load the dataset
iris.head(2) #show the first 2 rows from the dataset
iris.info()  #checking if there is any inconsistency in the dataset
#as we see there are no null values in the dataset, so the data can be processed<jupyter_output><empty_output><jupyter_text>Removing the unneeded column<jupyter_code>iris.drop('Id',axis=1,inplace=True) #dropping the Id column as it is unecessary, axis=1 specifies that it should be column wise, inplace =1 means the changes should be reflected into the dataframe<jupyter_output><empty_output><jupyter_text>Some Exploratory Data Analysis With Iris<jupyter_code>fig = iris[iris.Species=='Iris-setosa'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',color='orange', label='Setosa')
iris[iris.Species=='Iris-versicolor'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',color='blue', label='versicolor',ax=fig)
iris[iris.Species=='Iris-virginica'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',color='green', label='virginica', ax=fig)
fig.set_xlabel("Sepal Length")
fig.set_ylabel("Sepal Width")
fig.set_title("Sepal Length VS Width")
fig=plt.gcf()
fig.set_size_inches(10,6)
plt.show()<jupyter_output><empty_output><jupyter_text>The above graph shows relationship between the sepal length and width. Now we will check relationship between the petal length and width.<jupyter_code>fig = iris[iris.Species=='Iris-setosa'].plot.scatter(x='PetalLengthCm',y='PetalWidthCm',color='orange', label='Setosa')
iris[iris.Species=='Iris-versicolor'].plot.scatter(x='PetalLengthCm',y='PetalWidthCm',color='blue', label='versicolor',ax=fig)
iris[iris.Species=='Iris-virginica'].plot.scatter(x='PetalLengthCm',y='PetalWidthCm',color='green', label='virginica', ax=fig)
fig.set_xlabel("Petal Length")
fig.set_ylabel("Petal Width")
fig.set_title(" Petal Length VS Width")
fig=plt.gcf()
fig.set_size_inches(10,6)
plt.show()<jupyter_output><empty_output><jupyter_text>As we can see that the Petal Features are giving a better cluster division compared to the Sepal features. This is an indication that the Petals can help in better and accurate Predictions over the Sepal. We will check that later.  Now let us see how are the length and width are distributed<jupyter_code>iris.hist(edgecolor='black', linewidth=1.2)
fig=plt.gcf()
fig.set_size_inches(12,6)
plt.show()<jupyter_output><empty_output><jupyter_text>Now let us see how the length and width vary according to the species<jupyter_code>plt.figure(figsize=(15,10))
plt.subplot(2,2,1)
sns.violinplot(x='Species',y='PetalLengthCm',data=iris)
plt.subplot(2,2,2)
sns.violinplot(x='Species',y='PetalWidthCm',data=iris)
plt.subplot(2,2,3)
sns.violinplot(x='Species',y='SepalLengthCm',data=iris)
plt.subplot(2,2,4)
sns.violinplot(x='Species',y='SepalWidthCm',data=iris)<jupyter_output><empty_output><jupyter_text>The violinplot shows density of the length and width in the species. The thinner part denotes that there is less density whereas the fatter part conveys higher density  Now the given problem is a classification problem.. Thus we will be using the classification algorithms to build a model.**Classification**: samples belong to two or more classes and we want to learn from already labeled data how to predict the class of unlabeled data**Regression**: if the desired output consists of one or more continuous variables, then the task is called regression. An example of a regression problem would be the prediction of the length of a salmon as a function of its age and weight. Before we start, we need to clear some ML notations.**attributes**-->An attribute is a property of an instance that may be used to determine its classification. In the following dataset, the attributes are the petal and sepal length and width. It is also known as **Features**.**Target variable**, in the machine learning context is the variable that is or should be the output. Here the target variables are the 3 flower species.<jupyter_code># importing alll the necessary packages to use the various classification algorithms
from sklearn.linear_model import LogisticRegression  # for Logistic Regression algorithm
from sklearn.model_selection import train_test_split #to split the dataset for training and testing
from sklearn.neighbors import KNeighborsClassifier  # for K nearest neighbours
from sklearn import svm  #for Support Vector Machine (SVM) Algorithm
from sklearn import metrics #for checking the model accugracy
from sklearn.tree import DecisionTreeClassifier #for using Decision Tree Algoithm
iris.shape #get the shape of the dataset<jupyter_output><empty_output><jupyter_text>Now, when we train any algorithm, the number of features and their correlation plays an important role. If there are features and many of the features are highly correlated, then training an algorithm with all the featues will reduce the accuracy. Thus features selection should be done carefully. This dataset has less featues but still we will see the correlation.<jupyter_code>plt.figure(figsize=(7,4)) 
sns.heatmap(iris.corr(),annot=True,cmap='cubehelix_r') #draws  heatmap with input as the correlation matrix calculted by(iris.corr())
plt.show()<jupyter_output><empty_output><jupyter_text>**Observation--->**The Sepal Width and Length are not correlatedThe Petal Width and Length are highly correlatedWe will use all the features for training the algorithm and check the accuracy.Then we will use 1 Petal Feature and 1 Sepal Feature to check the accuracy of the algorithm as we are using only 2 features that are not correlated. Thus we can have a variance in the dataset which may help in better accuracy. We will check it later.  Steps To Be followed When Applying an Algorithm 1. Split the dataset into training and testing dataset. The testing dataset is generally smaller than training one as it will help in training the model better. 2. Select any algorithm based on the problem (classification or regression) whatever you feel may be good. 3. Then pass the training dataset to the algorithm to train it. We use the **.fit()** method 4. Then pass the testing data to the trained algorithm to predict the outcome. We use the **.predict()** method. 5. We then check the accuracy by **passing the predicted outcome and the actual output** to the model.  Splitting The Data into Training And Testing Dataset<jupyter_code>train, test = train_test_split(iris, test_size = 0.3)# in this our main data is split into train and test
# the attribute test_size=0.3 splits the data into 70% and 30% ratio. train=70% and test=30%
print(train.shape)
print(test.shape)
train_X = train[['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm']]# taking the training data features
train_y=train.Species# output of our training data
test_X= test[['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm']] # taking test data features
test_y =test.Species   #output value of test data<jupyter_output><empty_output><jupyter_text>Lets check the Train and Test Dataset<jupyter_code>train_X.head(2)
test_X.head(2)
train_y.head()  ##output of the training data<jupyter_output><empty_output><jupyter_text>Support Vector Machine (SVM)<jupyter_code>model = svm.SVC() #select the algorithm
model.fit(train_X,train_y) # we train the algorithm with the training data and the training output
prediction=model.predict(test_X) #now we pass the testing data to the trained algorithm
print('The accuracy of the SVM is:',metrics.accuracy_score(prediction,test_y))#now we check the accuracy of the algorithm. 
#we pass the predicted output by the model and the actual output<jupyter_output>The accuracy of the SVM is: 0.9777777777777777<jupyter_text>SVM is giving very good accuracy . We will continue to check the accuracy for different models.Now we will follow the same steps as above for training various machine learning algorithms.  Logistic Regression<jupyter_code>model = LogisticRegression()
model.fit(train_X,train_y)
prediction=model.predict(test_X)
print('The accuracy of the Logistic Regression is',metrics.accuracy_score(prediction,test_y))<jupyter_output>The accuracy of the Logistic Regression is 0.9555555555555556<jupyter_text>Decision Tree<jupyter_code>model=DecisionTreeClassifier()
model.fit(train_X,train_y)
prediction=model.predict(test_X)
print('The accuracy of the Decision Tree is',metrics.accuracy_score(prediction,test_y))<jupyter_output>The accuracy of the Decision Tree is 0.9555555555555556<jupyter_text>K-Nearest Neighbours<jupyter_code>model=KNeighborsClassifier(n_neighbors=3) #this examines 3 neighbours for putting the new data into a class
model.fit(train_X,train_y)
prediction=model.predict(test_X)
print('The accuracy of the KNN is',metrics.accuracy_score(prediction,test_y))<jupyter_output>The accuracy of the KNN is 0.9555555555555556<jupyter_text>Let's check the accuracy for various values of n for K-Nearest nerighbours<jupyter_code>a_index=list(range(1,11))
a=pd.Series()
x=[1,2,3,4,5,6,7,8,9,10]
for i in list(range(1,11)):
    model=KNeighborsClassifier(n_neighbors=i) 
    model.fit(train_X,train_y)
    prediction=model.predict(test_X)
    a=a.append(pd.Series(metrics.accuracy_score(prediction,test_y)))
plt.plot(a_index, a)
plt.xticks(x)<jupyter_output><empty_output><jupyter_text>Above is the graph showing the accuracy for the KNN models using different values of n.   We used all the features of iris in above models. Now we will use Petals and Sepals Seperately  Creating Petals And Sepals Training Data<jupyter_code>petal=iris[['PetalLengthCm','PetalWidthCm','Species']]
sepal=iris[['SepalLengthCm','SepalWidthCm','Species']]
train_p,test_p=train_test_split(petal,test_size=0.3,random_state=0)  #petals
train_x_p=train_p[['PetalWidthCm','PetalLengthCm']]
train_y_p=train_p.Species
test_x_p=test_p[['PetalWidthCm','PetalLengthCm']]
test_y_p=test_p.Species


train_s,test_s=train_test_split(sepal,test_size=0.3,random_state=0)  #Sepal
train_x_s=train_s[['SepalWidthCm','SepalLengthCm']]
train_y_s=train_s.Species
test_x_s=test_s[['SepalWidthCm','SepalLengthCm']]
test_y_s=test_s.Species<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>model=svm.SVC()
model.fit(train_x_p,train_y_p) 
prediction=model.predict(test_x_p) 
print('The accuracy of the SVM using Petals is:',metrics.accuracy_score(prediction,test_y_p))

model=svm.SVC()
model.fit(train_x_s,train_y_s) 
prediction=model.predict(test_x_s) 
print('The accuracy of the SVM using Sepal is:',metrics.accuracy_score(prediction,test_y_s))<jupyter_output>The accuracy of the SVM using Petals is: 0.9777777777777777
The accuracy of the SVM using Sepal is: 0.8<jupyter_text>Logistic Regression<jupyter_code>model = LogisticRegression()
model.fit(train_x_p,train_y_p) 
prediction=model.predict(test_x_p) 
print('The accuracy of the Logistic Regression using Petals is:',metrics.accuracy_score(prediction,test_y_p))

model.fit(train_x_s,train_y_s) 
prediction=model.predict(test_x_s) 
print('The accuracy of the Logistic Regression using Sepals is:',metrics.accuracy_score(prediction,test_y_s))<jupyter_output>The accuracy of the Logistic Regression using Petals is: 0.6888888888888889
The accuracy of the Logistic Regression using Sepals is: 0.6444444444444445<jupyter_text>Decision Tree<jupyter_code>model=DecisionTreeClassifier()
model.fit(train_x_p,train_y_p) 
prediction=model.predict(test_x_p) 
print('The accuracy of the Decision Tree using Petals is:',metrics.accuracy_score(prediction,test_y_p))

model.fit(train_x_s,train_y_s) 
prediction=model.predict(test_x_s) 
print('The accuracy of the Decision Tree using Sepals is:',metrics.accuracy_score(prediction,test_y_s))<jupyter_output>The accuracy of the Decision Tree using Petals is: 0.9555555555555556
The accuracy of the Decision Tree using Sepals is: 0.6666666666666666<jupyter_text>K-Nearest Neighbours<jupyter_code>model=KNeighborsClassifier(n_neighbors=3) 
model.fit(train_x_p,train_y_p) 
prediction=model.predict(test_x_p) 
print('The accuracy of the KNN using Petals is:',metrics.accuracy_score(prediction,test_y_p))

model.fit(train_x_s,train_y_s) 
prediction=model.predict(test_x_s) 
print('The accuracy of the KNN using Sepals is:',metrics.accuracy_score(prediction,test_y_s))<jupyter_output>The accuracy of the KNN using Petals is: 0.9777777777777777
The accuracy of the KNN using Sepals is: 0.7333333333333333
{'fraction_non_alphanumeric': 0.0919942218505284, 'fraction_numerical': 0.021059834258344103, 'mean_word_length': 7.272955974842767, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 75, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 11, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '184558', 'license': 'MIT', 'n_tokens_mistral': 4479, 'n_tokens_neox': 4228, 'n_words': 1289, 'path': 'IRIS_caso_COMPLETO#2.ipynb', 'repo_name': 'naarean/Pandas', 'size': '13153'}