<jupyter_start><jupyter_text><jupyter_code>%tensorflow_version 2.x
from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
import pandas as pd<jupyter_output><empty_output><jupyter_text>Get the Data<jupyter_code>columns = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
species = ['Setosa', 'Versicolor', 'Virginica']
# This cell get the path of file, next we will be needing to get the data from the file
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_training.csv', 'https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv')
train = pd.read_csv(train_path, names=columns, header=0)
test = pd.read_csv(test_path, names=columns, header=0)
train.head()
y_train = train.pop('Species')
y_test = test.pop('Species')
train.shape, test.shape
def input_fn(features, labels, training=True, batch_size=256):
  dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

  if training:
    dataset = dataset.shuffle(1000).repeat()
  return dataset.batch(batch_size)
feature_columns = []
for key in train:
  feature_columns.append(tf.feature_column.numeric_column(key))
feature_columns<jupyter_output><empty_output><jupyter_text>Build the modelWe are going to use pre-built TF classification model:*   DNNClassifier (Deep Neural Network)*   LinearClassifier<jupyter_code>classifier = tf.estimator.DNNClassifier(
    feature_columns=feature_columns,
    hidden_units=[30, 10],
    n_classes=3
)
classifier.train(
    input_fn = lambda: input_fn(train, y_train, training=True),
    steps=5000
)
results = classifier.evaluate(
    input_fn = lambda: input_fn(test, y_test, training=False)
)
print('Test Set Accuracy: ', {results['accuracy']})
def input_fn(features, batch_size=256):
  return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)


features = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']
predict = {}

print('Type Numeric Values')
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_fn(predict)
    )
predictions
for pred_dict in predictions:
  print(pred_dict)
  class_id = pred_dict['class_ids'][0]
  probability = pred_dict['probabilities'][class_id]
print(f'Flower is a {species[class_id]} with a probability of {round(probability*100, 2)}%')<jupyter_output>Flower is a Versicolor with a probability of 39.76%
{'fraction_non_alphanumeric': 0.12546957175056347, 'fraction_numerical': 0.011645379413974455, 'mean_word_length': 6.256130790190736, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '406987', 'license': 'MIT', 'n_tokens_mistral': 922, 'n_tokens_neox': 886, 'n_words': 191, 'path': 'Classification_with_TF_and_IRIS_Dataset.ipynb', 'repo_name': 'EnricoHuber/TensorFlow-Review', 'size': '2662'}