#Josh Kabo 2019
#Flight Pricing Predictor

from __future__ import print_function
import math

from InputFunctions import single_feature_input_fn
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import os
import tensorflow as tf
#ingores all warnings and things to clean up the output
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
from tensorflow.python.data import Dataset
pd.__version__

airfare_report_dataframe = pd.read_csv("airfare_report.csv", sep=",")

def train_single_feature_linear_regressor(learning_rate, steps, batch_size, input_feature="nsmiles"):
  """Trains a linear regression model of one feature.
  
  Args:
    learning_rate: A `float`, the learning rate.
    steps: A non-zero `int`, the total number of training steps. A training step
      consists of a forward and backward pass using a single batch.
    batch_size: A non-zero `int`, the batch size.
    input_feature: A `string` specifying a column from `airfare_report`
      to use as input feature.
  """
  
  print("TRAINING SINGLE FEATURE LINEAR REGRESSOR")

  periods = 10
  steps_per_period = steps / periods

  my_feature = input_feature
  my_feature_data = airfare_report_dataframe[[my_feature]]
  my_label = "fare"
  targets = airfare_report_dataframe[my_label]

  # Create feature columns.
  feature_columns = [tf.feature_column.numeric_column(my_feature)]
  
  # Create input functions.
  training_input_fn = lambda: single_feature_input_fn(my_feature_data, targets, batch_size=batch_size)
  prediction_input_fn = lambda: single_feature_input_fn(my_feature_data, targets, num_epochs=1, shuffle=False)
  
  # Create a linear regressor object.
  my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)
  linear_regressor = tf.estimator.LinearRegressor(
      feature_columns=feature_columns,
      optimizer=my_optimizer
  )

  # Set up to plot the state of our model's line each period.
  plt.figure(figsize=(15, 6))
  plt.subplot(1, 2, 1)
  plt.title("Learned Line by Period")
  plt.ylabel(my_label)
  plt.xlabel(my_feature)
  sample = airfare_report_dataframe.sample(n=300)
  plt.scatter(sample[my_feature], sample[my_label])
  colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]

  plt.show()

  # Train the model, but do so inside a loop so that we can periodically assess
  # loss metrics.
  print("Training model...")
  print("RMSE (on training data):")
  root_mean_squared_errors = []
  for period in range (0, periods):
    # Train the model, starting from the prior state.
    linear_regressor.train(
        input_fn=training_input_fn,
        steps=steps_per_period
    )
    # Take a break and compute predictions.
    predictions = linear_regressor.predict(input_fn=prediction_input_fn)
    predictions = np.array([item['predictions'][0] for item in predictions])
    
    # Compute loss.
    root_mean_squared_error = math.sqrt(
        metrics.mean_squared_error(predictions, targets))
    # Occasionally print the current loss.
    print("  period %02d : %0.2f" % (period, root_mean_squared_error))
    # Add the loss metrics from this period to our list.
    root_mean_squared_errors.append(root_mean_squared_error)
    
        # Finally, track the weights and biases over time.
    # Apply some math to ensure that the data and line are plotted neatly.
    y_extents = np.array([0, sample[my_label].max()])

  print("Model training finished.")

  # Output a graph of loss metrics over periods.
  plt.subplot(1, 2, 2)
  plt.ylabel('RMSE')
  plt.xlabel('Periods')
  plt.title("Root Mean Squared Error vs. Periods")
  plt.tight_layout()
  plt.plot(root_mean_squared_errors)

  plt.show()

  #I left out the calibration data graph

  print("Final RMSE (on training data): %0.2f" % root_mean_squared_error)
614c05afbe511b99857cb6588f6b30e97fec9985
<mask token>


def train_single_feature_linear_regressor(learning_rate, steps, batch_size,
    input_feature='nsmiles'):
    """Trains a linear regression model of one feature.
  
  Args:
    learning_rate: A `float`, the learning rate.
    steps: A non-zero `int`, the total number of training steps. A training step
      consists of a forward and backward pass using a single batch.
    batch_size: A non-zero `int`, the batch size.
    input_feature: A `string` specifying a column from `airfare_report`
      to use as input feature.
  """
    print('TRAINING SINGLE FEATURE LINEAR REGRESSOR')
    periods = 10
    steps_per_period = steps / periods
    my_feature = input_feature
    my_feature_data = airfare_report_dataframe[[my_feature]]
    my_label = 'fare'
    targets = airfare_report_dataframe[my_label]
    feature_columns = [tf.feature_column.numeric_column(my_feature)]
    training_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, batch_size=batch_size)
    prediction_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, num_epochs=1, shuffle=False)
    my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=
        learning_rate)
    my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer,
        5.0)
    linear_regressor = tf.estimator.LinearRegressor(feature_columns=
        feature_columns, optimizer=my_optimizer)
    plt.figure(figsize=(15, 6))
    plt.subplot(1, 2, 1)
    plt.title('Learned Line by Period')
    plt.ylabel(my_label)
    plt.xlabel(my_feature)
    sample = airfare_report_dataframe.sample(n=300)
    plt.scatter(sample[my_feature], sample[my_label])
    colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]
    plt.show()
    print('Training model...')
    print('RMSE (on training data):')
    root_mean_squared_errors = []
    for period in range(0, periods):
        linear_regressor.train(input_fn=training_input_fn, steps=
            steps_per_period)
        predictions = linear_regressor.predict(input_fn=prediction_input_fn)
        predictions = np.array([item['predictions'][0] for item in predictions]
            )
        root_mean_squared_error = math.sqrt(metrics.mean_squared_error(
            predictions, targets))
        print('  period %02d : %0.2f' % (period, root_mean_squared_error))
        root_mean_squared_errors.append(root_mean_squared_error)
        y_extents = np.array([0, sample[my_label].max()])
    print('Model training finished.')
    plt.subplot(1, 2, 2)
    plt.ylabel('RMSE')
    plt.xlabel('Periods')
    plt.title('Root Mean Squared Error vs. Periods')
    plt.tight_layout()
    plt.plot(root_mean_squared_errors)
    plt.show()
    print('Final RMSE (on training data): %0.2f' % root_mean_squared_error)

<mask token>
pd.__version__
<mask token>


def train_single_feature_linear_regressor(learning_rate, steps, batch_size,
    input_feature='nsmiles'):
    """Trains a linear regression model of one feature.
  
  Args:
    learning_rate: A `float`, the learning rate.
    steps: A non-zero `int`, the total number of training steps. A training step
      consists of a forward and backward pass using a single batch.
    batch_size: A non-zero `int`, the batch size.
    input_feature: A `string` specifying a column from `airfare_report`
      to use as input feature.
  """
    print('TRAINING SINGLE FEATURE LINEAR REGRESSOR')
    periods = 10
    steps_per_period = steps / periods
    my_feature = input_feature
    my_feature_data = airfare_report_dataframe[[my_feature]]
    my_label = 'fare'
    targets = airfare_report_dataframe[my_label]
    feature_columns = [tf.feature_column.numeric_column(my_feature)]
    training_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, batch_size=batch_size)
    prediction_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, num_epochs=1, shuffle=False)
    my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=
        learning_rate)
    my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer,
        5.0)
    linear_regressor = tf.estimator.LinearRegressor(feature_columns=
        feature_columns, optimizer=my_optimizer)
    plt.figure(figsize=(15, 6))
    plt.subplot(1, 2, 1)
    plt.title('Learned Line by Period')
    plt.ylabel(my_label)
    plt.xlabel(my_feature)
    sample = airfare_report_dataframe.sample(n=300)
    plt.scatter(sample[my_feature], sample[my_label])
    colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]
    plt.show()
    print('Training model...')
    print('RMSE (on training data):')
    root_mean_squared_errors = []
    for period in range(0, periods):
        linear_regressor.train(input_fn=training_input_fn, steps=
            steps_per_period)
        predictions = linear_regressor.predict(input_fn=prediction_input_fn)
        predictions = np.array([item['predictions'][0] for item in predictions]
            )
        root_mean_squared_error = math.sqrt(metrics.mean_squared_error(
            predictions, targets))
        print('  period %02d : %0.2f' % (period, root_mean_squared_error))
        root_mean_squared_errors.append(root_mean_squared_error)
        y_extents = np.array([0, sample[my_label].max()])
    print('Model training finished.')
    plt.subplot(1, 2, 2)
    plt.ylabel('RMSE')
    plt.xlabel('Periods')
    plt.title('Root Mean Squared Error vs. Periods')
    plt.tight_layout()
    plt.plot(root_mean_squared_errors)
    plt.show()
    print('Final RMSE (on training data): %0.2f' % root_mean_squared_error)

<mask token>
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
<mask token>
pd.__version__
airfare_report_dataframe = pd.read_csv('airfare_report.csv', sep=',')


def train_single_feature_linear_regressor(learning_rate, steps, batch_size,
    input_feature='nsmiles'):
    """Trains a linear regression model of one feature.
  
  Args:
    learning_rate: A `float`, the learning rate.
    steps: A non-zero `int`, the total number of training steps. A training step
      consists of a forward and backward pass using a single batch.
    batch_size: A non-zero `int`, the batch size.
    input_feature: A `string` specifying a column from `airfare_report`
      to use as input feature.
  """
    print('TRAINING SINGLE FEATURE LINEAR REGRESSOR')
    periods = 10
    steps_per_period = steps / periods
    my_feature = input_feature
    my_feature_data = airfare_report_dataframe[[my_feature]]
    my_label = 'fare'
    targets = airfare_report_dataframe[my_label]
    feature_columns = [tf.feature_column.numeric_column(my_feature)]
    training_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, batch_size=batch_size)
    prediction_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, num_epochs=1, shuffle=False)
    my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=
        learning_rate)
    my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer,
        5.0)
    linear_regressor = tf.estimator.LinearRegressor(feature_columns=
        feature_columns, optimizer=my_optimizer)
    plt.figure(figsize=(15, 6))
    plt.subplot(1, 2, 1)
    plt.title('Learned Line by Period')
    plt.ylabel(my_label)
    plt.xlabel(my_feature)
    sample = airfare_report_dataframe.sample(n=300)
    plt.scatter(sample[my_feature], sample[my_label])
    colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]
    plt.show()
    print('Training model...')
    print('RMSE (on training data):')
    root_mean_squared_errors = []
    for period in range(0, periods):
        linear_regressor.train(input_fn=training_input_fn, steps=
            steps_per_period)
        predictions = linear_regressor.predict(input_fn=prediction_input_fn)
        predictions = np.array([item['predictions'][0] for item in predictions]
            )
        root_mean_squared_error = math.sqrt(metrics.mean_squared_error(
            predictions, targets))
        print('  period %02d : %0.2f' % (period, root_mean_squared_error))
        root_mean_squared_errors.append(root_mean_squared_error)
        y_extents = np.array([0, sample[my_label].max()])
    print('Model training finished.')
    plt.subplot(1, 2, 2)
    plt.ylabel('RMSE')
    plt.xlabel('Periods')
    plt.title('Root Mean Squared Error vs. Periods')
    plt.tight_layout()
    plt.plot(root_mean_squared_errors)
    plt.show()
    print('Final RMSE (on training data): %0.2f' % root_mean_squared_error)

from __future__ import print_function
import math
from InputFunctions import single_feature_input_fn
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from tensorflow.python.data import Dataset
pd.__version__
airfare_report_dataframe = pd.read_csv('airfare_report.csv', sep=',')


def train_single_feature_linear_regressor(learning_rate, steps, batch_size,
    input_feature='nsmiles'):
    """Trains a linear regression model of one feature.
  
  Args:
    learning_rate: A `float`, the learning rate.
    steps: A non-zero `int`, the total number of training steps. A training step
      consists of a forward and backward pass using a single batch.
    batch_size: A non-zero `int`, the batch size.
    input_feature: A `string` specifying a column from `airfare_report`
      to use as input feature.
  """
    print('TRAINING SINGLE FEATURE LINEAR REGRESSOR')
    periods = 10
    steps_per_period = steps / periods
    my_feature = input_feature
    my_feature_data = airfare_report_dataframe[[my_feature]]
    my_label = 'fare'
    targets = airfare_report_dataframe[my_label]
    feature_columns = [tf.feature_column.numeric_column(my_feature)]
    training_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, batch_size=batch_size)
    prediction_input_fn = lambda : single_feature_input_fn(my_feature_data,
        targets, num_epochs=1, shuffle=False)
    my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=
        learning_rate)
    my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer,
        5.0)
    linear_regressor = tf.estimator.LinearRegressor(feature_columns=
        feature_columns, optimizer=my_optimizer)
    plt.figure(figsize=(15, 6))
    plt.subplot(1, 2, 1)
    plt.title('Learned Line by Period')
    plt.ylabel(my_label)
    plt.xlabel(my_feature)
    sample = airfare_report_dataframe.sample(n=300)
    plt.scatter(sample[my_feature], sample[my_label])
    colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]
    plt.show()
    print('Training model...')
    print('RMSE (on training data):')
    root_mean_squared_errors = []
    for period in range(0, periods):
        linear_regressor.train(input_fn=training_input_fn, steps=
            steps_per_period)
        predictions = linear_regressor.predict(input_fn=prediction_input_fn)
        predictions = np.array([item['predictions'][0] for item in predictions]
            )
        root_mean_squared_error = math.sqrt(metrics.mean_squared_error(
            predictions, targets))
        print('  period %02d : %0.2f' % (period, root_mean_squared_error))
        root_mean_squared_errors.append(root_mean_squared_error)
        y_extents = np.array([0, sample[my_label].max()])
    print('Model training finished.')
    plt.subplot(1, 2, 2)
    plt.ylabel('RMSE')
    plt.xlabel('Periods')
    plt.title('Root Mean Squared Error vs. Periods')
    plt.tight_layout()
    plt.plot(root_mean_squared_errors)
    plt.show()
    print('Final RMSE (on training data): %0.2f' % root_mean_squared_error)

#Josh Kabo 2019
#Flight Pricing Predictor

from __future__ import print_function
import math

from InputFunctions import single_feature_input_fn
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import os
import tensorflow as tf
#ingores all warnings and things to clean up the output
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
from tensorflow.python.data import Dataset
pd.__version__

airfare_report_dataframe = pd.read_csv("airfare_report.csv", sep=",")

def train_single_feature_linear_regressor(learning_rate, steps, batch_size, input_feature="nsmiles"):
  """Trains a linear regression model of one feature.
  
  Args:
    learning_rate: A `float`, the learning rate.
    steps: A non-zero `int`, the total number of training steps. A training step
      consists of a forward and backward pass using a single batch.
    batch_size: A non-zero `int`, the batch size.
    input_feature: A `string` specifying a column from `airfare_report`
      to use as input feature.
  """
  
  print("TRAINING SINGLE FEATURE LINEAR REGRESSOR")

  periods = 10
  steps_per_period = steps / periods

  my_feature = input_feature
  my_feature_data = airfare_report_dataframe[[my_feature]]
  my_label = "fare"
  targets = airfare_report_dataframe[my_label]

  # Create feature columns.
  feature_columns = [tf.feature_column.numeric_column(my_feature)]
  
  # Create input functions.
  training_input_fn = lambda: single_feature_input_fn(my_feature_data, targets, batch_size=batch_size)
  prediction_input_fn = lambda: single_feature_input_fn(my_feature_data, targets, num_epochs=1, shuffle=False)
  
  # Create a linear regressor object.
  my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)
  linear_regressor = tf.estimator.LinearRegressor(
      feature_columns=feature_columns,
      optimizer=my_optimizer
  )

  # Set up to plot the state of our model's line each period.
  plt.figure(figsize=(15, 6))
  plt.subplot(1, 2, 1)
  plt.title("Learned Line by Period")
  plt.ylabel(my_label)
  plt.xlabel(my_feature)
  sample = airfare_report_dataframe.sample(n=300)
  plt.scatter(sample[my_feature], sample[my_label])
  colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]

  plt.show()

  # Train the model, but do so inside a loop so that we can periodically assess
  # loss metrics.
  print("Training model...")
  print("RMSE (on training data):")
  root_mean_squared_errors = []
  for period in range (0, periods):
    # Train the model, starting from the prior state.
    linear_regressor.train(
        input_fn=training_input_fn,
        steps=steps_per_period
    )
    # Take a break and compute predictions.
    predictions = linear_regressor.predict(input_fn=prediction_input_fn)
    predictions = np.array([item['predictions'][0] for item in predictions])
    
    # Compute loss.
    root_mean_squared_error = math.sqrt(
        metrics.mean_squared_error(predictions, targets))
    # Occasionally print the current loss.
    print("  period %02d : %0.2f" % (period, root_mean_squared_error))
    # Add the loss metrics from this period to our list.
    root_mean_squared_errors.append(root_mean_squared_error)
    
        # Finally, track the weights and biases over time.
    # Apply some math to ensure that the data and line are plotted neatly.
    y_extents = np.array([0, sample[my_label].max()])

  print("Model training finished.")

  # Output a graph of loss metrics over periods.
  plt.subplot(1, 2, 2)
  plt.ylabel('RMSE')
  plt.xlabel('Periods')
  plt.title("Root Mean Squared Error vs. Periods")
  plt.tight_layout()
  plt.plot(root_mean_squared_errors)

  plt.show()

  #I left out the calibration data graph

  print("Final RMSE (on training data): %0.2f" % root_mean_squared_error)