<filename>Machine Learning with Python/Code Learning Algorithms (A, B, C, D, E)/example-project.py
#---README---

# I decided to have a pure Python file in this folder for the example Titanic Project
# So that you can look at the code all in one place
# I would highly recommend using the Jupyter Notebook, as it allows for better useage, and I've got example outputs embedded in it

#All of this was pulled from: https://www.tensorflow.org/tutorials/estimator/linear

#I'll put in a few notes in here as well.

#---Start of Code---

#Importing the required libraries;

import numpy as np #For manipulation of datasets, and general tools
import pandas as pd #For neater and more efficient dataset usage
import matplotlib.pyplot as plt #for plotting
from IPython.display import clear_output #For some better and clearer output
from six.moves import urllib #For potential backwards compatibility with certain modules

#Importing TenserFlow

import tensorflow.compat.v2.feature_column as fc

import tensorflow as tf

# Loading the dataset.
dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv')
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv')

y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')



#---Machine Learning Model---


#Seperating columns, and creating 'Feature' Columns:
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck',
                       'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']

feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
  vocabulary = dftrain[feature_name].unique()
  feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))


#Making an input function:
def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
  def input_function():
    ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))
    if shuffle:
      ds = ds.shuffle(1000)
    ds = ds.batch(batch_size).repeat(num_epochs)
    return ds
  return input_function

train_input_fn = make_input_fn(dftrain, y_train)
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)

# Inspecting the dataset:
ds = make_input_fn(dftrain, y_train, batch_size=10)()
for feature_batch, label_batch in ds.take(1):
  print('Some feature keys:', list(feature_batch.keys()))
  print()
  print('A batch of class:', feature_batch['class'].numpy())
  print()
  print('A batch of Labels:', label_batch.numpy())

# Inspecting a specific feature column:
age_column = feature_columns[7]
tf.keras.layers.DenseFeatures([age_column])(feature_batch).numpy()

gender_column = feature_columns[0]
tf.keras.layers.DenseFeatures([tf.feature_column.indicator_column(gender_column)])(feature_batch).numpy()

#---Training the Model---

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

clear_output()
print(result)

age_x_gender = tf.feature_column.crossed_column(['age', 'sex'], hash_bucket_size=100)
derived_feature_columns = [age_x_gender]
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns+derived_feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

clear_output()
print(result)

pred_dicts = list(linear_est.predict(eval_input_fn))
probs = pd.Series([pred['probabilities'][1] for pred in pred_dicts])

probs.plot(kind='hist', bins=20, title='predicted probabilities')

from sklearn.metrics import roc_curve
from matplotlib import pyplot as plt

fpr, tpr, _ = roc_curve(y_eval, probs)
plt.plot(fpr, tpr)
plt.title('ROC curve')
plt.xlabel('false positive rate')
plt.ylabel('true positive rate')
plt.xlim(0,)
plt.ylim(0,)
<filename>Machine Learning with Python/Code Learning Algorithms (A, B, C, D, E)/example-project.py
#---README---

# I decided to have a pure Python file in this folder for the example Titanic Project
# So that you can look at the code all in one place
# I would highly recommend using the Jupyter Notebook, as it allows for better useage, and I've got example outputs embedded in it

#All of this was pulled from: https://www.tensorflow.org/tutorials/estimator/linear

#I'll put in a few notes in here as well.

#---Start of Code---

#Importing the required libraries;

import numpy as np #For manipulation of datasets, and general tools
import pandas as pd #For neater and more efficient dataset usage
import matplotlib.pyplot as plt #for plotting
from IPython.display import clear_output #For some better and clearer output
from six.moves import urllib #For potential backwards compatibility with certain modules

#Importing TenserFlow

import tensorflow.compat.v2.feature_column as fc

import tensorflow as tf

# Loading the dataset.
dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv')
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv')

y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')



#---Machine Learning Model---


#Seperating columns, and creating 'Feature' Columns:
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck',
                       'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']

feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
  vocabulary = dftrain[feature_name].unique()
  feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))


#Making an input function:
def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
  def input_function():
    ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))
    if shuffle:
      ds = ds.shuffle(1000)
    ds = ds.batch(batch_size).repeat(num_epochs)
    return ds
  return input_function

train_input_fn = make_input_fn(dftrain, y_train)
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)

# Inspecting the dataset:
ds = make_input_fn(dftrain, y_train, batch_size=10)()
for feature_batch, label_batch in ds.take(1):
  print('Some feature keys:', list(feature_batch.keys()))
  print()
  print('A batch of class:', feature_batch['class'].numpy())
  print()
  print('A batch of Labels:', label_batch.numpy())

# Inspecting a specific feature column:
age_column = feature_columns[7]
tf.keras.layers.DenseFeatures([age_column])(feature_batch).numpy()

gender_column = feature_columns[0]
tf.keras.layers.DenseFeatures([tf.feature_column.indicator_column(gender_column)])(feature_batch).numpy()

#---Training the Model---

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

clear_output()
print(result)

age_x_gender = tf.feature_column.crossed_column(['age', 'sex'], hash_bucket_size=100)
derived_feature_columns = [age_x_gender]
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns+derived_feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

clear_output()
print(result)

pred_dicts = list(linear_est.predict(eval_input_fn))
probs = pd.Series([pred['probabilities'][1] for pred in pred_dicts])

probs.plot(kind='hist', bins=20, title='predicted probabilities')

from sklearn.metrics import roc_curve
from matplotlib import pyplot as plt

fpr, tpr, _ = roc_curve(y_eval, probs)
plt.plot(fpr, tpr)
plt.title('ROC curve')
plt.xlabel('false positive rate')
plt.ylabel('true positive rate')
plt.xlim(0,)
plt.ylim(0,)
#---README--- # I decided to have a pure Python file in this folder for the example Titanic Project # So that you can look at the code all in one place # I would highly recommend using the Jupyter Notebook, as it allows for better useage, and I've got example outputs embedded in it #All of this was pulled from: https://www.tensorflow.org/tutorials/estimator/linear #I'll put in a few notes in here as well. #---Start of Code--- #Importing the required libraries; #For manipulation of datasets, and general tools #For neater and more efficient dataset usage #for plotting #For some better and clearer output #For potential backwards compatibility with certain modules #Importing TenserFlow # Loading the dataset. #---Machine Learning Model--- #Seperating columns, and creating 'Feature' Columns: #Making an input function: # Inspecting the dataset: # Inspecting a specific feature column: #---Training the Model---