前言
根据官方文档整理而来的,主要是对Iris数据集进行分类。使用tf.contrib.learn.tf.contrib.learn快速搭建一个深层网络分类器,
步骤
- 导入csv数据
- 搭建网络分类器
- 训练网络
- 计算测试集正确率
- 对新样本进行分类
数据
Iris数据集包含150行数据,有三种不同的Iris品种分类。每一行数据给出了四个特征信息和一个分类信息。
现在已经将数据分为训练集和测试集
- A training set of 120 samples (iris_training.csv)
- A test set of 30 samples (iris_test.csv)
网络搭建
1. 首先,导入tensorflow 和 numpy
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
2. 导入数据
# 定义数据地址
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"
# 导入数据
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)
load_csv_with_header() 有三个参数
- filename, 数据地址
- target_dtype, 目标值的numpy datatype(iris的目标值是0,1,2,所以是np.int)
- features_dtype, 特征值的numpy datatype .
3. 搭建网络结构
# 每行数据4个特征,都是real-value的
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
# 3层DNN,3分类问题
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="iris_model")
参数解释
- feature_columns 特征值
- hidden_units=[10, 20, 10]. 3个隐藏层,包含的隐藏神经元依次是10, 20, 10
- n_classes 类别个数
- model_dir 模型保存地址
4. 训练数据
classifier.fit(x=training_set.data, y=training_set.target, steps=2000)
steps 为训练次数
5. 计算准确率
accuracy_score = classifier.evaluate(x=test_set.data, y=test_set.target)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))
运行结果是
Accuracy: 0.966667
6. 对新样本进行预测
# Classify two new flower samples.
new_samples = np.array(
[[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = list(classifier.predict(new_samples, as_iterable=True))
print('Predictions: {}'.format(str(y)))
运行结果为:
Prediction: [1 2]
完整代码
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"
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)
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="iris_model")
classifier.fit(x=training_set.data,
y=training_set.target,
steps=2000)
accuracy_score = classifier.evaluate(x=test_set.data,
y=test_set.target)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))
new_samples = np.array(
[[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = list(classifier.predict(new_samples, as_iterable=True))
print('Predictions: {}'.format(str(y)))
参考
原文地址: http://www.datalearner.com/blog/1051488938031745