<jupyter_start><jupyter_text>The Above cell should be the bench mark we try and beat<jupyter_code>features = list(champ2021.columns)
print(features)
df = pd.DataFrame(champ2021, columns = features)
df.head()
home_winList = []
away_winList = []
drawList = []
for index, row in champ2021.iterrows():
    home_win = row['home_win']
    away_win = row['away_win']
    draw = row['draw']
    new_home=home_win.replace('%','')
    new_away=away_win.replace('%','')
    new_draw=draw.replace('%','')
    home_winList.append(new_home)
    away_winList.append(new_away)
    drawList.append(new_draw)
    print(new_home)
champ2021['home_win'] = home_winList
champ2021['away_win'] = away_winList
champ2021['draw'] = drawList
home_EOL = df['home_EOL']
All_EOL = df[['home_EOL','away_EOL','home_win','away_win','draw','Home_Yellow', 'Home_Red', 'Away_Yellow', 'Away_Red',]]
result = df['difference']
fig, ax = plt.subplots(figsize=(16,6))
ax.scatter(All_EOL,result)
plt.show()
x = np.array(All_EOL)
y = np.array(result)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=4)
model = LinearRegression()
model.fit(X_train, y_train)
fig, ax = plt.subplots(figsize=(16,6))
ax.scatter(X_train, y_train)
ax.plot(X_train, model.predict(X_train), color='green')
plt.show()<jupyter_output><empty_output><jupyter_text>y = mx + b<jupyter_code>b = model.intercept_
m = model.coef_
print(b)
print(m)
#r squred value. the closer to one the better
model.score(X_train, y_train)
predictions = model.predict(X_test)
predictions<jupyter_output><empty_output><jupyter_text>Building our own regression model<jupyter_code>x = np.array(All_EOL)
y = np.array(result)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=4)
# y = Bx + a 
#Taking linear regression equations from https://en.wikipedia.org/wiki/Simple_linear_regression
B_hat = ((X_train - X_train.mean())*(y_train - y_train.mean())).sum() / ((X_train - X_train.mean()) ** 2).sum()
a_hat = y_train.mean() - B_hat * X_train.mean()
a_hat
def make_predictions(X_train, X_test, y_train, y_test):
    B_hat = ((X_train - X_train.mean())*(y_train - y_train.mean())).sum() / ((X_train - X_train.mean()) ** 2).sum()
    a_hat = y_train.mean() - B_hat * X_train.mean()
    #y = Bx + a
    return B_hat * X_test + a_hat
make_predictions(X_train, X_test, y_train, y_test)
#Creating r-squared. eqatuon taken from https://en.wikipedia.org/wiki/Simple_linear_regression
numerator = (X_train * y_train).mean() - X_train.mean() * y_train.mean()
denominator = np.sqrt(((X_train**2).mean() - X_train.mean()**2)*((y_train**2).mean() - y_train.mean()**2))
rxy = numerator / denominator
r_sqaured = rxy ** 2
r_sqaured<jupyter_output><empty_output><jupyter_text>From AICore portal:<jupyter_code>x = np.array(All_EOL)
y = np.array(result)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
X_test, X_validation, y_test, y_validation = train_test_split(
    X_test, y_test, test_size = 0.3
)
print("Number of samples in:")
print(f"  Training: {len(y_train)}")
print(f"  Validation: {len(y_validation)}")
print(f"  Testing: {len(y_test)}")
np.random.seed(2)

models = [
    DecisionTreeRegressor(splitter="random"),
    SVR(),
    LinearRegression()
]

for model in models:
    model.fit(X_train, y_train)
    
    y_train_pred = model.predict(X_train)
    y_validation_pred = model.predict(X_validation)
    y_test_pred = model.predict(X_test)
    
    train_loss = mean_squared_error(y_train, y_train_pred)
    validation_loss = mean_squared_error(y_validation, y_validation_pred)
    test_loss = mean_squared_error(y_test, y_test_pred)
    
    print(
        f"{model.__class__.__name__}: "
        f"Train Loss: {train_loss} | Validation Loss: {validation_loss} | "
        f"Test Loss: {test_loss}"
    
    )
def calculate_validation_loss(X_train, y_train, X_validation, y_validation):
    model = LinearRegression()

    # Without data leakage, train on train, validate on validation
    model.fit(X_train, y_train)
    y_validation_pred = model.predict(X_validation)
    validation_loss = mean_squared_error(y_validation, y_validation_pred)

    print(f"Validation loss: {validation_loss}")
    
# Without data leakage, train on train, validate on validation
calculate_validation_loss(X_train, y_train, X_validation, y_validation)

# With data leakage, 50 samples from validation added
fail_X_train = np.concatenate((X_train, X_validation[:50]))
fail_y_train = np.concatenate((y_train, y_validation[:50]))

calculate_validation_loss(fail_X_train, fail_y_train, X_validation, y_validation)<jupyter_output>Validation loss: 1.7485361462926046
Validation loss: 1.700120722302347
{'fraction_non_alphanumeric': 0.14163457424047926, 'fraction_numerical': 0.018613607188703467, 'mean_word_length': 5.5937940761636105, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 2, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '88548', 'license': 'MIT', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1775, 'n_words': 324, 'path': 'linear regression test Tamim.ipynb', 'repo_name': 'TamimEhsan2021/AiBetCore', 'size': '4674'}