What I learned about AI / machine learning using Python (3)

Introduction

I'm studying with this book How to make AI / machine learning / deep learning apps with Python

scikit-learn A classic machine learning framework for Python http://scikit-learn.org/

It has the following features

--Supports various algorithms used in machine learning -Sample data is included so that you can try machine learning immediately --Has a function to verify the results of machine learning --High affinity with other libraries often used in machine learning (Pnadas, Numpy, scipy, Matplotlib etc) --Free commercial use due to BSD licensed open source

Select an algorithm

https://scikit-learn.org/stable/tutorial/machine_learning_map/index.html image.png You can select an algorithm by following the conditions such as what kind of machine learning you want to do and what kind of data you are preparing.

Machine learning AND operation

and.py


#Import the library
from sklearn.svm import LinearSVC            #Package for utilizing the LinearSVC algorithm(sklearn.svm.LinearSVC)
from sklearn.metrics import accuracy_score   #Package for evaluating test results(sklearn.metrics.accuracy_score)
                           

#Prepare learning data
learn_data  = [[0, 0], [1, 0], [0, 1], [1, 1]] #Input data for learning(AND input value)
learn_label = [0, 0, 0, 1]                     #Result data for learning(AND output value)

#Algorithm specification
clf = LinearSVC()  

#learn(Pass input data and result data for learning)
clf.fit(learn_data, learn_label)               #Pass input data and result data for learning

#Verify learning
test_data = [[0, 0], [1, 0], [0, 1], [1, 1]]   #Prepare input data for test data
test_label = clf.predict(test_data)            #Get test data results

#Display test data results
print(test_data, "Prediction result", test_label)

#Display correct answer rate
Accuracy_rate= accuracy_score([0, 0, 0, 1], test_label) # accuracy_score(Correct answer data,Test results)
print("Correct answer rate:", Accuracy_rate)
[[0, 0], [1, 0], [0, 1], [1, 1]]Prediction result[0 0 0 1]
Correct answer rate: 1.0

You are learning AND logical operations correctly. Those that can be classified linearly can be solved by the LinearSVC algorithm. But what about XOR?

Words used

--Linear SVM Classification ... Linear SVM Classification --accuracy ... accuracy --classification ... classification --classifier ... classifier --fit ... learn (fit) --predict ... predict

Machine learning XOR operation

xor.py


#Import the library
from sklearn.svm import LinearSVC            #Package for utilizing the LinearSVC algorithm(sklearn.svm.LinearSVC)
from sklearn.metrics import accuracy_score   #Package for evaluating test results(sklearn.metrics.accuracy_score)
                           

#Prepare learning data
learn_data  = [[0, 0], [1, 0], [0, 1], [1, 1]] #Input data for learning(XOR input value)
learn_label = [0, 1, 1, 0]                     #Result data for learning(XOR output value)

#Algorithm specification
clf = LinearSVC()  

#learn(Pass input data and result data for learning)
clf.fit(learn_data, learn_label)               #Pass input data and result data for learning

#Verify learning
test_data = [[0, 0], [1, 0], [0, 1], [1, 1]]   #Prepare input data for test data
test_label = clf.predict(test_data)            #Get test data results

#Display test data results
print(test_data, "Prediction result", test_label)

#Display correct answer rate
Accuracy_rate= accuracy_score([0, 1, 1, 0], test_label) # accuracy_score(Correct answer data,Test results)
print("Correct answer rate:", Accuracy_rate)
[[0, 0], [1, 0], [0, 1], [1, 1]]Prediction result[0 0 0 0]
Correct answer rate: 0.5

Well, it can't be classified correctly by linear.

xor2.py


#Import the library
from sklearn.neighbors import KNeighborsClassifier #Package for utilizing the LinearSVC algorithm(sklearn.svm.LinearSVC)
from sklearn.metrics import accuracy_score         #Package for evaluating test results(sklearn.metrics.accuracy_score)


#Prepare learning data
learn_data  = [[0, 0], [1, 0], [0, 1], [1, 1]] #Input data for learning(XOR input value)
learn_label = [0, 1, 1, 0]                     #Result data for learning(XOR output value)

#Algorithm specification
clf = KNeighborsClassifier(n_neighbors = 1)  

#learn(Pass input data and result data for learning)
clf.fit(learn_data, learn_label)               #Pass input data and result data for learning

#Verify learning
test_data = [[0, 0], [1, 0], [0, 1], [1, 1]]   #Prepare input data for test data
test_label = clf.predict(test_data)            #Get test data results

#Display test data results
print(test_data, "Prediction result", test_label)

#Display correct answer rate
Accuracy_rate= accuracy_score([0, 1, 1, 0], test_label) # accuracy_score(Correct answer data,Test results)
print("Correct answer rate:", Accuracy_rate)
[[0, 0], [1, 0], [0, 1], [1, 1]]Prediction result[0 1 1 0]
Correct answer rate: 1.0

I was able to learn using the KNeighborsClassifier algorithm.

Recommended Posts

What I learned about AI / machine learning using Python (1)
What I learned about AI / machine learning using Python (3)
What I learned about AI / machine learning using Python (2)
What I learned about AI and machine learning using Python (4)
What I learned about Linux
What I learned in Python
I learned about processes in Python
I installed Python 3.5.1 to study machine learning
Build AI / machine learning environment with Python
[ML-Aents] I tried machine learning using Unity and Python TensorFlow (v0.11β compatible)
A story about simple machine learning using TensorFlow
[Python3] Let's analyze data using machine learning! (Regression)
I started machine learning with Python Data preprocessing
About machine learning overfitting
What is machine learning?
[Python] I made a classifier for irises [Machine learning]
Memo for building a machine learning environment using Python
What I was addicted to when using Python tornado
I tried to compress the image using machine learning
Machine learning learned with Pokemon
Python learning plan for AI learning
Python Machine Learning Programming> Keywords
[Python] I tried using OpenPose
I implemented Extreme learning machine
Beginning with Python machine learning
I learned Python basic grammar
Build an environment for machine learning using Python on MacOSX
I started machine learning with Python Clustering & Dimension Compression & Visualization
Python beginners publish web applications using machine learning [Part 1] Introduction
What I learned by solving 30 questions of python Project Euler
I tried using Tensorboard, a visualization tool for machine learning
A note of what I learned when I thought about using pyenv or virtualenv on Windows
Machine learning A story about people who are not familiar with GBDT using GBDT in Python
Python Note: About comparison using is
[Python machine learning] Recommendation of using Spyder for beginners (as of August 2020)
What I stumbled upon using Airflow
I made a Line-bot using Python!
How about Anaconda for building a machine learning environment in Python?
I tried machine learning with liblinear
Machine learning with python (1) Overall classification
Machine learning summary by Python beginners
Notation I encountered while learning Python
[Python] What is @? (About the decorator)
Perceptron learning experiment learned with Python
I took Progete's Python Learning Course I
I tried using Thonny (Python / IDE)
Python: Preprocessing in Machine Learning: Overview
What I checked about Qiita's post
I calculated "Levenshtein distance" using Python
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
Under investigation about PYNQ-Let's do deep learning with FPGA using Python-
"Scraping & machine learning with Python" Learning memo
[Note] AI / machine learning / python related websites [updated from time to time]
[Python] I tried using YOLO v3
Application development using Azure Machine Learning
What I learned in two months before the product was released as a machine learning fucking amateur
[Definitive Edition] Building an environment for learning "machine learning" using Python on Windows
What I read about VBA x Python fastest work technique Memo chapter3
[Definitive Edition] Building an environment for learning "machine learning" using Python on Mac
Vulkan compute with Python with VkInline and think about GPU machine learning and more