Tensorflow(アイリス訓練データ) | python3Xのブログ

python3Xのブログ

ここでは40代、50代の方が日々の生活で役に立つ情報や私の趣味であるプログラム、Excelや科学に関する内容で投稿する予定です。

scikite-learnでおなじみのアイリス訓練データをTensorflowを使って行います。

データ数:120、2行目の左からがく片長さ、がく片幅、花弁長さ、花弁幅、種類

種類:0はセトサ、1はバージカラー、2はバージニカ

120    4    setosa    versicolor    virginica
6.4  ,  2.8  ,  5.6    ,      2.2     ,       2     ,
5.0  ,  2.3  ,  3.3    ,      1.0     ,       1     ,
4.9  ,  2.5  ,  4.5    ,      1.7     ,       2     ,
4.9  ,  3.1  ,  1.5    ,      0.1     ,       0     ,
5.7  ,  3.8  ,  1.7    ,      0.3     ,       0     ,
4.4  ,  3.2  ,  1.3    ,      0.2     ,       0     ,
5.4  ,  3.4  ,  1.5    ,      0.4     ,       0     ,
6.9  ,  3.1  ,  5.1    ,      2.3     ,       2     ,
6.7  ,  3.1  ,  4.4    ,      1.4     ,       1     ,
5.1  ,  3.7  ,  1.5    ,      0.4     ,       0     ,
      ,        ,          ,               ,              ,
# python2系でもpython3系でも実行できるように
from __future__ import absolute_import
from __future__ import division, print_function, unicode_literals
import os
import urllib.request as urllib
 
import numpy as np
import tensorflow as tf
 
# データセットの保存場所
IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"
 
IRIS_TEST = "iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"
 
def main():
  # 訓練セットとテストセットがローカルフォルダに存在     しないならダウンロードする
  if not os.path.exists(IRIS_TRAINING):
    raw = urllib.urlopen(IRIS_TRAINING_URL).read()
    f = open(IRIS_TRAINING, "w", encoding='cp932')
    f.write(raw)
 
  if not os.path.exists(IRIS_TEST):
    raw = urllib.urlopen(IRIS_TEST_URL).read()
    f = open(IRIS_TEST, "w", encoding='cp932')
    f.write(raw)
 
  # データセットを読み込む
  training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TRAINING,
      target_dtype=np.int,
      features_dtype=np.float32)
  test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TEST,
      target_dtype=np.int,
      features_dtype=np.float32)
 
  # (がく、花弁)4つの特徴量の列
  feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]
 
  # ニューロン数が 10, 20, 10個隠れ層、出力層3個の
tf.estimator.DNNClassifier をインスタンス化する
  classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                          hidden_units=[10, 20, 10],
                                          n_classes=3,
                                          model_dir="/tmp/iris_model")
  # 訓練入力パイプラインを定義
  train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": np.array(training_set.data)},
      y=np.array(training_set.target),
      num_epochs=None,
      shuffle=True)
 
  # DNNClassifier をアイリス訓練データにフィットさせる
  # 訓練モデル
  classifier.train(input_fn=train_input_fn, steps=2000)
 
  # モデル精度を評価する
  # テスト入力を定義
  test_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": np.array(test_set.data)},
      y=np.array(test_set.target),
      num_epochs=1,
      shuffle=False)
 
  # 精度を評価する
  accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]
 
  print("\nTest Accuracy: {0:f}\n".format(accuracy_score))
 
  # 2つの花サンプルを分類する
  new_samples = np.array(
      [[6.4, 3.2, 4.5, 1.5],
       [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)
  predict_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": new_samples},
      num_epochs=1,
      shuffle=False)
 
  predictions = list(classifier.predict(input_fn=predict_input_fn))
  predicted_classes = [p["classes"] for p in predictions]
 
  print(
      "New Samples, Class Predictions:    {}\n"
      .format(predicted_classes))
 
if __name__ == "__main__":
    main()
====================
Test Accuracy: 0.933333
New Samples, Class Predictions:    [array([b'1'], dtype=object), array([b'2'], dtype=object)]
正解率は93.3%
花サンプルはそれぞれ
バージカラー
バージニカ
と分類されました。