用深度神经网络对Iris数据集进行分类的程序--tensorflow
2017-05-08 15:58
267 查看
先确保你已经安装了tensorflow…
运行结果:
# 引入必要的module from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import urllib import numpy as np import tensorflow as tf # Data sets 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 the training and test sets aren't stored locally, download them. if not os.path.exists(IRIS_TRAINING): raw = urllib.urlopen(IRIS_TRAINING_URL).read() with open(IRIS_TRAINING, "w") as f: f.write(raw) if not os.path.exists(IRIS_TEST): raw = urllib.urlopen(IRIS_TEST_URL).read() with open(IRIS_TEST, "w") as f: f.write(raw) # Load datasets. 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) # Specify that all features have real-value data feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)] # Build 3 layer DNN with 10, 20, 10 units respectively. classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,hidden_units=[10, 20, 10],n_classes=3,model_dir="/tmp/iris_model") # Define the training inputs def get_train_inputs(): x = tf.constant(training_set.data) y = tf.constant(training_set.target) return x, y # Fit model. classifier.fit(input_fn=get_train_inputs, steps=2000) # Define the test inputs def get_test_inputs(): x = tf.constant(test_set.data) y = tf.constant(test_set.target) return x, y # Evaluate accuracy. accuracy_score = classifier.evaluate(input_fn=get_test_inputs,steps=1)["accuracy"] print("\nTest Accuracy: {0:f}\n".format(accuracy_score)) # Classify two new flower samples. def new_samples(): return np.array( [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=np.float32) predictions = list(classifier.predict(input_fn=new_samples)) print( "New Samples, Class Predictions: {}\n" .format(predictions)) if __name__ == "__main__": main()
运行结果:
Test Accuracy: 0.966667 New Samples, Class Predictions: [1, 1]
相关文章推荐
- Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_训练模型
- 机器学习逻辑回归:使用C++语言手工编写程序对a1a数据集进行Logistic分类
- 数据挖掘-oneR算法-Iris数据集分析-使用oneR算法进行分类预测(五)
- 数据挖掘-K-近邻分类器-Iris数据集分析-使用K-近邻分类器进行分类预测(四)
- Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_利用训练好的模型进行分类
- c#神经网络,实现对Iris数据集进行分类
- tensorflow下对MNIST数据集进行识别的程序代码
- tensorflow 学习专栏(四):使用tensorflow在mnist数据集上使用逻辑回归logistic Regression进行分类
- iris数据集进行KNN分类
- 使用不同的SVM对iris数据集进行分类并绘出结果
- [Java][机器学习]用决策树分类算法对Iris花数据集进行处理
- 使用sklearn中的Iris植物分类数据集进行特征检验与预测分析
- 用深度神经网络对boston housing data进行回归预测的程序--tensorflow
- RBF神经网络对iris鸢尾花数据集进行分类识别
- 利用manifest文件对程序目录下的dll进行分类
- 数据挖掘-K-近邻分类器-Iris数据集分析-PCA降维处理后显示分类情况(三)
- 运用BP反向传播算法进行Iris鸢尾花分类
- 汐月教育之理解TensorFlow(三.1)构建简单的BP神经网络+softmax多分类进行MNIST识别
- Nearest Neighbor算法对Cifar-10数据集进行分类
- tensorflow-示例2(MNIST集合上进行分类深度卷积网DCNN)