[Python] Determine the type of iris with SVM

Let's try a machine learning classification problem using iris data, which is often used in beginner lectures.

Data to use

irei data. (Iris means "iris" flower.) Data on three iris varieties, Setosa Versicolor Virginica. A total of 150 datasets.

Data set contents

Sepal Length: The length of the sepal Sepal Width: Width of sepal Petal Length: Petal Length Petal Width: Width of petals Name: Iris-Setosa, Iris-Vesicolor, and Iris-Virginica variety data image.png

Model to adopt

SVM (Support Vector Machine). SVMs are suitable for supervised learning classification problems. A model that can also make spam discriminators. Since it is supervised learning, feature data and objective variables are required.

Overall flow

  1. Data preparation
  2. Visualize the data
  3. Learning and evaluating the model

Practice

1) Data preparation

First, after importing the required library, import the data and check.

import numpy  as np
import pandas as pd
import seaborn as sns
sns.set_style("whitegrid")
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.model_selection import train_test_split, cross_validate

df = pd.read_csv("iris.csv")
df.head()

	SepalLength	SepalWidth	PetalLength	PetalWidth	Name
0	5.1	3.5	1.4	0.2	Iris-setosa
1	4.9	3.0	1.4	0.2	Iris-setosa
2	4.7	3.2	1.3	0.2	Iris-setosa
3	4.6	3.1	1.5	0.2	Iris-setosa
4	5.0	3.6	1.4	0.2	Iris-setosa

Divide into training data and test data.

X = df[["SepalLength","SepalWidth","PetalLength","PetalWidth"]]
y = df["Name"]

X_train, X_test, y_train, y_test = train_test_split(X, y)
X_train.shape, X_test.shape

((112, 4), (38, 4)) #Check the number of matrices after division

Create a Dataframe for each of the training data and test data.

data_train = pd.DataFrame(X_train)
data_train["Name"] = y_train

2) Visualize the data

In the verification data, what is the actual relationship between the type of iris and the amount of excess acquired? Plot and see if there are differences in features for each type.

sns.pairplot(data_train, hue='Name', palette="husl")

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/269450/43205a3a-1981-ad86-f9a3-d14a656ec628.png)


Certainly, it seems that there is a clear difference in each feature amount depending on the type of iris.

3) Learning and evaluating the model

Let's actually put the verification data in the SVM and create a model.

X_train = data_train[["SepalLength", "SepalWidth","PetalLength"]].values
y_train = data_train["Name"].values

from sklearn import svm,metrics

clf = svm.SVC()
clf.fit(X_train,y_train)

Enter test data into the created model.

pre = clf.predict(X_test)

Evaluate the model.

ac_score = metrics.accuracy_score(y_test,pre)
print("Correct answer rate=", ac_score)

Correct answer rate= 0.9736842105263158

It was confirmed that the test data and the results obtained by the model were 97% consistent.

Recommended Posts

[Python] Determine the type of iris with SVM
Master the type with Python [Python 3.9 compatible]
Check the existence of the file with python
[python] [meta] Is the type of python a type?
Prepare the execution environment of Python3 with Docker
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
the zen of Python
Check the date of the flag duty with Python
Convert the character code of the file with Python3
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
Destroy the intermediate expression of the sweep method with Python
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
Summary of the basic flow of machine learning with Python
Get the operation status of JR West with Python
Towards the retirement of Python2
Python --Check type of values
About the ease of Python
Determine prime numbers with python
Call the API with python3.
About the features of Python
The Power of Pandas: Python
I tried to find the entropy of the image with python
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
Visualize the results of decision trees performed with Python scikit-learn
I wrote the basic grammar of Python with Jupyter Lab
Tank game made with python About the behavior of tanks
Get the MIME type in Python and determine the file format
Issue WOL with Scapy of Python3 (LAN controller specified type)
I evaluated the strategy of stock system trading with Python.
Check the scope of local variables with the Python locals function.
Consideration for Python decorators of the type that passes variables
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
[Homology] Count the number of holes in data with Python
Try to automate the operation of network devices with Python
Rewrite the record addition node of SPSS Modeler with Python.
Treat the Interface class like that with Python type annotations
Estimate the attitude of AR markers with Python + OpenCV + drone
Play with the password mechanism of GitHub Webhook and Python
[Data science memorandum] Confirmation of the contents of DataFrame type [python]
Get the source of the page to load infinitely with python.
Extract the xz file with python
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
Get the weather with Python requests 2
pyenv-change the python version of virtualenv
Getting Started with Python Basics of Python
Find the Levenshtein Distance with python
Change the Python version of Homebrew
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2
Life game with Python! (Conway's Game of Life)
[Python] Understanding the potential_field_planning of Python Robotics
10 functions of "language with battery" python
I liked the tweet with python. ..
Implementation of Dijkstra's algorithm with python