-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearClass.py
More file actions
83 lines (61 loc) · 2.86 KB
/
Copy pathlinearClass.py
File metadata and controls
83 lines (61 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
SPECIES = ['Setosa', 'Versicolor', 'Virginica']
# Lets define some constants to help us later on
train_path = tf.keras.utils.get_file(
"iris_training.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv")
test_path = tf.keras.utils.get_file(
"iris_test.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv")
train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)
test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
# Here we use keras (a module inside of TensorFlow) to grab our datasets and read them into a pandas dataframe
# Now we can pop the species column off and use that as our label.
train_y = train.pop('Species')
test_y = test.pop('Species')
#print(train.shape)
def input_fn(features, labels, training=True, batch_size=256):
# Convert the inputs to a Dataset
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
# Shuffle and repeat if in training mode
if training:
dataset = dataset.shuffle(1000).repeat()
return dataset.batch(batch_size)
my_feature_columns = []
for key in train.keys():
my_feature_columns.append(tf.feature_column.numeric_column(key=key))
#print(my_feature_columns)
# Build a DNN (deep neaural network) with 2 hidden layers with 30 and 10 hidden nodes in each
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
# 2 hidden layers of 30 and 10 nodes respectively
hidden_units=[30, 10],
# The model must choose between 3 classes
n_classes=3)
classifier.train(
input_fn=lambda: input_fn(train, train_y, training=True),
steps=5000)
# We include a lambda to avoid creating an inner function previously
eval_result = classifier.evaluate(
input_fn=lambda: input_fn(test, test_y, training=False))
print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
def input_fun(features, batch_size=256):
# Convert the inputs to a Dataset without labels.
return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)
features = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']
predict = {}
print("Please type numeric values as prompted.")
for feature in features:
valid = True
while valid:
val = input(feature + ": ")
if not val.isdigit(): valid = False
predict[feature] = [float(val)]
predictions = classifier.predict(input_fn=lambda: input_fun(predict))
for pred_dict in predictions:
class_id = pred_dict['class_ids'][0]
probability = pred_dict['probabilities'][class_id]
print('Prediction is "{}" ({:.1f}%)'.format(
SPECIES[class_id], 100 * probability))