<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Model Versioning Design Pattern
In the Model Versioning design pattern, backward compatibility is achieved by deploying a changed model as a microservice with a different REST endpoint. This is a necessary prerequisite for many of the other patterns discussed in this chapter.
Step1:  Download and preprocess data
You'll need to authenticate to your Google Cloud to run the BigQuery query below.
Step2:  In the following cell, replace your-cloud-project with the name of your GCP project.
Step3:  Model version #1
Step4:  Deploying classification model to AI Platform
Replace your-cloud-project below with the name of your cloud project.
Step5:  Model version #2
Step6:  Note that accuracy will be similar to the XGBoost model. We're just using this to demonstrate how training a model with a different framework could be deployed as a new version.
Step7:  Next we'll deploy the updated TF model to AI Platform as a v2.
Step8:  Alternative
<ASSISTANT_TASK:>
Python Code:
import json
import numpy as np
import pandas as pd
import xgboost as xgb
import tensorflow as tf
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from google.cloud import bigquery
Explanation: Model Versioning Design Pattern
In the Model Versioning design pattern, backward compatibility is achieved by deploying a changed model as a microservice with a different REST endpoint. This is a necessary prerequisite for many of the other patterns discussed in this chapter.
End of explanation
from google.colab import auth
auth.authenticate_user()
Explanation: Download and preprocess data
You'll need to authenticate to your Google Cloud to run the BigQuery query below.
End of explanation
# Note: this query may take a few minutes to run
%%bigquery df --project your-cloud-project
SELECT
  arr_delay,
  carrier,
  origin,
  dest,
  dep_delay,
  taxi_out,
  distance
FROM
  `cloud-training-demos.flights.tzcorr`
WHERE
  extract(year from fl_date) = 2015
ORDER BY fl_date ASC
LIMIT 300000
df = df.dropna()
df = shuffle(df, random_state=2)
df.head()
# Only include origins and destinations that occur frequently in the dataset
df = df[df['origin'].map(df['origin'].value_counts()) > 500]
df = df[df['dest'].map(df['dest'].value_counts()) > 500]
df = pd.get_dummies(df, columns=['carrier', 'origin', 'dest'])
Explanation: In the following cell, replace your-cloud-project with the name of your GCP project.
End of explanation
# Create a boolean column to indicate whether flight was > 30 mins delayed
df.loc[df['arr_delay'] >= 30, 'arr_delay_bool'] = 1
df.loc[df['arr_delay'] < 30, 'arr_delay_bool'] = 0
df['arr_delay_bool'].value_counts()
classify_model_labels = df['arr_delay_bool']
classify_model_data = df.drop(columns=['arr_delay', 'arr_delay_bool'])
x,y = classify_model_data,classify_model_labels
x_train,x_test,y_train,y_test = train_test_split(x,y)
model = xgb.XGBRegressor(
    objective='reg:logistic'
)
# Given the dataset size, this may take 1-2 minutes to run
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
acc = accuracy_score(y_test, np.round(y_pred))
print(acc)
# Save the model
model.save_model('model.bst')
Explanation: Model version #1: predict whether or not the flight is > 30 min delayed
End of explanation
# Set your cloud project
PROJECT = 'your-cloud-project'
!gcloud config set project $PROJECT
BUCKET = PROJECT + '_flight_model_bucket'
# Create a bucket if you don't have one
# You only need to run this once
!gsutil mb gs://$BUCKET
!gsutil cp 'model.bst' gs://$BUCKET
# Create the model resource
!gcloud ai-platform models create flight_delay_prediction
# Create the version
!gcloud ai-platform versions create 'v1' \
  --model 'flight_delay_prediction' \
  --origin gs://$BUCKET \
  --runtime-version=1.15 \
  --framework 'XGBOOST' \
  --python-version=3.7
# Get a prediction on the first example from our test set
!rm input.json
num_examples = 10
with open('input.json', 'a') as f:
  for i in range(num_examples):
    f.write(str(x_test.iloc[i].values.tolist()))
    f.write('\n')
!cat input.json
# Make a prediction to the deployed model
!gcloud ai-platform predict --model 'flight_delay_prediction' --version \
  'v1' --json-instances 'input.json'
# Compare this with actual values
print(y_test.iloc[:5])
Explanation: Deploying classification model to AI Platform
Replace your-cloud-project below with the name of your cloud project.
End of explanation
tf_model = tf.keras.Sequential([
    tf.keras.layers.Dense(32, activation='relu', input_shape=[len(x_train.iloc[0])]),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')                       
])
tf_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
tf_model.fit(x_train, y_train, epochs=10, validation_split=0.1)
Explanation: Model version #2: replace XGBoost with TensorFlow
End of explanation
metrics = tf_model.evaluate(x_test, y_test)
print(metrics)
Explanation: Note that accuracy will be similar to the XGBoost model. We're just using this to demonstrate how training a model with a different framework could be deployed as a new version.
End of explanation
tf_model_path = 'gs://' + BUCKET + '/tf'
tf_model.save(tf_model_path, save_format='tf')
!gcloud ai-platform versions create 'v2' \
  --model 'flight_delay_prediction' \
  --origin $tf_model_path \
  --runtime-version=2.1 \
  --framework 'TENSORFLOW' \
  --python-version=3.7
# Make a prediction to the new version
!gcloud ai-platform predict --model 'flight_delay_prediction' --version \
  'v2' --json-instances 'input.json'
Explanation: Next we'll deploy the updated TF model to AI Platform as a v2.
End of explanation
regression_model_labels = df['arr_delay']
regression_model_data = df.drop(columns=['arr_delay', 'arr_delay_bool'])
x,y = regression_model_data,regression_model_labels
x_train,x_test,y_train,y_test = train_test_split(x,y)
model = xgb.XGBRegressor(
    objective='reg:linear'
)
# This will take 1-2 minutes to run
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
for i,val in enumerate(y_pred[:10]):
  print(val)
  print(y_test.iloc[i])
  print()
model.save_model('model.bst')
!gsutil cp model.bst gs://$BUCKET/regression/
!gcloud ai-platform models create 'flights_regression'
# Create the version
!gcloud ai-platform versions create 'v1' \
  --model 'flights_regression' \
  --origin gs://$BUCKET/regression \
  --runtime-version=1.15 \
  --framework 'XGBOOST' \
  --python-version=3.7
!gcloud ai-platform predict --model 'flighs_regression' --version \
  'v1' --json-instances 'input.json'
Explanation: Alternative: reframe as a regression problem
In this case, you'd likely want to create a new model resource since the response format of your model has changed.
End of explanation
<END_TASK>