[PYTHON] Artificial intelligence, machine learning, deep learning to implement and understand

Introduction

AI (artificial intelligence) is popular, but we will understand what artificial intelligence, machine learning, and deep learning are like while actually moving them. However, we'll just get a feel for what it's like, so we'll leverage the library framework instead of implementing it from scratch.

Target audience

――People studying artificial intelligence from now on --People who want to know the difference between artificial intelligence, machine learning, and deep learning

goal

--Understand the relationship between artificial intelligence, machine learning, and deep learning ――I can imagine what artificial intelligence, machine learning, and deep learning are like.

What is artificial intelligence?

Think about what artificial intelligence is in the first place. "[Does artificial intelligence surpass humans?](Https://www.amazon.co.jp/%E4%BA%BA%E5%B7%A5%E7%9F%A5%E8%83%BD%E3% 81% AF% E4% BA% BA% E9% 96% 93% E3% 82% 92% E8% B6% 85% E3% 81% 88% E3% 82% 8B% E3% 81% 8B-% E3% 83 % 87% E3% 82% A3% E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0 % E3% 81% AE% E5% 85% 88% E3% 81% AB% E3% 81% 82% E3% 82% 8B% E3% 82% 82% E3% 81% AE-% E8% A7% 92% E5% B7% 9DEPUB% E9% 81% B8% E6% 9B% B8-% E6% 9D% BE% E5% B0% BE-% E8% B1% 8A / dp / 4040800206) ”explains as follows: It has been.

True artificial intelligence-that is, a "computer that thinks like a human" has not been created.

In other words, the technology that seems to be acting like a human being is generally called ** artificial intelligence **. Furthermore, the definition of artificial intelligence itself has not been established among experts.

Level-based artificial intelligence

What is called artificial intelligence in the world can be divided into four levels: "[Does artificial intelligence exceed humans](https://www.amazon.co.jp/%E4%BA%BA%E5%B7] % A5% E7% 9F% A5% E8% 83% BD% E3% 81% AF% E4% BA% BA% E9% 96% 93% E3% 82% 92% E8% B6% 85% E3% 81% 88 % E3% 82% 8B% E3% 81% 8B-% E3% 83% 87% E3% 82% A3% E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0% E3% 81% AE% E5% 85% 88% E3% 81% AB% E3% 81% 82% E3% 82% 8B% E3% 82% 82% E3% 81% AE-% E8% A7% 92% E5% B7% 9DEPUB% E9% 81% B8% E6% 9B% B8-% E6% 9D% BE% E5% B0% BE-% E8 % B1% 8A / dp / 4040800206) ”.

--Level 1: ** Simple control program (control engineering) ** ――It is called "AI" in marketing. --Belonging to the fields of control engineering and system engineering

--Level 2: ** Classic artificial intelligence ** ――It is from this level that it is being researched as artificial intelligence --Various behavior patterns --Inference, search, knowledge base, etc. --Do not learn by yourself

--Level 3: ** Machine learning ** --The relationship between input and output is learned based on the data --Learn by yourself

--Level 4: ** Deep Learning ** --Learning features for learning from data

Differences between artificial intelligence, machine learning, and deep learning

Artificial intelligence, machine learning, and deep learning are as follows.

--Artificial intelligence: One field (definition itself is ambiguous) --Machine learning: A collection of methods that realize artificial intelligence that the program itself learns --Deep learning: A deeper layer of neural networks, which is one of the machine learning methods.

The relationship between artificial intelligence / machine learning / deep learning and artificial intelligence by level is as shown in the figure.

ai.png

Implement and try

Let's actually move it. It will be Level 2, Level 3, and Level 4 artificial intelligence to be implemented. At each level, the correct answer rate (0.0 to 1.0) is calculated as a score.

First, let's talk about the data that artificial intelligence predicts. Use the iris dataset from scikit-learn.

Artificial intelligence predicts the result from the input data (features). This time, there are 3 varieties of irises to predict, and we will predict which iris to use. The features for prediction are as follows.

--The length of the calyx --Width of calyx --Petal length --Petal width

Let's take a look at the contents of the data. The last target column is the name of the iris.

   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  target
0                5.1               3.5                1.4               0.2  setosa
1                4.9               3.0                1.4               0.2  setosa
2                4.7               3.2                1.3               0.2  setosa
3                4.6               3.1                1.5               0.2  setosa
4                5.0               3.6                1.4               0.2  setosa
5                5.4               3.9                1.7               0.4  setosa
6                4.6               3.4                1.4               0.3  setosa
7                5.0               3.4                1.5               0.2  setosa
8                4.4               2.9                1.4               0.2  setosa
9                4.9               3.1                1.5               0.1  setosa

Classic artificial intelligence (level 2)

First, we will implement classical artificial intelligence. This time, I found a trend from the data and put in some simple knowledge.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

#Data import
iris = load_iris()

#Divide into features and objective variables
X = iris['data']
y = iris['target']
#Divide into data to be trained and data to measure the accuracy of the trained model
#Level 2 implementation does not learn by itself, so only data that measures accuracy is used
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

#A function with embedded rules (knowledge) to predict
#Predict by branching according to the feature value
def predict_iris(feature):
    if feature[2] < 2 and feature[3] < 0.6:
        return 0
    elif 2 <= feature[2] < 5 and 0.6 <= feature[3] < 1.7:
        return 1
    else:
        return 2

#Function to measure score
def compute_score(pred, ans):
    correct_answer_num = 0
    for p, a in zip(pred, ans):
        if p == a:
            correct_answer_num += 1
    return correct_answer_num / len(pred)

pred = []
for feature in X_test:
    pred.append(predict_iris(feature))

score = compute_score(pred, y_test)

print('Score is', score)

result

Score is 0.9555555555555556

Machine learning (level 3)

Next, we will implement machine learning. In machine learning, a program learns from data.

The algorithm used this time is logistic regression, which uses the Python machine learning library scikit-learn.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

#Data import
iris = load_iris()

#Divide into features and objective variables
X = iris['data']
y = iris['target']
#Divide into data to be trained and data to measure the accuracy of the trained model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

#The algorithm used is logistic regression
model = LogisticRegression()
#Learning
model.fit(X_train, y_train)
#Score measurement
score = model.score(X_test, y_test)

print('Score is', score)

result

Score is 0.8888888888888888

Deep learning (level 4)

Finally, the implementation of deep learning. In deep learning, a program generates features from data and learns them.

The deep learning implemented this time will be a neural network consisting of 5 layers. The library to use will be keras.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from keras import layers
from keras import models
from keras.utils import np_utils

#Data import
iris = load_iris()

#Divide into features and objective variables
X = iris['data']
y = np_utils.to_categorical(iris['target'])
#Divide into data to be trained and data to measure the accuracy of the trained model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

#Define a model of a 5-layer neural network
model = models.Sequential()
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(3, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

#Learning
model.fit(X_train, y_train, epochs=10, batch_size=10)

#Score measurement
score = model.evaluate(X_test, y_test)

print('Score is', score[1])

result

Score is 0.8888888955116272

Summary

Artificial intelligence, machine learning, and deep learning had the following meanings.

--Artificial intelligence: Field (definition itself is ambiguous) --Machine learning: A collection of methods to realize artificial intelligence that the program itself learns --Deep learning: One of the machine learning methods

For each score, the result was that the simplest rule base was high. However, please note that this time the data was very simple, so the true value of machine learning and deep learning has not been demonstrated.

Artificial intelligence / AI has become a hot topic these days, but I felt that what I wanted to do (purpose) was more important than means (whether it was AI or not).

References

-[Is artificial intelligence surpassing humans](https://www.amazon.co.jp/%E4%BA%BA%E5%B7%A5%E7%9F%A5%E8%83%BD%E3% 81% AF% E4% BA% BA% E9% 96% 93% E3% 82% 92% E8% B6% 85% E3% 81% 88% E3% 82% 8B% E3% 81% 8B-% E3% 83 % 87% E3% 82% A3% E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0 % E3% 81% AE% E5% 85% 88% E3% 81% AB% E3% 81% 82% E3% 82% 8B% E3% 82% 82% E3% 81% AE-% E8% A7% 92% E5% B7% 9DEPUB% E9% 81% B8% E6% 9B% B8-% E6% 9D% BE% E5% B0% BE-% E8% B1% 8A / dp / 4040800206) -[Deep Learning Textbook Deep Learning G Test (Generalist) Official Text](https://www.amazon.co.jp/%E6%B7%B1%E5%B1%A4%E5%AD%A6%E7%BF % 92% E6% 95% 99% E7% A7% 91% E6% 9B% B8-% E3% 83% 87% E3% 82% A3% E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0-G% E6% A4% 9C% E5% AE% 9A-% E3% 82% B8% E3% 82 % A7% E3% 83% 8D% E3% 83% A9% E3% 83% AA% E3% 82% B9% E3% 83% 88-% E5% 85% AC% E5% BC% 8F% E3% 83% 86% E3% 82% AD% E3% 82% B9% E3% 83% 88 / dp / 4798157554)

Recommended Posts

Artificial intelligence, machine learning, deep learning to implement and understand
Organize machine learning and deep learning platforms
Record the steps to understand machine learning
Introduction to Deep Learning ~ Convolution and Pooling ~
Summary of recommended APIs for artificial intelligence, machine learning, and AI
Introduction to Deep Learning ~ Localization and Loss Function ~
Introduction to machine learning
School service (free / paid) where you can learn programming language Python and artificial intelligence technology (machine learning / deep learning)
An introduction to machine learning
Understand machine learning ~ ridge regression ~.
Machine learning and mathematical optimization
Deep Reinforcement Learning 1 Introduction to Reinforcement Learning
Super introduction to machine learning
Introduction to Deep Learning ~ Backpropagation ~
Neural network to understand and implement in high school mathematics
Personal memos and links related to machine learning ③ (BI / Visualization)
"Deep copy" and "Shallow copy" to understand with the smallest example
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
I tried to implement Perceptron Part 1 [Deep Learning from scratch]
I tried to implement Deep VQE
Introduction to machine learning Note writing
Significance of machine learning and mini-batch learning
Creating artificial intelligence by machine learning using TensorFlow from zero knowledge-Introduction 1
Implement Deep Learning / VAE (Variational Autoencoder)
[Machine learning] Understand from mathematics why the correlation coefficient ranges from -1 to 1.
Introduction to Deep Learning ~ Function Approximation ~
Deep learning to start without GPU
Classification and regression in machine learning
Introduction to Deep Learning ~ Coding Preparation ~
Introduction to Machine Learning Library SHOGUN
Try to implement and understand the segment tree step by step (python)
Introduction to Deep Learning ~ Dropout Edition ~
Introduction to Deep Learning ~ Forward Propagation ~
Introduction to Deep Learning ~ CNN Experiment ~
How to collect machine learning data
About the shortest path to create an image recognition model by machine learning and implement an Android application
I tried to implement deep learning that is not deep with only NumPy
Search for technical blogs by machine learning focusing on "easiness to understand"
(Machine learning) I tried to understand Bayesian linear regression carefully with implementation.
I tried to classify Oba Hana and Emiri Otani by deep learning
Machine learning beginners take Coursera's Deep learning course
[Machine learning] OOB (Out-Of-Bag) and its ratio
Introduction to Machine Learning: How Models Work
Reinforcement learning to learn from zero to deep
Implement and understand union-find trees in Go
I installed Python 3.5.1 to study machine learning
Meaning of deep learning models and parameters
An introduction to OpenCV for machine learning
How to study deep learning G test
Introduction to ClearML-Easy to manage machine learning experiments-
Image alignment: from SIFT to deep learning
Machine learning algorithm classification and implementation summary
Python and machine learning environment construction (macOS)
How to enjoy Coursera / Machine Learning (Week 10)
"OpenCV-Python Tutorials" and "Practical Machine Learning System"
Understand and implement ridge regression (L2 regularization)
Machine learning
Build a python environment to learn the theory and implementation of deep learning
I tried to implement various methods for machine learning (prediction model) using scikit-learn.
I tried to process and transform the image and expand the data for machine learning
I tried to implement Cifar10 with SONY Deep Learning library NNabla [Nippon Hurray]