<jupyter_start><jupyter_text>Final Project NotebookThe report for this final project can be found at this [link](https://cybertraining-dsc.github.io/report/fa20-523-301/project/project/).<jupyter_code>### Migrated to Github. No longer need to use this.

# from google.colab import drive
# drive.mount('/content/drive')<jupyter_output><empty_output><jupyter_text>Part 1 Importing the functionsThis file requires that we import Numpy, Matplotlib, Pylab, Keras, and Pandas<jupyter_code>import numpy as np
from numpy import mean
from numpy import std

from IPython.display import display
from PIL import Image
import matplotlib.pyplot as plt
import pylab
import os, sys
import pandas as pd
import io
import requests
import warnings
import sklearn
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from datetime import datetime
from math import sqrt

from sklearn import datasets, linear_model
from sklearn.model_selection import cross_val_score, KFold
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor

from lightgbm import LGBMRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold

from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_regression
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split

from matplotlib import pyplot

from sklearn.datasets import make_regression

! pip install cloudmesh-common -U

from cloudmesh.common.Benchmark import Benchmark

! pip install utils
! pip install lightgbm
import utils
import lightgbm

from IPython.display import Image
from IPython.core.display import HTML 

import time

### NEW ###
import seaborn as sns
from scipy.stats import zscore
### NEW ###<jupyter_output>Requirement already up-to-date: cloudmesh-common in /usr/local/lib/python3.6/dist-packages (4.3.26)
Requirement already satisfied, skipping upgrade: colorama in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (0.4.4)
Requirement already satisfied, skipping upgrade: pathlib in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (1.0.1)
Requirement already satisfied, skipping upgrade: python-dateutil in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (2.8.1)
Requirement already satisfied, skipping upgrade: humanize in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (0.5.1)
Requirement already satisfied, skipping upgrade: requests in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (2.23.0)
Requirement already satisfied, skipping upgrade: oyaml in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (1.0)
Requirement already satisfied, skipping upgrade: python-hostlist in /usr/local/lib/python3.6/dist-packages[...]<jupyter_text>Part 1.1 Setting up the accuracy metrics<jupyter_code># Setting up testing metrics. Decided not to use, but these could be if the built in functions did not work correctly
# First is Mean Absolute Error

def mae_metric(actual, predicted):
	sum_error = 0.0
	for i in range(len(actual)):
		sum_error += abs(predicted[i] - actual[i])
	return sum_error / float(len(actual))
 
# Calculate Root Mean Squared Error

def rmse_metric(actual, predicted):
	sum_error = 0.0
	for i in range(len(actual)):
		prediction_error = predicted[i] - actual[i]
		sum_error += (prediction_error ** 2)
	mean_error = sum_error / float(len(actual))
	return sqrt(mean_error)

# Calculate accuracy percentage between two lists

def accuracy_metric(actual, predicted):
	correct = 0
	for i in range(len(actual)):
		if actual[i] == predicted[i]:
			correct += 1
	return correct / float(len(actual)) * 100.0 

# Confusion Matrix Metric development

def confusion_matrix(actual, predicted):
	unique = set(actual)
	matrix = [list() for x in range(len(unique))]
	for i in range(len(unique)):
		matrix[i] = [0 for x in range(len(unique))]
	lookup = dict()
	for i, value in enumerate(unique):
		lookup[value] = i
	for i in range(len(actual)):
		x = lookup[actual[i]]
		y = lookup[predicted[i]]
		matrix[y][x] += 1
	return unique, matrix


# This code will print a table for the results when we get to our models

def print_results(key, values, png):
  print('Category \tValue')
  print('----------------------------------')
  for i in range(len(key)):
    print(key[i], ' \t', values[i])
  print('----------------------------------\n\n\n\nSCREENSHOT THESE RESPONSES TO MAKE', png)
  return<jupyter_output><empty_output><jupyter_text>Conditioned Code. Only Use this for work to save on processing.We have put all of our data files on Github at [Github Link](https://github.com/cybertraining-dsc/fa20-523-301/tree/master/project/code).Previously we had moved all of the files to the following [Drive Location](https://drive.google.com/drive/folders/1flPbO7Q0ID70Or_OZ4MdW6CbYDE32GeT).  Images fo data engineering and exploratory analysisBelow are images taken of the code from our project_dataEngineering.ipynb of the work done prepare the data for training.  This includes transforming data across multiple tables and using keys to develop avg game metrics specific to injury and recovery dates.<jupyter_code># If Images were ran from Google, this code would be used. We loaded these images into Github

# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture1.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture2.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture3.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture4.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture5.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture6.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture7.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture8.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture9.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture10.PNG'))
# display(Image.open('/content/drive/My Drive/Colab Notebooks/data_engineering_images/Capture11.PNG'))<jupyter_output><empty_output><jupyter_text>![Capture 1](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture1.png)![Capture 2](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture2.png)![Capture 3](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture3.png)![Capture 4](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture4.png)![Capture 5](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture5.png)![Capture 6](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture6.png)![Capture 7](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture7.png)![Capture 8](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture8.png)![Capture 9](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture9.png)![Capture 10](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture10.png)![Capture 11](https://github.com/cybertraining-dsc/fa20-523-301/raw/master/project/images/capture11.png)<jupyter_code># ########################################################################
# # Load In Conditioned Datasets - Upload to the Colab files on the left #
# ########################################################################

# # df_Injury_length
# #df_Injury_length = pd.read_csv('/content/drive/My Drive/Colab Notebooks/df_Injury_length.csv') 
# df_Injury_length = pd.read_csv('/content/drive/My Drive/Colab Notebooks/data/df_Injury_length.csv') 
# # df_Injury_stats
# #df_Injury_stats = pd.read_csv('/content/drive/My Drive/Colab Notebooks/df_Injury_stats.csv') 
# df_Injury_stats = pd.read_csv('/content/drive/My Drive/Colab Notebooks/data/df_Injury_stats.csv') 

# ds_NBA_Injuries = [df_Injury_length, df_Injury_stats]
# #nba games dataset
# df_Games_games = pd.read_csv('/content/drive/My Drive/Colab Notebooks/data/games.csv')
# df_Games_gamesDetails = pd.read_csv('/content/drive/My Drive/Colab Notebooks/data/df_Games_gamesDetails.csv')
# df_Games_players = pd.read_csv('/content/drive/My Drive/Colab Notebooks/data/players.csv')
# df_Games_ranking = pd.read_csv('/content/drive/My Drive/Colab Notebooks/data/ranking.csv')
# df_Games_teams = pd.read_csv('/content/drive/My Drive/Colab Notebooks/data/teams.csv')
# ds_NBA_Games = [df_Games_games, df_Games_gamesDetails, df_Games_players, df_Games_ranking, df_Games_teams]
Benchmark.Start()

df_Injury_length = pd.read_csv('https://github.com/cybertraining-dsc/fa20-523-301/raw/main/project/data/df_Injury_length.csv') 
df_Injury_stats = pd.read_csv('https://github.com/cybertraining-dsc/fa20-523-301/raw/main/project/data/df_Injury_stats.csv') 
df_Injury_stats = pd.read_csv('https://github.com/cybertraining-dsc/fa20-523-301/raw/main/project/data/df_Injury_stats.csv') 
df_Season_stats = pd.read_csv('https://github.com/cybertraining-dsc/fa20-523-301/raw/main/project/data/df_Season_stats.csv') 

ds_NBA_Injuries = [df_Injury_length, df_Injury_stats]

time.sleep(3)
Benchmark.Stop()

# df = pd.read_csv(url, index_col=0)
# print(df.head(5))
Benchmark.print()<jupyter_output>+---------------------+------------------------------------------------------------------+
| Attribute           | Value                                                            |
|---------------------+------------------------------------------------------------------|
| BUG_REPORT_URL      | "https://bugs.launchpad.net/ubuntu/"                             |
| DISTRIB_CODENAME    | bionic                                                           |
| DISTRIB_DESCRIPTION | "Ubuntu 18.04.5 LTS"                                             |
| DISTRIB_ID          | Ubuntu                                                           |
| DISTRIB_RELEASE     | 18.04                                                            |
| HOME_URL            | "https://www.ubuntu.com/"                                        |
| ID                  | ubuntu                                                           |
| ID_LIKE             | debian                                                           [...]<jupyter_text>Data Exploration<jupyter_code>df_Injury_length.head()
df_Injury_stats.head()

plt.plot(df_Injury_length['NumberInjuries'], df_Injury_length['InjuryLengthDays'], 'o', color='black')
#plt.yticks(np.arange(0, max(df_Injury_length['InjuryLengthDays']), 100.0))
plt.title("Injury Length in Days over Number of Injuries")
plt.savefig('injury_length.png')
plt.plot(df_Injury_stats['pr5_MIN'], df_Injury_stats['InjuryLengthDays'], 'o', color='black')
#plt.yticks(np.arange(0, 800, 100.0))
plt.title("Injury Length in Days over Avg Minutes Playes in Prior5 games")
plt.savefig('injury_length_over_avg_min.png')
plt.hist(df_Injury_stats['pr5_MIN'], bins=[0, 6, 12, 18, 24, 30, 36, 42, 48], edgecolor='black')
plt.xticks([0, 6, 12, 18, 24, 30, 36, 42, 48])
plt.title("Frquency of injuries by Avg Minutes Played in Prior 5 games")
plt.savefig('frequencies_by_average_minutes.png')
plt.plot(df_Injury_stats['InjuryLengthDays'], df_Injury_stats['po5_MIN'], 'o', color='black')
plt.title("Avg Minutes Played in Post 5 Games over Injury Length in Days")
#####made mistake in engineering file need to fix beofre continue
plt.savefig('avg_minutes_played_in_post_5_per_injury_length.png')
plt.plot(df_Injury_stats['InjuryLengthDays'], (df_Injury_stats['po5_MIN']-df_Injury_stats['pr5_MIN']), 'o', color='black')
plt.title("Difference in Avg Minutes Played between Post 5 Games and Prior 5 games over Injury Length in Days")
plt.savefig('diff_avg_min_post5_and_prior5.png')
plt.plot(df_Injury_stats['InjuryLengthDays'], (df_Injury_stats['po5_PLUS_MINUS']-df_Injury_stats['pr5_PLUS_MINUS']), 'o', color='black')
plt.title("Difference in Avg Plus/Minus between Post 5 Games and Prior 5 games over Injury Length in Days")
plt.savefig('diff_plus_minus_post5_and_prior5.png')
plt.plot(df_Injury_stats['gp'], (df_Injury_stats['InjuryLengthDays']), 'o', color='black')
plt.title("Injury Length in Days over Player Games Played that Season")
plt.savefig('injurylength_gamesplayed.png')
plt.plot(df_Injury_stats['age'], (df_Injury_stats['InjuryLengthDays']), 'o', color='black')
plt.title("Injury Length in Days over Player Age that Season")
plt.savefig('injurylength_playerage.png')
plt.plot(df_Injury_stats['player_height'], (df_Injury_stats['InjuryLengthDays']), 'o', color='black')
plt.title("Injury Length in Days over Player Height")
plt.savefig('injurylength_playerHeight.png')
plt.plot(df_Injury_stats['player_weight'], (df_Injury_stats['InjuryLengthDays']), 'o', color='black')
plt.title("Injury Length in Days over Player Weight")
plt.savefig('injurylength_playerWeight.png')
#Pair Plot
sns.pairplot(df_Injury_stats)
plt.show()
 df_boxplot = pd.DataFrame()

df_boxplot['pr5_PLUS_MINUS'] = df_Injury_stats['pr5_PLUS_MINUS']

df_boxplot['po5_PLUS_MINUS'] = df_Injury_stats['po5_PLUS_MINUS']

sns.boxplot(data=df_boxplot)
#sns.plt.show()<jupyter_output><empty_output><jupyter_text>We are now ready to structure our dataframes The team must now use these downloads to create dataframes. Pandas dataframes will be easier to manage the data. The team will be able to use Pandas to process the data and allow the team to make correlations for feature engineering to create the models.  Part 2 Building the ModelsA link for Keras for us to use can be found [here](https://keras.io/guides/sequential_model/). We are first going to set up our Benchmark Test to be used when we are Benchmarking our model.<jupyter_code>def b():
  Benchmark.Start()
  print ("b")
  import time
  time.sleep(3)
  Benchmark.Stop()

def c():
  Benchmark.Start()
  print ("c")
  import time
  time.sleep(1)
  Benchmark.Stop()
 b()
 c()

 Benchmark.print()<jupyter_output>b
c

+---------------------+------------------------------------------------------------------+
| Attribute           | Value                                                            |
|---------------------+------------------------------------------------------------------|
| BUG_REPORT_URL      | "https://bugs.launchpad.net/ubuntu/"                             |
| DISTRIB_CODENAME    | bionic                                                           |
| DISTRIB_DESCRIPTION | "Ubuntu 18.04.5 LTS"                                             |
| DISTRIB_ID          | Ubuntu                                                           |
| DISTRIB_RELEASE     | 18.04                                                            |
| HOME_URL            | "https://www.ubuntu.com/"                                        |
| ID                  | ubuntu                                                           |
| ID_LIKE             | debian                                                       [...]<jupyter_text>Now that we know which GPU we are using, we can get into the actual work. The following is building our various models.<jupyter_code>np.random.seed(23)
warnings.filterwarnings("ignore")
df_Injury_stats_fillna = df_Injury_stats.copy()
df_Injury_stats_fillna.head()
cols =  ['DateRecovered',	'DateInjured',	'TEAM_ID',	'Team',	'Player',	'InjuryNotes',	'RecoverNotes', 'season', 'draft_year']

df_Injury_stats_fillna = df_Injury_stats_fillna.drop(cols, 1)
df_Injury_stats_fillna = df_Injury_stats_fillna.fillna(df_Injury_stats_fillna.mean())
## This applies the z-score to standardize our data. If a printout of the values 
## is needed, the reverse z-score with the mean can be used to pull out values

df_Injury_stats_fillna = df_Injury_stats_fillna.apply(zscore)

df_Injury_stats_fillna.head()
df_baseline = df_Injury_stats_fillna.copy()
sort_by = 'PLAYER_ID'

df_baseline.sort_values(by=[sort_by]).reset_index(drop=True, inplace=True)
df_baseline.head()
df_baseline.sort_values(by=[sort_by]).reset_index(drop=True, inplace=True)

X = df_baseline
X.head()
# When the dataframes are combined, this code could potentially be used for building up features in the future.

# features = ['inj_FGA',	'inj_FG_PCT',	'inj_FG3A',	'inj_FG3_PCT',	'inj_FTA',	'inj_FT_PCT',	'inj_REB',	'inj_AST',	'inj_STL',	
#             'inj_BLK',	'inj_TO',	'inj_PF', 'inj_PTS',	'inj_PLUS_MINUS',	'p5_MIN',	
#             'p5_FGA',	'p5_FG_PCT', 	'p5_FG3A', 	'p5_FG3_PCT',	'p5_FTA',	'p5_FT_PCT',	'p5_REB',
#             'p5_AST',	'p5_STL',	'p5_BLK',	'p5_TO',	'p5_PF',	'p5_PTS',	'p5_PLUS_MINUS']
# _all  = ['inj_FGA',	'inj_FG_PCT',	'inj_FG3A',	'inj_FG3_PCT',	'inj_FTA',	'inj_FT_PCT',	'inj_REB',	'inj_AST',	'inj_STL',	
#             'inj_BLK',	'inj_TO',	'inj_PF', 'inj_PTS',	'inj_PLUS_MINUS',	'p5_MIN',	
#             'p5_FGA',	'p5_FG_PCT', 	'p5_FG3A', 	'p5_FG3_PCT',	'p5_FTA',	'p5_FT_PCT',	'p5_REB',
#             'p5_AST',	'p5_STL',	'p5_BLK',	'p5_TO',	'p5_PF',	'p5_PTS',	'p5_PLUS_MINUS']
df_Injury_stats_fillna.head()
df_baseline = df_Injury_stats_fillna.copy()
### This should work for passing numbers for GBR. These results will be put into the final report.

# df_baseline = clean_dataset(df_baseline)


# print(df_baseline.head)

X = df_baseline
X = X.values.reshape(-1, 1)
# X was called above

# X = MinMaxScaler().fit_transform(X)

# print(X)

Y = df_baseline.values.reshape(-1,1).flatten() # Y is 38 times larger. Not sure what I did here. 
Y = Y.reshape(-1, 1) 

print(X.shape)
print(Y.shape)

size_x = X.shape[0]
size_y = Y.shape[0]
size_y = int(size_y/size_x)
# print(size_y)

Y = Y.reshape((size_x, size_y))
# Y = Y.reshape((16894, 38)) # Y is 38 times larger. Not sure what I did here. 

# Takes 2 minutes
# clf.set_params(n_estimators=2000)
# clf.fit(X, y, sample_weight=train_weight)

model = GradientBoostingRegressor()
model.fit(X, Y)



# model = GradientBoostingRegressor()
# define the evaluation procedure
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)


scoring = 'neg_mean_absolute_error'

# evaluate the model
n_scores = cross_val_score(model, X, Y, scoring=scoring, cv=cv, n_jobs=-1)
# report performance
print('MAE: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))


# accuracy=accuracy_score(y_pred,y_test)

results_GBR_key = []
results_GBR_values = []

results_GBR_key.append('MAE Mean')
results_GBR_values.append(np.round(mean(n_scores),decimals=3))
results_GBR_key.append('MAE STD')
results_GBR_values.append(np.round(std(n_scores),decimals=3))

# results_GBR.update({'MAE Mean' : np.round(str(mean(n_scores)),decimals=3)})
# results_GBR.update({'MAE STD' : np.round(str(std(n_scores)),decimals=3)})
print(results_GBR_key)
print(results_GBR_values)
n_scores = cross_val_score(model, X, Y, scoring='neg_root_mean_squared_error', cv=cv, n_jobs=-1)

results_GBR_key.append('RMSE Mean')
results_GBR_values.append(np.round(mean(n_scores),decimals=3))
results_GBR_key.append('RMSE STD')
results_GBR_values.append(np.round(std(n_scores),decimals=3))
                                          
print(results_GBR_key)
print(results_GBR_values)
n_scores = cross_val_score(model, X, Y, scoring='explained_variance', cv=cv, n_jobs=-1)
# report performance
print ("Explained Variance Score score: {0:.3f} (+/-{1:.3f})".format(np.mean(n_scores), std(n_scores)))

results_GBR_key.append('EV Mean')
results_GBR_values.append(np.round(mean(n_scores),decimals=3))
results_GBR_key.append('EV STD')
results_GBR_values.append(np.round(std(n_scores),decimals=3))

# results_GBR.update({'ACC Mean' : str(mean(n_scores))})
# results_GBR.update({'ACC STD' : str(std(n_scores))})

# # scoring = ['accuracy', 'neg_root_mean_squared_error', 'neg_mean_absolute_error']
print(results_GBR_key)
print(results_GBR_values)

png = 'gbr_results.PNG'
print_results(results_GBR_key, results_GBR_values, png)<jupyter_output>Category 	Value
----------------------------------
MAE Mean  	 -10.787
MAE STD  	 0.687
RMSE Mean  	 -115.929
RMSE STD  	 96.64
EV Mean  	 1.0
EV STD  	 0.0
----------------------------------



SCREENSHOT THESE RESPONSES TO MAKE gbr_results.PNG<jupyter_text>Light GBMThis is where we will make our Light GBM Model.<jupyter_code>df_baseline = df_Injury_stats_fillna.copy()

# lightgbm for regression

X = df_baseline
# X_scrub = df_baseline

# mean_X = np.mean(X_scrub)
# for i in  range(len(X_scrub)):
#   # print(mean_X)
#   X[i] = X_scrub[i]

# X = X.values.reshape(-1, 1)
X = X.values.reshape(-1, 1)

Y = df_baseline.values.reshape(-1,1).flatten() # Y is 38 times larger. Not sure what I did here. 
Y = Y.reshape(-1, 1) 

size_x = X.shape[0]
size_y = Y.shape[0]
size_y = int(size_y/size_x)
print(size_y)
Y = Y.reshape((size_x, size_y))

print("X Shape is ",X.shape)
print("Y Shape is ",Y.shape)

# Neat code from other write-up. This creates a dataset to define dataset
# X, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)

# evaluate the model
model = LGBMRegressor()
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)

### Searched Online. I need to use the TfidfVectorizer
# from sklearn.feature_extraction.text import TfidfVectorizer
# tfidf = TfidfVectorizer(binary=True)
# Y = tfidf.fit_transform(Y)

print("X Shape is ",X.shape)
print("Y Shape is ",Y.shape)

# print("Y Shape is ",Y.shape)

# n_scores = cross_val_score(model, X, Y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1, error_score='raise')
# print('MAE: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))

# fit the model on the whole dataset
model = LGBMRegressor()
model.fit(X, Y)


# y_pred=clf.predict(x_test)


n_scores = cross_val_score(model, X, Y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
# report performance
print('MAE: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
results_LGBM_key = []
results_LGBM_values = []

results_LGBM_key.append('MAE Mean')
results_LGBM_values.append(np.round(mean(n_scores),decimals=3))
results_LGBM_key.append('MAE STD')
results_LGBM_values.append(np.round(std(n_scores),decimals=3))

print(results_LGBM_key)
print(results_LGBM_values)
png = 'lgbm_results.PNG'
print_results(results_LGBM_key, results_LGBM_values, png)
n_scores = cross_val_score(model, X, Y, scoring='neg_root_mean_squared_error', cv=cv, n_jobs=-1)
# report performance
print('RMSE: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))
results_LGBM_key.append('RMSE Mean')
results_LGBM_values.append(np.round(mean(n_scores),decimals=3))
results_LGBM_key.append('RMSE STD')
results_LGBM_values.append(np.round(std(n_scores),decimals=3))

print(results_LGBM_key)
print(results_LGBM_values)
n_scores = cross_val_score(model, X, Y, scoring='explained_variance', cv=cv, n_jobs=-1)
# report performance
print ("Explained Variance Score: {0:.3f} (+/-{1:.3f})".format(np.mean(n_scores), std(n_scores)))
 

results_LGBM_key.append('EV Mean')
results_LGBM_values.append(np.round(mean(n_scores),decimals=3))
results_LGBM_key.append('EV STD')
results_LGBM_values.append(np.round(std(n_scores),decimals=3))


png = 'lgbm_results.PNG'
print_results(results_LGBM_key, results_LGBM_values, png)

### If omitting lower level features, use this code. It will help on speed.
# omit_lowest = 20
# _selected = list(top_features[omit_lowest:].index)<jupyter_output><empty_output><jupyter_text>Building the Keras Model<jupyter_code>from keras import backend as K
from keras.models import Sequential
from keras.layers.core import Dense , Dropout
from keras import regularizers
tf.keras.backend.set_floatx('float64')


# define base model
def baseline_model(loss_val):
	# create model
	model = Sequential()
	model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu'))
	model.add(Dense(1, kernel_initializer='normal'))
	# Compile model
	model.compile(loss=loss_val, optimizer='adam')
	return model
# evaluate model
df_baseline = df_Injury_stats_fillna.copy()
sort_by = 'PLAYER_ID'
df_baseline.head()
df_baseline = df_Injury_stats_fillna.copy()
X = df_baseline
# X = X.values.reshape(-1, 1)
X = X.values.reshape(-1, 1)

Y = df_baseline.values.reshape(-1,1).flatten()
Y = df_baseline.values.reshape(-1,1).flatten() # Y is 38 times larger. Not sure what I did here. 
Y = Y.reshape(-1, 1) 
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=101)<jupyter_output><empty_output><jupyter_text>Kera Model Result Testing<jupyter_code>from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import cross_val_score, KFold
from keras.models import Sequential
from sklearn.metrics import accuracy_score
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
seed = 1

# diabetes = datasets.load_diabetes()
# X = diabetes.data[:150]
# Y = diabetes.target[:150]

#Regressor_Epochs = 25, Regressor batch_size=25, KFold=10, model.fit epochs=10
def baseline_model():
    model = Sequential()
    model.add(Dense(10, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


model_keras = KerasRegressor(build_fn=baseline_model, nb_epoch=25, batch_size=25, verbose=False)
kfold = KFold(n_splits=10, random_state=seed)
n_scores = cross_val_score(model_keras, X_train, Y_train, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (n_scores.mean(), n_scores.std()))


model_keras.fit(X_train, Y_train, epochs=10, steps_per_epoch=30)
prediction = model_keras.predict(X_test)
print(mean_squared_error(Y_test, prediction))
print(r2_score(Y_test, prediction))
#Regressor_Epochs =40, Regressor batch_size=25, KFold=20, model.fit epochs=10
def baseline_model2():
    model = Sequential()
    model.add(Dense(10, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


model_keras = KerasRegressor(build_fn=baseline_model2, nb_epoch=40, batch_size=25, verbose=False)
kfold = KFold(n_splits=20, random_state=seed)
n_scores = cross_val_score(model_keras, X_train, Y_train, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (n_scores.mean(), n_scores.std()))

model_keras.fit(X_train, Y_train, epochs=10, steps_per_epoch=30)
prediction = model_keras.predict(X_test)
print(mean_squared_error(Y_test, prediction))
print(r2_score(Y_test, prediction))
#Regressor_Epochs = 20, Regressor batch_size=25, KFold=20, model.fit epochs=10
def baseline_model3():
    model = Sequential()
    model.add(Dense(10, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


model_keras = KerasRegressor(build_fn=baseline_model3, nb_epoch=20, batch_size=25, verbose=False)
kfold = KFold(n_splits=20, random_state=seed)
n_scores = cross_val_score(model_keras, X_train, Y_train, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (n_scores.mean(), n_scores.std()))



model_keras.fit(X_train, Y_train, epochs=10, steps_per_epoch=30)
prediction = model_keras.predict(X_test)
print(mean_squared_error(Y_test, prediction))
print(r2_score(Y_test, prediction))
#Regressor_Epochs = 20, Regressor batch_size=20, KFold=10, model.fit epochs=10
def baseline_model4():
    model = Sequential()
    model.add(Dense(10, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


model_keras = KerasRegressor(build_fn=baseline_model4, nb_epoch=20, batch_size=20, verbose=False)
kfold = KFold(n_splits=10, random_state=seed)
n_scores = cross_val_score(model_keras, X_train, Y_train, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (n_scores.mean(), n_scores.std()))


model_keras.fit(X_train, Y_train, epochs=10, steps_per_epoch=30)
prediction = model_keras.predict(X_test)
print(mean_squared_error(Y_test, prediction))
print(r2_score(Y_test, prediction))
#Regressor_Epochs = 25, Regressor batch_size=25, KFold=10, model.fit epochs=5
def baseline_model5():
    model = Sequential()
    model.add(Dense(10, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


model_keras = KerasRegressor(build_fn=baseline_model5, nb_epoch=25, batch_size=25, verbose=False)
kfold = KFold(n_splits=10, random_state=seed)
n_scores = cross_val_score(model_keras, X_train, Y_train, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (n_scores.mean(), n_scores.std()))


model_keras.fit(X_train, Y_train, epochs=5, steps_per_epoch=30)
prediction = model_keras.predict(X_test)
print(mean_squared_error(Y_test, prediction))
print(r2_score(Y_test, prediction))
#Regressor_Epochs = 25, Regressor batch_size=25, KFold=10, model.fit epochs=20
def baseline_model6():
    model = Sequential()
    model.add(Dense(10, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


model_keras = KerasRegressor(build_fn=baseline_model6, nb_epoch=25, batch_size=25, verbose=False)
kfold = KFold(n_splits=10, random_state=seed)
n_scores = cross_val_score(model_keras, X_train, Y_train, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (n_scores.mean(), n_scores.std()))


model_keras.fit(X_train, Y_train, epochs=20, steps_per_epoch=30)
prediction = model_keras.predict(X_test)
print(mean_squared_error(Y_test, prediction))
print(r2_score(Y_test, prediction))<jupyter_output>Results: -0.00 (0.00) MSE
0.01853343164863198
0.9816710726977972<jupyter_text>Not Sure What is going on here. Strange. Will work more on the Keras model tomorrow.<jupyter_code>print(X_train)
results_cnn_key = ['To\t','Be\t','Determined']
results_cnn_values = ['To','Be','Determined']
### Need to fill this out as I get real results. This is frustrating

png = 'cnn_results.png'
print_results(results_cnn_key, results_cnn_values, png)<jupyter_output>Category 	Value
----------------------------------
To	  	 To
Be	  	 Be
Determined  	 Determined
----------------------------------



SCREENSHOT THESE RESPONSES TO MAKE cnn_results.png
{'fraction_non_alphanumeric': 0.14713592869314457, 'fraction_numerical': 0.02346310132389566, 'mean_word_length': 4.464100268576544, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 23, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '391452', 'license': 'Apache-2.0', 'n_tokens_mistral': 11576, 'n_tokens_neox': 10672, 'n_words': 2146, 'path': 'docs/report/fa20-523-301/project/code/project.ipynb', 'repo_name': 'mikahla1/cybertraining-dsc.github.io', 'size': '30516'}