<jupyter_start><jupyter_text>Importing required packages:<jupyter_code>import numpy as np
import pandas as pd
import scipy as sp
import warnings
import seaborn as sns
import matplotlib.pyplot as plt
warnings.filterwarnings('ignore')
# ps2020 stands for pitching_stats 2020:
data_ps2020 = pd.read_excel("pitching_stats.xls")
data_ps2020.head()<jupyter_output><empty_output><jupyter_text>Model Alterations    RK: Rank     G: Games    GS: Games started    IP: Innings pitched    ER: Earned runs    K: Strikeouts    SV: Saves    SHO: Shutouts    W-L: Wins-Losses        CYP: In The Neyer/James Guide To Pitchers -- co-authored by <NAME> and ESPN.com's <NAME> presents a method, based on past results, to predict Cy Young balloting. This report provides an in-season snapshot of the Cy Young "race," as figured by the following formula: Cy Young Points         (CYP) = ((5*IP/9)-ER) + (SO/12) + (SV*2.5) + Shutouts + ((W*6)-(L*2)) + VB     VB: Victory Bonus is a 12-point bonus awarded for leading your team to the division championship. (***Is not included in the model.)<jupyter_code>#cyp for each row:
ip,er,so,sv,shutouts,w,l  = data_ps2020["IP"],data_ps2020["ER"],data_ps2020["SO"],data_ps2020["SV"],data_ps2020["SHO"],data_ps2020["W"],data_ps2020["L"]
CYP = []
for i in range(data_ps2020.shape[0]):
    cyp = [5 * (ip[i] / 9)] - er[i] +(so[i] / 12) + (sv[i] * 2.5)+ shutouts [i] +((w[i] * 6) - (l[i] * 2)) + 0
    CYP.append(int(cyp))
data_ps2020["CYP"] = CYP<jupyter_output><empty_output><jupyter_text>Top 30 MLB starters<jupyter_code>filtered_data2020 = data_ps2020[(data_ps2020["GS"] > 10) & (data_ps2020["CYP"] > 32)].sort_values("CYP", ascending=False)
filtered_data2020 = filtered_data2020 [['Name','Tm','Lg','GS','IP','ER','SO','SV','SHO','W','L','CYP']]
filtered_data2020
data_ps2020[['GS','IP','ER','SO','SV','SHO','W','L','CYP']].describe().T
#cyp for each row in Pitching dataset:
data_p = pd.read_csv("Pitching.csv")

data_p.shape
data_p["IP"] =  [int(i/3) for i in data_p["IPouts"]] #since it is mentioned that we can calculate IP by dividing the IPouts by 3;
ip,er,so,sv,shutouts,w,l  = data_p["IP"],data_p["ER"],data_p["SO"],data_p["SV"],data_p["SHO"],data_p["W"],data_p["L"]
CYP = []
for i in range(data_p.shape[0]):
    cyp = [(5 * (ip[i] / 9))] - er[i] +(so[i] / 12) + (sv[i] * 2.5)+ shutouts [i] +((w[i] * 6) - (l[i] * 2)) + 0
    CYP.append(int(cyp))
data_p["CYP"] = CYP
data_p.head()
##adding the CYA column based on the CY YOUNG Award recipients table - used on pitching table:
# reading AwardsSharePlayers data
data_aw = pd.read_csv("AwardsSharePlayers.csv").reset_index(drop = True)
data_aw.head()
# 2. Adding awardID to the dataset (0/1 column):
data_merged = data_p.merge(data_aw, on=['playerID','yearID'], how='left')
# adding 0 or 1 column for CYA and MVP:
CYA = []
MVP = []
for i in range(data_merged.shape[0]):
    cya = 0
    mvp = 0
    if data_merged["awardID"][i] == "Cy Young":
        cya = 1
        mvp =0
    elif data_merged["awardID"][i] == "MVP":
        cya = 0
        mvp =1
    else:
        cya = 0
        mvp =0
    CYA.append(int(cya))
    MVP.append(int(mvp))
data_merged ["CYA"],data_merged ["MVP"] = CYA, MVP

data_final = data_merged.drop(['votesFirst','pointsMax','lgID_y','awardID','pointsWon'],axis = 1)<jupyter_output><empty_output><jupyter_text>Filtering the dataset based on CYP column (for Cy Young Awards):<jupyter_code># # filtering from 1950 to 2017
data_final_flt = data_final[(data_final["yearID"]>= 1950) & (data_final["yearID"] <= 2017) & (data_final["CYP"] >= 80)].reset_index(drop = True)
data_final_flt[['GS','IP','ER','SO','SV','SHO','W','L','CYP']].describe()
# CYA column, value counts:
print(f"***whole dataset****")
print(data_final_flt["CYA"].value_counts(normalize=True))
import seaborn as sns
data_final_flt["CYA"].value_counts(normalize=True).plot(kind='bar');
plt.xlabel("CYA", labelpad=14)
plt.ylabel("Portion", labelpad=14)
plt.title(f"Portion of CYA awards receivers/none-receivers \n'Raw Data'", y=1.02);
data_final_flt.to_csv("CyYoungAward_Data.csv")
# handling the imbalanced dataset:
# upsampling:
from sklearn.utils import resample
data_majority = data_final_flt[data_final_flt["CYA"]==0]
data_minority = data_final_flt[data_final_flt["CYA"]==1]

data_minority_upsampled = resample(data_minority, 
                                 replace=True,     # sample with replacement
                                 n_samples=4635,    # to match majority class
                                 random_state=123) # reproducible results
data_minority_upsampled = pd.concat([data_majority, data_minority_upsampled])
data_minority_upsampled["CYA"].value_counts()
data = data_minority_upsampled

data.to_csv("CyYoungAward_Data_UpSampled.csv")
data["CYA"].value_counts(normalize=True).plot(kind='bar');
plt.xlabel("CYA", labelpad=14)
plt.ylabel("Portion", labelpad=14)
plt.title(f"Portion of CYA awards receivers/none-receivers \n'After Over Sampling'", y=1.02);
#spliting the data:
from sklearn.model_selection import train_test_split

# for CYA:
X = data[["GS" ,"IP","ER", "SO", "SV","W" ,"L"]]
y = data[["CYA"]]

x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.2,random_state = 0)
#summary of the table:
print(f"***train dataset****")
print(y_train["CYA"].value_counts(normalize=True))
print(f"****test dataset****")
print(y_test["CYA"].value_counts(normalize=True))
# data is not imbalanced<jupyter_output>***train dataset****
1    0.501348
0    0.498652
Name: CYA, dtype: float64
****test dataset****
0    0.505394
1    0.494606
Name: CYA, dtype: float64<jupyter_text>Train and Test datasets are balanced on CYA column;<jupyter_code>print(y_train.shape)
print(x_train.shape)
print(y_test.shape)
print(x_test.shape)<jupyter_output>(7416, 1)
(7416, 7)
(1854, 1)
(1854, 7)<jupyter_text>Logistic Regression:<jupyter_code>from sklearn import linear_model
from sklearn import metrics
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score
lr = linear_model.LogisticRegression()
lr.fit(x_train,y_train)
lr.coef_
lr.intercept_
#model evaluation:
lr_train_pred = lr.predict(x_train)
lr_test_pred = lr.predict(x_test)
metrics.accuracy_score(y_train,lr_train_pred), metrics.accuracy_score(y_test,lr_test_pred)
y_hats = lr.predict(x_test)

y_test['preds'] = y_hats

df_out = pd.merge(data,y_test[['preds']],how = 'left',left_index = True, right_index = True)
df_out  = df_out.dropna().drop_duplicates()
df_outx= df_out[(df_out["yearID"] >= 2017)]
df_outx
from sklearn.metrics import classification_report
print(classification_report(y_test,lr_test_pred))
test_cm = metrics.confusion_matrix(y_test,lr_test_pred)
test_cm
#confusion matrix plot:
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");
cohen_kappa_score(y_test, lr_test_pred)
mean_absolute_error(y_test,lr_test_pred)
mean_squared_error(y_test,lr_test_pred)
r2_score(y_test,lr_test_pred)<jupyter_output><empty_output><jupyter_text>Naïve Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.3,
                                                     random_state = 0)
NB = GaussianNB()
NB.fit(x_train,y_train)
NB_train_pred = NB.predict(x_train)
NB_test_pred = NB.predict(x_test)
metrics.accuracy_score(y_train,NB_train_pred)
metrics.accuracy_score(y_test,NB_test_pred)
print(classification_report(y_test,NB_test_pred))
#confusion matrix:
test_cm = metrics.confusion_matrix(y_test,NB_test_pred)
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");
cohen_kappa_score(y_test, NB_test_pred)
r2_score(y_test,NB_test_pred)<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>from sklearn import tree
from sklearn.tree import export_graphviz
x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.3, random_state = 0)
DT = tree.DecisionTreeClassifier(max_depth = 10, min_samples_split = 5)

#-------------------
DT.fit(x_train,y_train)
DT_pred = DT.predict(x_test)
print(classification_report(y_test,DT_pred))
test_cm = metrics.confusion_matrix(y_test,DT_pred)
#confusion matrix:
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");
cohen_kappa_score(y_test, DT_pred)
r2_score(y_test,DT_pred)
#importance of each value:
fi = DT.feature_importances_
pd.DataFrame({'variable':x_test.columns,
             'importance': fi })<jupyter_output><empty_output><jupyter_text>Neural Network<jupyter_code>#data preprocessing
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X = data[["GS" ,"IPouts","ER", "SO", "SV", "SHO" ,"W" ,"L"]]
y = data[["CYA"]]

x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.3, random_state = 0)
# #-------------------
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.fit_transform(x_test)
from sklearn.neural_network import MLPClassifier
NN = MLPClassifier(solver='lbfgs',alpha=0.5,
                   hidden_layer_sizes=(10,4),random_state=1)
NN.fit(x_train_scaled,y_train)
#prediction model:
NN_pred = NN.predict(x_test_scaled)
metrics.accuracy_score(NN_pred, y_test)
print(classification_report(y_test,NN_pred))
r2_score(y_test,NN_pred)
#confusion matrix:
test_cm = metrics.confusion_matrix(y_test,NN_pred)
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");<jupyter_output><empty_output><jupyter_text>Support Vector Machine<jupyter_code>from sklearn import svm
scaler = MinMaxScaler()
x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.3, random_state = 0)
# -------------------
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.fit_transform(x_test)

#SVM Linear:
svm_linear = svm.SVC(kernel='linear')
svm_linear.fit(x_train_scaled,y_train)
svm_linear.coef_
##SVM Linear - Prediction Model:
svm_linear_pred = svm_linear.predict(x_test_scaled)
metrics.accuracy_score(y_test,svm_linear_pred)
print(classification_report(y_test,svm_linear_pred))
#All other SVM methods (RBF, Poly) gave the same results, so I just put the Linear method in the report:
#confusion matrix:
test_cm = metrics.confusion_matrix(y_test,svm_linear_pred)
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");<jupyter_output><empty_output><jupyter_text>Random Forests<jupyter_code>x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.3, random_state = 0)
from sklearn.ensemble import RandomForestClassifier
RF = RandomForestClassifier(n_estimators=100,random_state=0)
RF.fit(x_train,y_train)
RF_pred = RF.predict(x_test)
metrics.accuracy_score(RF_pred,y_test)
print(classification_report(y_test,RF_pred))
fi = RF.feature_importances_
pd.DataFrame({'variable':x_test.columns,
             'importance': fi })
from sklearn.model_selection import cross_val_score
n_estimator = range(2,104,2)
accuracy = []
for i in n_estimator:
    RF = RandomForestClassifier(n_estimators = i, random_state = 0)
    scores = cross_val_score(RF,x_train,y_train)
    accuracy.append(scores.mean());
fig = plt.figure();
ax0 = fig.add_subplot(111)
ymax = max(accuracy)
xpos = accuracy.index(ymax)
xmax = n_estimator[xpos]
plt.plot(n_estimator,accuracy);
plt.xlabel("n_estimator");
plt.ylabel("Accuracy");
plt.title("Accuracy vs. n_estimator");
txt = f"max accuracy:{round(ymax,5)}"
ax0.set_ylim(0.4,0.9);
ax0.annotate(txt, xy=(xmax, ymax), xytext=(xmax, ymax+0.06),
arrowprops=dict(facecolor='blue', shrink=0.05),);<jupyter_output><empty_output><jupyter_text>AdaBoost Classifier<jupyter_code>from sklearn.ensemble import AdaBoostClassifier
n_estimator = list(range(1,100,2))
accuracy = []
for i in n_estimator:
    Ada = AdaBoostClassifier(n_estimators = i, learning_rate=0.005, random_state=21)
    scores = cross_val_score(Ada,x_train,y_train)
    accuracy.append(scores.mean())
fig = plt.figure();
ax = fig.add_subplot(111)
plt.plot(n_estimator,accuracy)
plt.xlabel("n_estimators")
plt.ylabel("Accuracy")
plt.title("Adaboost Accuracy");
ymax = max(accuracy)
xpos = accuracy.index(ymax)
xmax = n_estimator[xpos]
txt = f"max accuracy:{round(ymax,3)}"
ax.annotate(txt, xy=(xmax, ymax), xytext=(xmax, ymax+0.0029),
arrowprops=dict(facecolor='red', shrink=1),)
ax.set_ylim(0.100,1.00);<jupyter_output><empty_output><jupyter_text>MLB Most Valuable Player MVP Award WinnersThe Most Valuable Player Award is given annually to one player in each league. The award began in 1911 as the Chalmers Award, honoring the "most important and useful player to the club and to the league". This award was discontinued in 1914. From 1922 to 1928 in the American League and from 1924 to 1929 in the National League, an MVP award was given to "the baseball player who is of the greatest all-around service to his club". Prior winners were not eligible to win the MVP award again during this time. The current incarnation of the MVP award was established in 1931. The Baseball Writers' Association of America (BBWAA) votes on the MVP award at the conclusion of each season before the postseason starts.     Dataset used: Batting.csv    Variables used (Already in the dataset):    H: Hits    BB: Walks by batters    HBP: Batters hit by pitch    AB: At Bats    SF : Sacrifice flies    H, 2B, 3B: Hits, Doubles, Triples    HR: Homeruns    RBI: Runs batted in    SB: Stolen Bases;              MVP Awards related variables (not in the dataset but calculated with formulas):    • BA:Hits/At Bats (H/AB);     according to baseball reference website;    • OBP: (H + BB + HBP) / (AB + BB + HBP + SF);     according to baseball reference website;    • SLG: (H - X2B - X3B - HR + 2 * X2B + 3 * X3B + 4 * HR) / AB;    according to <NAME> and <NAME> article;<jupyter_code>data_bat = pd.read_csv("Batting.csv")
data_bat.shape
#importing data:

data_bat = data_bat[["playerID","yearID","H","BB","HBP","AB","SF","2B","3B","HR","RBI","SB"]]
data_bat_merged = data_bat.merge(data_aw, on=['playerID','yearID'], how='left')
# #------
MVP_bat = []
for i in range(data_bat_merged.shape[0]):
    mvp = 0
    if data_bat_merged["awardID"][i] == "MVP":
        mvp = 1
    else:
        mvp = 0
    MVP_bat.append(int(mvp))
data_bat_merged ["MVP"] = MVP_bat
data_bat_merged_final = data_bat_merged.drop(['votesFirst','lgID','pointsMax','awardID','pointsWon'],axis = 1)
#------
data_bat_merged_final.head()
dbmf = data_bat_merged_final
dbmf.fillna(0)
## Calculating BA:
dbmf["BA"] =  [float(dbmf["H"][i]/dbmf["AB"][i]) for i in range(dbmf.shape[0])] 
## Calculating OBP: (H + BB + HBP) / (AB + BB + HBP + SF)
h,bb,hbp,ab,sf  = dbmf["H"],dbmf["BB"],dbmf["HBP"],dbmf["AB"],dbmf["SF"]
OBP = []
for i in range(dbmf.shape[0]): 
    obp = (h[i] + bb[i] + hbp[i]) / (ab[i] + bb [i] + hbp [i] + sf [i])        
    OBP.append(float(obp))
dbmf["OBP"] = OBP
## Calculating SLG: (H - X2B - X3B - HR + 2 * X2B + 3 * X3B + 4 * HR) / AB
b2,b3,hr  = dbmf["2B"],dbmf["3B"],dbmf["HR"]
SLG = []
for i in range(dbmf.shape[0]): 
    slg = ([h[i] - b2[i] - b3[i] - hr[i] + (2*b2[i]) + (3*b3[i]) + (4*hr[i])]/ ab[i])   
    SLG.append(float(slg))
dbmf["SLG"] = SLG
# droping unnecessary columns: 
dbmf = dbmf.drop (['H','2B','3B','AB','SF','HBP'],axis = 1)
dbmf.fillna(0)
dbmf.isna().sum() #there are too many null values that should be removed from dataset
#dropping null values:
dbmf.dropna(inplace = True, how= "any")
dbmf.head()
dbmf["MVP"].value_counts(normalize=True)
dbmf.to_csv("MVPAward_Data.csv")
# filtering data from 1950 to 2017; 
# also trying to remove irrelevant values to balance the data as much as possible:
dbmf_flt = dbmf[(dbmf["yearID"]>= 1950)  & (dbmf["yearID"]<= 2017) & (dbmf["BA"] >= 0.267) & (dbmf["SLG"] >= 0.4889) & (dbmf["OBP"] >= 0.3441) & (dbmf["HR"] >= 2)].reset_index(drop = True)
dbmf_flt["MVP"].value_counts(normalize=True)
dbmf["MVP"].value_counts(normalize=True).plot(kind='bar');
plt.xlabel("MVP", labelpad=14)
plt.ylabel("Portion", labelpad=14)
plt.title(f"Portion of MVP awards receivers/none-receivers \n'Raw Data'", y=1.02);
dbmf_flt.to_csv("MVPAward_Data_Filtered.csv")
dbmf_flt["MVP"].value_counts(normalize=True).plot(kind='bar');
plt.xlabel("MVP", labelpad=14)
plt.ylabel("Portion", labelpad=14)
plt.title(f"Portion of MVP awards receivers/none-receivers \n'Raw Data'", y=1.02);<jupyter_output><empty_output><jupyter_text>Dataset is balanced!  spliting the data:<jupyter_code>X = dbmf_flt[["BA" ,"OBP","SLG", "HR", "RBI","SB"]]
y = dbmf_flt[["MVP"]]

x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.2,random_state = 0)<jupyter_output><empty_output><jupyter_text>Logistic Regression:<jupyter_code>lr = linear_model.LogisticRegression()
lr.fit(x_train,y_train)
lr.coef_
lr.intercept_
lr_train_pred = lr.predict(x_train)
lr_test_pred = lr.predict(x_test)
metrics.accuracy_score(y_train,lr_train_pred), metrics.accuracy_score(y_test,lr_test_pred)
#confusion matrix plot:
test_cm = metrics.confusion_matrix(y_test,lr_test_pred)
print(classification_report(y_test,lr_test_pred));
print(test_cm);
plt.matshow(test_cm);
plt.title("Confusion Matrix");
plt.colorbar();
plt.ylabel("True Label");
plt.xlabel("Predicted Label");
plt.figure();<jupyter_output><empty_output><jupyter_text>Naïve Bayes<jupyter_code>NB = GaussianNB()
NB.fit(x_train,y_train)
NB_train_pred = NB.predict(x_train)
NB_test_pred = NB.predict(x_test)
metrics.accuracy_score(y_train,NB_train_pred)
metrics.accuracy_score(y_test,NB_test_pred)
print(classification_report(y_test,NB_test_pred))
#confusion matrix:
test_cm = metrics.confusion_matrix(y_test,NB_test_pred)
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.3, random_state = 0)
DT = tree.DecisionTreeClassifier(max_depth = 10, min_samples_split = 5)

#-------------------
DT.fit(x_train,y_train)
DT_pred = DT.predict(x_test)
test_cm = metrics.confusion_matrix(y_test,DT_pred)
print(classification_report(y_test,DT_pred))
#confusion matrix:
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");
print(test_cm);
#importance of each value:
fi = DT.feature_importances_
pd.DataFrame({'variable':x_test.columns,
             'importance': fi })<jupyter_output><empty_output><jupyter_text>Nerual Network<jupyter_code>scaler = MinMaxScaler()
X = dbmf_flt[["BA" ,"OBP","SLG", "HR", "RBI","SB"]]
y = dbmf_flt[["MVP"]]


x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.3, random_state = 0)
# #-------------------
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.fit_transform(x_test)
NN = MLPClassifier(solver='lbfgs',alpha=0.5,
                   hidden_layer_sizes=(10,4),random_state=1)
NN.fit(x_train_scaled,y_train)
#prediction model:
NN_pred = NN.predict(x_test_scaled)
metrics.accuracy_score(NN_pred, y_test)
print(classification_report(y_test,NN_pred))
test_cm = metrics.confusion_matrix(y_test,NN_pred)
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");
test_cm = metrics.confusion_matrix(y_test,NN_pred)
print(test_cm)<jupyter_output><empty_output><jupyter_text>Support Vector Machine<jupyter_code>scaler = MinMaxScaler()
x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.8, random_state = 0)
# -------------------
x_train_scaled = scaler.fit_transform(x_train)
x_test_scaled = scaler.fit_transform(x_test)
#SVM Linear:
svm_linear = svm.SVC(kernel='linear')
svm_linear.fit(x_train_scaled,y_train)
svm_linear.coef_
svm_linear_pred = svm_linear.predict(x_test_scaled)
metrics.accuracy_score(y_test,svm_linear_pred)

test_cm = metrics.confusion_matrix(y_test,svm_linear_pred)
plt.figure()
plt.matshow(test_cm)
plt.title("Confusion Matrix")
plt.colorbar()
plt.ylabel("True Label")
plt.xlabel("Predicted Label");
print(classification_report(y_test,svm_linear_pred))
print(test_cm)
#All other SVM methods (RBF, Poly) gave the same results, so I just put the Linear method in the report:<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>x_train, x_test, y_train, y_test = train_test_split (X,y, test_size = 0.2, random_state = 0)
from sklearn.ensemble import RandomForestClassifier
RF = RandomForestClassifier(n_estimators=100,random_state=0)
RF.fit(x_train,y_train)
RF_pred = RF.predict(x_test)
metrics.accuracy_score(RF_pred,y_test)
print(classification_report(y_test,RF_pred))
fi = RF.feature_importances_
pd.DataFrame({'variable':x_test.columns,
             'importance': fi })
from sklearn.model_selection import cross_val_score
n_estimator = range(2,100,2)
accuracy = []
for i in n_estimator:
    RF = RandomForestClassifier(n_estimators = i, random_state = 0)
    scores = cross_val_score(RF,x_train,y_train)
    accuracy.append(scores.mean());
fig = plt.figure();
ax0 = fig.add_subplot(111)
ymax = max(accuracy)
xpos = accuracy.index(ymax)
xmax = n_estimator[xpos]
plt.plot(n_estimator,accuracy);
plt.xlabel("n_estimator");
plt.ylabel("Accuracy");
plt.title("Accuracy vs. n_estimator");
txt = f"max accuracy:{round(ymax,12)}"
ax0.set_ylim(0.4,0.9);
ax0.annotate(txt, xy=(xmax, ymax), xytext=(xmax, ymax+0.03),
arrowprops=dict(facecolor='blue', shrink=0.05),);<jupyter_output><empty_output><jupyter_text>AdaBoost Classifier<jupyter_code>from sklearn.ensemble import AdaBoostClassifier
n_estimator = list(range(1,100,2))
accuracy = []
for i in n_estimator:
    Ada = AdaBoostClassifier(n_estimators = i, learning_rate=0.005, random_state=21)
    scores = cross_val_score(Ada,x_train,y_train)
    accuracy.append(scores.mean())
fig = plt.figure();
ax = fig.add_subplot(111)
plt.plot(n_estimator,accuracy)
plt.xlabel("n_estimators")
plt.ylabel("Accuracy")
plt.title("Adaboost Accuracy");
ymax = max(accuracy)
xpos = accuracy.index(ymax)
xmax = n_estimator[xpos]
txt = f"max accuracy:{round(ymax,3)}"
ax.annotate(txt, xy=(xmax, ymax), xytext=(xmax, ymax+0.0029),
arrowprops=dict(facecolor='red', shrink=1),)
ax.set_ylim(0.100,1.00);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15328076974475477, 'fraction_numerical': 0.023163615305804266, 'mean_word_length': 6.108929702343255, 'pattern_counts': {'":': 3, '<': 89, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '447139', 'license': 'MIT', 'n_tokens_mistral': 9257, 'n_tokens_neox': 8636, 'n_words': 1666, 'path': 'projects/Baseball Project/PythonCode_.ipynb', 'repo_name': 'afshin-arab/afshin-arab.github.io', 'size': '22464'}