[Python] I made a classifier for irises [Machine learning]

I made a classifier for iris while referring to this course, so it is a memorandum. [Must-see for beginners! Completely capture neural networks and deep learning with Python]

environment

Mac OS Catalina 10.15.7 Spyder 4.1.4 Anaconda 3 Python 3.7.9 Keras 2.3.1

What i did

This is the code I created.

iris.py


from sklearn.datasets import load_iris#Get iris data
iris = load_iris() 
from sklearn.model_selection import train_test_split as split #Tool to separate datasets
X_train,X_test,y_train,y_test = split(iris.data, iris.target, train_size = 0.8) #80 of the dataset%For learning, 20%For experimentation
import keras 

#Creating a neural network
 #Dense:Neural network definition class
 #Activateion:Activation function class
from keras.layers import Dense, Activation 
model = keras.models.Sequential()#Make a model Make a container
model.add(Dense(units =32,input_dim = 4 )) #32 intermediate layers, 4 input layers
model.add(Activation('relu'))#Activation function Relu
model.add(Dense(units = 3))#Output layer:Three
model.add(Activation('softmax'))#Activation function softmax

#compile
model.compile( 
loss = 'sparse_categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy']) 

#Execution of learning
model.fit(X_train,y_train,epochs = 100)#100 times iterative learning

#Perform evaluation
 #Check the correct answer rate of test data
score = model.evaluate(X_test,y_test,batch_size = 1)#loss in score(loss) ,accuracy(accuracy)Vector
#Reference: https://aidiary.hatenablog.com/category/Keras?page=1478696865 
accuracy = score[1] 
print('accuracy="',str(accuracy))#To combine numbers and strings with print, str()Make a string with
#Reference: https://www.javadrive.jp/python/string/index9.html 

 #Check only one data
import numpy as np 
x = np.array([[5.1,3.5,1.4,0.2]])#X_Create an array in the same format as train
r = model.predict(x)#Probability vector
print('Probability per label=',r) 
print('Label with the highest probability=',r.argmax())#argmax()Returns the largest label in the vector

#Output data
json_string = model.to_json() 
#If you want to import a model
#from keras.models import model_fromjson #model = model_from_json/json_string) 
#Save learning parameters
#Install h5py first
model.save_weights('param.hdf5') 
#When reading
#model.load_weight('param.hdf5')  

Execution result

accuracy=" 0.9666666388511658
Probability per label= [[0.9405338  0.05598015 0.00348606]]
Label with the highest probability= 0

Recommended Posts

[Python] I made a classifier for irises [Machine learning]
I made a python dictionary file for Neocomplete
[Updated Ver1.3.1] I made a data preprocessing library DataLiner for machine learning.
I made a python text
Memo for building a machine learning environment using Python
I made a C ++ learning site
I made a Line-bot using Python!
I made a fortune with Python.
<For beginners> python library <For machine learning>
I made a daemon with Python
[VSCode] I made a user snippet for Python print f-string
I tried using Tensorboard, a visualization tool for machine learning
I made a dash docset for Holoviews
Amplify images for machine learning with python
I made a payroll program in Python!
How about Anaconda for building a machine learning environment in Python?
Building a Windows 7 environment for getting started with machine learning with Python
Python> I made a test code for my own external file
I made a character counter with Python
Why Python is chosen for machine learning
I made a lot of files for RDP connection with Python
[Python] Web application design for machine learning
I made a Hex map with Python
An introduction to Python for machine learning
I made a scaffolding tool for the Python web framework Bottle
I made a tool that makes it convenient to set parameters for machine learning models.
After studying Python3, I made a Slackbot
I made a roguelike game with Python
Creating a development environment for machine learning
I made a simple blackjack with Python
I made a Python wrapper library for docomo image recognition API.
I made a configuration file with Python
I made a library for actuarial science
A textbook for beginners made by Python beginners
I made a neuron simulator with Python
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
I made a Dir en gray face classifier using TensorFlow --⑥ Learning program
I made a Dir en gray face classifier using TensorFlow --- ⑧ Learning execution
I made a Dir en gray face classifier using TensorFlow --- ⑦ Learning model
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a GUI application with Python + PyQt5
Python & Machine Learning Study Memo ⑤: Classification of irises
I made a Twitter fujoshi blocker with Python ①
Procedure for creating a LineBot made with Python
[Python] I made a Youtube Downloader with Tkinter.
[Python] Collect images with Icrawler for machine learning [1000 images]
I made a downloader for word distributed expression
Get a glimpse of machine learning in Python
I started machine learning with Python Data preprocessing
I made a peeping prevention product for telework.
I made a Caesar cryptographic program in Python.
I installed Chainer, a framework for deep learning
Build a Python machine learning environment with a container
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
What I learned about AI / machine learning using Python (1)
I made a Twitter BOT with GAE (python) (with a reference)