<jupyter_start><jupyter_text>Hybrid Neural Net to solve Regression Problem We use a neural net with a quantum layer to predict the second half betting lines given the result of the first half and the opening line. The quantum layer is an 8 qubit layer and the model is from Keras.<jupyter_code>import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import pennylane as qml
import warnings
warnings.filterwarnings('ignore')
tf.keras.backend.set_floatx('float64')

import warnings
warnings.filterwarnings('ignore')
###predict 2nd half line using 1st half total and open ##

df1 = pd.read_csv("nfl_odds.csv")
df1['1H'] = df1['1st'] + df1['2nd']

df2 = pd.read_csv('bet.csv')
df = df1.merge(df2, left_on = 'Team', right_on = 'Tm')

df = df[['1H','Open', 'TO%','PF','Yds','ML', '2H']]
df.head()
n_qubits = 8
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(inputs, weights):
    qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
    qml.templates.BasicEntanglerLayers(weights, wires=range(n_qubits))
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

n_layers = 4
weight_shapes = {"weights": (n_layers, n_qubits)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
clayer_1 = tf.keras.layers.Dense(8,  activation="relu")
clayer_2 = tf.keras.layers.Dense(2, activation="relu")
model = tf.keras.models.Sequential([clayer_1, qlayer, clayer_2])
opt = tf.keras.optimizers.SGD(learning_rate=0.2)
model.compile(opt, loss="mae", metrics=["mean_absolute_error"])
df = df[df.Open != 'pk']
df = df[df['2H'] != 'pk']
df['Open'] = df['Open'].astype(float)
df['2H'] = df['2H'].astype(float)
X = df[['1H','Open','TO%','PF','Yds','ML']]
y = df['2H']

X = np.asarray(X).astype(np.float32)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=0)

scaler = MinMaxScaler(feature_range = (0,1))
scaler.fit(X_train)
X_train = scaler.transform(X_train)
fitting = model.fit(X_train, y_train, epochs=10, batch_size=5, validation_split=0.15, verbose=2)
X_test = scaler.transform(X_test)
preds = model.predict(X_test)
pred = pd.DataFrame(preds, columns =[ 'prediction1', 'prediction2'])
pred = pred[(pred.prediction1 > 0) & (pred.prediction1 < 30)]
y_test = y_test.reset_index()
y_test = y_test[y_test['2H'] > 6]

compare = pd.concat([pred, y_test], axis=1)
compare = compare.drop('index', axis=1)
compare.dropna()<jupyter_output><empty_output><jupyter_text>Classical NN (Benchmarking) The MAE is twice as large for the purely classical NN. The quantum layer is helping the solution converge more quickly! (As an aside, the quantum NN takes alot longer to run)<jupyter_code>clayer_1 = tf.keras.layers.Dense(8,  activation="relu")
clayer_2 = tf.keras.layers.Dense(2, activation="relu")
model = tf.keras.models.Sequential([clayer_1, clayer_2])
opt = tf.keras.optimizers.SGD(learning_rate=0.2)
model.compile(opt, loss="mae", metrics=["mean_absolute_error"])
df = df[df.Open != 'pk']
df = df[df['2H'] != 'pk']
df['Open'] = df['Open'].astype(float)
df['2H'] = df['2H'].astype(float)
X = df[['1H','Open','TO%','PF','Yds','ML']]
y = df['2H']

X = np.asarray(X).astype(np.float32)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=0)

scaler = MinMaxScaler(feature_range = (0,1))
scaler.fit(X_train)
X_train = scaler.transform(X_train)
fitting = model.fit(X_train, y_train, epochs=15, batch_size=10, validation_split=0.15, verbose=2)<jupyter_output>Epoch 1/15
37/37 - 1s - loss: 10.3219 - mean_absolute_error: 10.3219 - val_loss: 9.8135 - val_mean_absolute_error: 9.8135
Epoch 2/15
37/37 - 0s - loss: 10.2575 - mean_absolute_error: 10.2575 - val_loss: 9.8118 - val_mean_absolute_error: 9.8118
Epoch 3/15
37/37 - 0s - loss: 10.2742 - mean_absolute_error: 10.2742 - val_loss: 9.8250 - val_mean_absolute_error: 9.8250
Epoch 4/15
37/37 - 0s - loss: 10.2460 - mean_absolute_error: 10.2460 - val_loss: 10.3656 - val_mean_absolute_error: 10.3656
Epoch 5/15
37/37 - 0s - loss: 10.1914 - mean_absolute_error: 10.1914 - val_loss: 9.8487 - val_mean_absolute_error: 9.8487
Epoch 6/15
37/37 - 0s - loss: 10.2714 - mean_absolute_error: 10.2714 - val_loss: 10.2363 - val_mean_absolute_error: 10.2363
Epoch 7/15
37/37 - 0s - loss: 10.3317 - mean_absolute_error: 10.3317 - val_loss: 10.0592 - val_mean_absolute_error: 10.0592
Epoch 8/15
37/37 - 0s - loss: 10.2152 - mean_absolute_error: 10.2152 - val_loss: 9.8159 - val_mean_absolute_error: 9.8159
Epoch 9/15
37/37 -[...]
{'fraction_non_alphanumeric': 0.15591515416575552, 'fraction_numerical': 0.07434944237918216, 'mean_word_length': 5.469589816124469, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '2396', 'license': 'MIT', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1820, 'n_words': 396, 'path': 'Loo Boys/PythonNotebooks/betting.ipynb', 'repo_name': 'ushnishray/Hackathon2021', 'size': '4573'}