<jupyter_start><jupyter_text>1. Ingesting the data to jupyter notebook<jupyter_code>data = pd.read_csv('game.csv') 
print("Dataset Shape: ", data.shape)

data.head(10)
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 171907 entries, 0 to 171906
Columns: 161 entries, date to acquisition_info
dtypes: float64(77), int64(6), object(78)
memory usage: 211.2+ MB<jupyter_text>The dataset contains 161 columns and 171906 rows<jupyter_code>list(data)<jupyter_output><empty_output><jupyter_text>Above is the list of all the 161 columns  After studying the columns, we eleminated columns 29 to the last column.<jupyter_code>data.drop(data.iloc[:,29:200], inplace = True, axis = 1)
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 171907 entries, 0 to 171906
Data columns (total 29 columns):
 #   Column             Non-Null Count   Dtype  
---  ------             --------------   -----  
 0   date               171907 non-null  int64  
 1   number_of_game     171907 non-null  int64  
 2   day_of_week        171907 non-null  object 
 3   v_name             171907 non-null  object 
 4   v_league           171907 non-null  object 
 5   v_game_number      171907 non-null  int64  
 6   h_name             171907 non-null  object 
 7   h_league           171907 non-null  object 
 8   h_game_number      171907 non-null  int64  
 9   v_score            171907 non-null  int64  
 10  h_score            171907 non-null  int64  
 11  length_outs        140841 non-null  float64
 12  day_night          140150 non-null  object 
 13  completion         116 non-null     object 
 14  forefeit           145 non-null     object 
 15  protest            180 non-null     object 
 16  pa[...]<jupyter_text>The above is the list of the columns after applying Line 215   2. Eliminating further the columns that are not useful in the dataset<jupyter_code>res = data.drop(['date','number_of_game','day_of_week','v_name','v_league','v_game_number','h_name',
          'h_league','h_game_number','h_name','h_league','h_game_number',
          'length_outs','day_night','completion','forefeit','protest','park_id','attendance',
          'length_minutes','v_line_score','h_line_score'],axis=1)
res
res.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 171907 entries, 0 to 171906
Data columns (total 10 columns):
 #   Column             Non-Null Count   Dtype  
---  ------             --------------   -----  
 0   v_score            171907 non-null  int64  
 1   h_score            171907 non-null  int64  
 2   v_at_bats          140838 non-null  float64
 3   v_hits             140838 non-null  float64
 4   v_doubles          140838 non-null  float64
 5   v_triples          140838 non-null  float64
 6   v_homeruns         140838 non-null  float64
 7   v_rbi              140838 non-null  float64
 8   v_sacrifice_hits   140838 non-null  float64
 9   v_sacrifice_flies  140838 non-null  float64
dtypes: float64(8), int64(2)
memory usage: 13.1 MB<jupyter_text>The information above are the columns that are important to answer our hypothesis.<jupyter_code>res.head(10)<jupyter_output><empty_output><jupyter_text>3. We created another column called Winner to represent the winning team (note: its not yet converted to binary)<jupyter_code>res['Winner'] = res['h_score'] - res['v_score']
res<jupyter_output><empty_output><jupyter_text>4. Our target is the Winner while our features are v_at_bats,	v_hits,	v_doubles,	v_triples,	v_homeruns,	v_rbi,	v_sacrifice_hits, and	v_sacrifice_flies. Now we graph each feature versus the target to find pattern in the data.<jupyter_code># v_at_bats vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_at_bats', data = res)
res.plot.scatter(x = 'v_at_bats', y = 'Winner')<jupyter_output><empty_output><jupyter_text>The barplot shows a downward trend while the scatter plot shows no trend.<jupyter_code># v_hits vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_hits', data = res)
res.plot.scatter(x = 'v_hits', y = 'Winner')<jupyter_output><empty_output><jupyter_text>The barplot shows a downward trend. Also the scatter plot shows a downward trend.<jupyter_code># v_doubles vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_doubles', data = res)
res.plot.scatter(x = 'v_doubles', y = 'Winner')<jupyter_output><empty_output><jupyter_text>The barplot shows a downward trend while the scatter plot shows a slight downward trend and an inverse behavior.<jupyter_code># v_triples vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_triples', data = res)
res.plot.scatter(x = 'v_triples', y = 'Winner')<jupyter_output><empty_output><jupyter_text>Both bar graph behaves downward trend while scatter plot shows downward trend and an inversely proportional behavior.<jupyter_code># v_homeruns vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_homeruns', data = res)
res.plot.scatter(x = 'v_homeruns', y = 'Winner')<jupyter_output><empty_output><jupyter_text>Both bar and scatter plots show downward trend. Also, the scatter graph display an inverse proportional relationship.<jupyter_code># v_rbi vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_rbi', data = res)
res.plot.scatter(x = 'v_rbi', y = 'Winner')<jupyter_output><empty_output><jupyter_text>The bar plot follows a downward trend while scatter graph displays  strong downward trend and an inversely proportional behavior.<jupyter_code># v_sacrifice_hits vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_sacrifice_hits', data = res)
res.plot.scatter(x = 'v_sacrifice_hits', y = 'Winner')<jupyter_output><empty_output><jupyter_text>The barplot shows a slight downward trend. The scatter plot shows an inverse relationship.<jupyter_code># v_sacrifice_flies vs Winner
plt.figure(figsize=(40,10))
sns.barplot(x = 'Winner', y = 'v_sacrifice_flies', data = res)
res.plot.scatter(x = 'v_sacrifice_flies', y = 'Winner')<jupyter_output><empty_output><jupyter_text>The barplot shows a downward trend while the scatter graph shows an inverse behavior.  5. Now we are going to convert the target to binary numbers: 1 represents a win and 0 represents a loss.<jupyter_code>res['Winner'] = res['Winner'].apply(lambda x: 1 if x > 0 else 0)
res.head(20)
print(res['Winner'].value_counts())

fig = plt.figure(figsize=(10,6))
sns.countplot(res['Winner'])<jupyter_output>1    93587
0    78320
Name: Winner, dtype: int64<jupyter_text>Removing the first two columns, we do not really need them. Here we obtained our final data with the features as our independent variables and the target (Winner) as the dependent variable.<jupyter_code>Data_Final = res.drop(['v_score','h_score'],axis=1)
Data_Final.head()
Data_Final.mean()<jupyter_output><empty_output><jupyter_text>Replacing data that are empty, Null, na, or none to NAN<jupyter_code>df = Data_Final.replace([' ','NULL','na','(none)'],np.nan)
print(df.isin(['NaN']))
print(df.isin(['NaN']).mean())
# drop any NaNs
df_2 = df.dropna()
# drop any NaNs
df_2.shape
df_2.dtypes<jupyter_output><empty_output><jupyter_text>6. Apply different Machine Learning Model<jupyter_code>#ML model
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer 
from sklearn.model_selection import TimeSeriesSplit
from sklearn.model_selection import cross_validate

from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
#VS
from yellowbrick.classifier import classification_report, confusion_matrix, discrimination_threshold
features = df_2[[ 'v_at_bats', 'v_hits', 'v_doubles','v_triples','v_homeruns',
                 'v_rbi','v_sacrifice_hits','v_sacrifice_flies']]

target = df_2[['Winner']].values.ravel()
from sklearn.ensemble import RandomForestClassifier

from yellowbrick.datasets import load_occupancy
from yellowbrick.model_selection import FeatureImportances

# Load the classification data set

model = RandomForestClassifier(n_estimators=10)
viz = FeatureImportances(model)
viz.fit(features, target)
viz.show()
#figer out the best seven features
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
    
fvalue_Best = SelectKBest(f_classif, k=8)
X_kbest = fvalue_Best.fit_transform(features, target)
print('Original number of features:', df_2.shape)
print('Reduced number of features:', X_kbest.shape)
#split the data
X_train, X_test, y_train, y_test = train_test_split(
    X_kbest, target, test_size=0.2
)

X_train.shape
y_train.shape
# Taking the best seven features and applying them to different ML models
cross_validate(GaussianNB(), X_train, y_train, cv=TimeSeriesSplit(), scoring='f1_macro')
classification_report(model, X_train, y_train, is_fitted=False)
cross_validate(SVC(kernel='rbf', degree=3), X_train, y_train, cv=TimeSeriesSplit(), scoring='f1_macro')
classification_report(SVC(kernel='rbf', degree=3), X_train, y_train,is_fitted=False)
cross_validate(KNeighborsClassifier(n_neighbors=5), X_train, y_train, cv=TimeSeriesSplit(),
               scoring='f1_macro')
classification_report(KNeighborsClassifier(n_neighbors=5), X_train, y_train)
cross_validate(LogisticRegression(), X_train, y_train, cv=TimeSeriesSplit(), scoring='f1_macro')
classification_report(LogisticRegression(), X_train, y_train)
cross_validate(GaussianNB(), X_train, y_train, cv=TimeSeriesSplit(), scoring='f1_macro')
classification_report(GaussianNB(), X_train, y_train, is_fitted=False)
X = features
y = target
from yellowbrick.classifier import confusion_matrix
from sklearn.model_selection import train_test_split as tts

X_train, X_test, y_train, y_test = tts(X,y, test_size = 0.2)

confusion_matrix(
        GaussianNB(),
        X_train, y_train, X_test, y_test,
        classes=['not_defaulted','defaulted']
)
plt.tight_layout()
from yellowbrick.classifier import confusion_matrix
from sklearn.model_selection import train_test_split as tts

X_train, X_test, y_train, y_test = tts(X,y, test_size = 0.2)

confusion_matrix(
        LogisticRegression(),
        X_train, y_train, X_test, y_test,
        classes=['not_defaulted','defaulted']
)
plt.tight_layout()
from yellowbrick.classifier import confusion_matrix
from sklearn.model_selection import train_test_split as tts

X_train, X_test, y_train, y_test = tts(X,y, test_size = 0.2)

confusion_matrix(
        SVC(kernel='rbf', degree=3),
        X_train, y_train, X_test, y_test,
        classes=['not_defaulted','defaulted']
)
plt.tight_layout()
from yellowbrick.classifier import confusion_matrix
from sklearn.model_selection import train_test_split as tts

X_train, X_test, y_train, y_test = tts(X,y, test_size = 0.2)

confusion_matrix(
        KNeighborsClassifier(n_neighbors=5),
        X_train, y_train, X_test, y_test,
        classes=['not_defaulted','defaulted']
)
plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10732105597387281, 'fraction_numerical': 0.03665063957180441, 'mean_word_length': 4.829719725013221, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 73, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '78090', 'license': 'MIT', 'n_tokens_mistral': 4123, 'n_tokens_neox': 3784, 'n_words': 946, 'path': 'Capstone project (December 16, 2021).ipynb', 'repo_name': 'georgetown-analytics/MLB', 'size': '11023'}