# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.9.2 64-bit
#     metadata:
#       interpreter:
#         hash: 7d6993cb2f9ce9a59d5d7380609d9cb5192a9dedd2735a011418ad9e827eb538
#     name: python3
# ---

# + dotnet_interactive={"language": "python"}
# Assignment One
# Course: MIS 5150
# Professor: Dr. <NAME>

# + dotnet_interactive={"language": "python"}
import tensorflow as tf
import numpy as np

# + dotnet_interactive={"language": "python"}
# Question 1:
# 
import tensorflow as tf
import numpy as np

# a.
# Creating dataset
data_set = tf.data.Dataset.from_tensor_slices([[0, 1, 9, 8, 7, 3], [2, 9, 4, 0, 2, 3], [7, 3, 3, 2, 2, 1], [0, 0, 1, 2, 2, 5]])

# b.
# Iterating data
# method 1
for elements in data_set:
  print(elements)
print('')
# method 2
for elements in data_set:
  print(elements.numpy())
print('')
# method 3
iterator = iter(data_set)
print(iterator.get_next())
print(iterator.get_next())
print(iterator.get_next())
print(iterator.get_next())
print('')
# method 4
for data in data_set:
    print (data.numpy())
print('')

# c.
# display 3rd element from 2nd tensor
# method 1
ls = []
for item in data_set:
  e = item.numpy()
  ls.append(e)
print('3rd element from 2nd tensor:',ls[1][2])
print('')
# method 2
np_arr = np.asarray(ls, dtype=np.float32)
print('3rd element from 2nd tensor:',int(np_arr[1][2]))
print('')
# method 3
display_element = np.array([[0, 1, 9, 8, 7, 3], [2, 9, 4, 0, 2, 3], [7, 3, 3, 2, 2, 1], [0, 0, 1, 2, 2, 5]])
print('3rd element from 2nd tensor:', display_element[1][2])

# + dotnet_interactive={"language": "python"}
# Question 2:
# 
import tensorflow as tf
# GPU Enable
tf.__version__, tf.test.gpu_device_name()


# output ('2.4.1', '/device:GPU:0')

# + dotnet_interactive={"language": "python"}
# Question 3:
# 
import tensorflow as tf
import pandas as pd
from tensorflow import keras

ds = 'flag.data'
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/flags/flag.data'
dataset_path = tf.keras.utils.get_file(ds, url)
# downloaded the data using keras
dataset_path

# cols from the flag.names file
cols = ['name','landmass','zone','area','population','language','religion','bars','stripes','colours','red','green','blue','gold','white','black','orange','mainhue','circles','crosses','saltires','quarters','sunstars','crescent','triangle','icon','animate','text','topleft','botright']

# raw dataset using pandas
raw_dataset = pd.read_csv(dataset_path, names=cols)

# displaying dataset
raw_dataset.head()

# remove non numerical values
raw_dataset.pop('name')
raw_dataset.pop('mainhue')
raw_dataset.pop('topleft')
raw_dataset.pop('botright')
# print to check removal of non-numerical values
raw_dataset.head()

# step 2
dataset = tf.data.Dataset.from_tensor_slices(raw_dataset.values)

# range for dataset
dataset = tf.data.Dataset.range(10)

# using take function
for i in dataset.take(3):
  print(i)
print('')

# + dotnet_interactive={"language": "python"}
# Question 4:
# 
import tensorflow as tf

# a. create a tensor
dataset = tf.data.Dataset.range(10)

# b. display the tendor
for row in dataset:
  print(row)
print('')

# c. repeat and batch
data_batch = dataset.repeat(5).batch(4)

# d. display batched sensor
for row in data_batch:
  print(row)
print('')

# e. map to cube the dataset
data_map = data_batch.map(lambda x:x ** 3)

# f. batched tensor display
for item in data_map.take(2):
  print(item)
print('')

# + dotnet_interactive={"language": "python"}
# Question 5:
# 
import tensorflow as tf

# a. create dataset
dataset = tf.data.Dataset.range(10).repeat(5)
print ('dataset has', len(list(dataset)), 'elements')
print('')

# b. dataset buffer and display
ds = dataset.shuffle(buffer_size=5).batch(7)

for item in ds:
  print(item)
print('')

StarcoderdataJupyterScriptsDedupFiltered