アヤメの分類 tensorflow

import tensorflow as tf
import tensorflow.keras as keras
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import matplotlib
matplotlib.use('TkAgg')  
import matplotlib.pyplot as plt

# アヤメデータの読み込み --- (*1)
iris_data = pd.read_csv("iris.csv", encoding="utf-8")

# アヤメデータをラベルと入力データに分離する
y_labels = iris_data.loc[:,"Name"]
x_data = iris_data.loc[:,["SepalLength","SepalWidth","PetalLength","PetalWidth"]]

# ラベルデータをone-hotベクトルに直す
labels = {
    'Iris-setosa': [1, 0, 0], 
    'Iris-versicolor': [0, 1, 0], 
    'Iris-virginica': [0, 0, 1]
}
y_nums = np.array(list(map(lambda v : labels[v] , y_labels)))
x_data = np.array(x_data)

# 学習用とテスト用に分割する --- (*2)
x_train, x_test, y_train, y_test = train_test_split(
    x_data, y_nums, train_size=0.8)

# モデル構造を定義 --- (*3)
Dense = keras.layers.Dense
model = keras.models.Sequential()
model.add(Dense(10, activation='relu', input_shape=(4,)))
model.add(Dense(3, activation='softmax')) # ---(*3a)

# モデルを構築 --- (*4)
model.compile(
    loss='categorical_crossentropy',
    optimizer='adam',
    metrics=['accuracy'])

# 学習を実行 --- (*5)
# model.fit(x_train, y_train,
#     batch_size=20,
#     epochs=10)
epochs=300
result = model.fit(x_train, y_train, batch_size=20, epochs=epochs)

# モデルを評価 --- (*6)
score = model.evaluate(x_test, y_test, verbose=1)
print('正解率=', score[1], 'loss=', score[0])


#学習の様子をグラフに描写
plt.plot(range(1, epochs+1),
         result.history['accuracy'], label="training")
plt.plot(range(1, epochs+1),
         result.history['loss'], label="loss")
plt.xlabel('Epochs=' + str(epochs))
plt.ylabel('Accuracy')
plt.legend()
plt.show()
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9695 - loss: 0.2152 
Epoch 299/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9671 - loss: 0.2169 
Epoch 300/300
6/6 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - accuracy: 0.9651 - loss: 0.2248 
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 115ms/step - accuracy: 1.0000 - loss: 0.1521
正解率= 1.0 loss= 0.1521015763282776
タイトルとURLをコピーしました