Skip to content

Instantly share code, notes, and snippets.

@yunho0130
Created October 9, 2016 01:31
Show Gist options
  • Save yunho0130/fb9f6ea863408b6ff45212bc1cee6ae3 to your computer and use it in GitHub Desktop.
Save yunho0130/fb9f6ea863408b6ff45212bc1cee6ae3 to your computer and use it in GitHub Desktop.
import tensorflow as tf
import numpy as np
# 데이터셋
IRIS_TRAINING = "/Users/Yunho/Desktop/iris_data/iris_char_training.csv"
IRIS_TEST = "/Users/Yunho/Desktop/iris_data/iris_char_test.csv"
# 데이터셋을 불러옵니다.
#load_csv_with_header(
data_path,
target_dtype=np.float,
features_dtype=np.float)
def load_csv_without_header(filename,
target_dtype,
features_dtype,
target_column=-1):
training_set = tf.contrib.learn.datasets.base.load_csv_without_header(filename=IRIS_TRAINING, target_dtype=np.int, features_dtype=np.long)
test_set = tf.contrib.learn.datasets.base.load_csv(filename=IRIS_TEST, target_dtype=np.int)
x_train, x_test, y_train, y_test = training_set.data, test_set.data, \
training_set.target, test_set.target
# 10-20-10의 구조를 갖는 3층 DNN를 만듭니다
classifier = tf.contrib.learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3)
# 모델을 피팅합니다.
classifier.fit(x=x_train, y=y_train, steps=200)
# 정확도를 평가합니다.
accuracy_score = classifier.evaluate(x=x_test, y=y_test)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))
# 새로운 두 꽃의 표본을 분류합니다.
new_samples = np.array(
[[6.4, 3.2, 4(new_samples)
print ('Predictions: {}'.format(str(y))).5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = classifier.predict
# Tensorflow 최신화
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
# Data sets
IRIS_TRAINING = "/development/bigcon/tensor_train.csv"
IRIS_TEST = "/development/bigcon/tensor_test.csv"
IRIS_NEW_SAMPLE = "/development/bigcon/tensor_evaluation.csv"
IRIS_RESULT = "/development/bigcon/tensor_result.txt"
# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_without_header(
filename=IRIS_TRAINING,
target_dtype=np.float32,
features_dtype=np.int32,
target_column=0)
test_set = tf.contrib.learn.datasets.base.load_csv_without_header(
filename=IRIS_TEST,
target_dtype=np.float32,
features_dtype=np.int32,
target_column=0)
# Specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=49)]
# 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=2,
model_dir="/tmp/tensor_model2")
# Fit model.
classifier.fit(x=training_set.data,
y=training_set.target,
steps=2000)
# Evaluate accuracy.
accuracy_score = classifier.evaluate(x=test_set.data,
y=test_set.target)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))
# 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)
array1 = [3,42,3,1,42,3,30,20,20,2,89800,0,1,20,1,4,0,935,6,6,0,0,0,0,3159,0,3159,1,1686,12,60,20,1,1,1,3,4,2,4,87,133,40,0,612,0,1,0,0,0]
new_samples = np.array(array1, dtype=int)
y = classifier.predict(new_samples)
print('Predictions: {}'.format(str(y)))
#from numpy import genfromtxt
#new_samples = np.array(genfromtxt(IRIS_NEW_SAMPLE, delimiter=',',dtype=int))
# y = classifier.predict(new_samples)
# 출력
f = open(IRIS_RESULT, 'w')
f.write(y)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment