<jupyter_start><jupyter_text>Columns for batting- Rk -- Rank. This is a count of the rows from top to bottom. It is recalculated following the sorting of a column.- Pos -- Position- Name -- Player Name. Bold can mean player is active for this team or player has appeared in MLB * means LHP or LHB,  means switch hitter, + can mean HOFer.- Age -- Player’s age at midnight of June 30th of that year- G -- Games Played This includes all times that the player appeared on the lineup card. Pitchers in non-DH games that appeared on the lineup card but didn't bat will still have a game in this column.- PA -- Plate Appearances When available, we use actual plate appearances from play-by-play game accounts. Otherwise estimated using AB + BB + HBP + SF + SH, which excludes catcher interferences. When this color click for a summary of each PA.- AB -- At Bats- R -- Runs Scored/Allowed- H -- Hits/Hits Allowed- 2B -- Doubles Hit/Allowed- 3B -- Triples Hit/Allowed- HR -- Home Runs Hit/Allowed- RBI -- Runs Batted In- SB -- Stolen Bases- CS -- Caught Stealing- BB -- Bases on Balls/Walks- SO -- Strikeouts- BA -- Hits/At Bats For recent years, leaders need 3.1 PA per team game played. Bold indicates highest BA using current stats. Gold means awarded title at end of year.- OBP -- (H + BB + HBP)/(At Bats + BB + HBP + SF) For recent years, leaders need 3.1 PA per team game played- SLG -- Total Bases/At Bats or (1B + 2*2B + 3*3B + 4*HR)/AB For recent years, leaders need 3.1 PA per team game played- OPS -- On-Base + Slugging Percentages For recent years, leaders need 3.1 PA per team game played- OPS+ -- OPS+ 100*[OBP/lg OBP + SLG/lg SLG - 1] Adjusted to the player’s ballpark(s)- TB -- Total Bases Singles + 2 x Doubles + 3 x Triples + 4 x Home Runs.- GDP -- Double Plays Grounded Into Only includes standard 6-4-3, 4-3, etc. double plays. First tracked in 1933. For gamelogs only in seasons we have play-by-play, we include triple plays as well. All official seasonal totals do not include GITP's.- HBP -- Times Hit by a Pitch.- SH -- Sacrifice Hits (Sacrifice Bunts)- SF -- Sacrifice Flies First tracked in 1954.- IBB -- Intentional Bases on Balls First tracked in 1955.<jupyter_code>#Selecting main statistics columns and Renaming them for easier interpretation:
batting = batting[['Pos', 'Name', 'G', 'PA', 'AB', 'R', 'H', '2B', '3B', 'HR', 'SB', 'CS', 'BB', 'SO']]
col_ren = ['position', 'name', 'games_played', 'plate_appearances', 'at bats', 'runs_made', 'hits_made', 
           'doubles-made','triples-made','home_runs-made','stolen_bases','caught_stealing','base_on_balls','strikes_out']
batting.columns = col_ren
batting.head()
#For pitching
pitching.head()<jupyter_output><empty_output><jupyter_text>Pitching Columns- Rk -- Rank This is a count of the rows from top to bottom. It is recalculated following the sorting of a column.- Pos -- Position- Name -- Player Name Bold can mean player is active for this team or player has appeared in MLB * means LHP or LHB,  means switch hitter, + can mean HOFer.- Age -- Player’s age at midnight of June 30th of that year- W -- Wins- L -- Losses- W-L% -- Win-Loss Percentage- W / (W + L) For players, leaders need one decision for every ten team games. For managers, minimum to qualify for leading is 320 games.- ERA -- 9 * ER / IP For recent years, leaders need 1 IP per team game played. Bold indicates lowest ERA using current stats Gold means awarded ERA title at end of year.- G -- Games Played or Pitched- GS -- Games Started- GF -- Games Finished- CG -- Complete Game- SHO -- Shutouts No runs allowed and a complete game.- SV -- Saves- IP -- Innings Pitched- H -- Hits/Hits Allowed- R -- Runs Scored/Allowed- ER -- Earned Runs Allowed- HR -- Home Runs Hit/Allowed- BB -- Bases on Balls/Walks- IBB -- Intentional Bases on Balls First tracked in 1955.- SO -- Strikeouts- HBP -- Times Hit by a Pitch.- BK -- Balks- WP -- Wild Pitches- BF -- Batters Faced- ERA+ -- ERA+100*[lgERA/ERA] Adjusted to the player’s ballpark(s).- FIP -- Fielding Independent Pitching this stat measures a pitcher's effectiveness at preventing HR, BB, HBP and causing SO(13*HR + 3*(BB+HBP) - 2*SO)/IP + Constantlg The constant is set so that each season MLB average FIP is the same as the MLB avg ERA- WHIP -- (BB + H)/IP For recent years, leaders need 1 IP per team game played- H9 -- 9 x H / IP For recent years, leaders need 1 IP per team game played- HR9 -- 9 x HR / IP For recent years, leaders need 1 IP per team game played- BB9 -- 9 x BB / IP For recent years, leaders need 1 IP per team game played- SO9 -- 9 x SO / IP For recent years, leaders need 1 IP per team game played- SO/W -- SO/W or SO/BB For recent years, pitching leaders need 1 IP per team game played.No batting leaders computed.<jupyter_code>#Getting the main statistics and renaming the columns
pitching = pitching[['Name', 'W', 'L', 'G', 'GS', 'GF', 'CG', 'SHO', 'SV', 'IP', 'H', 'R', 'ER', 'HR', 'BB', 'SO', 'HBP','BK', 'WP', 'BF']]
col_ren = ['name', 'wins', 'losses','games_pitched', 'games_started','games_finished', 'complete_game','shutouts','saves','innings_pitched','hits-allowed',
          'runs-allowed','earned_runs-allowed', 'home_runs-allowed','base_on_balls-allowed','strikeouts','times_hit_by_pitch','balks','wild_pitches','batters_faced']
pitching.columns = col_ren
pitching.head()
#Creating the main data set using player name as primary key
df = pd.merge(pd.merge(man40, batting, left_on='name', right_on='name', how='left'), pitching, left_on='name', right_on='name', how='left')
df.shape
df.head()
df.columns
#Checking of missing values
plt.figure(figsize=(20,10))
df.isna().sum()
#Missing values processing
#Replace all missing values with zero
df.fillna(0,axis=0,inplace=True)
#Convert numerical values into integers
def change_num_type(dtf):    
    for c in list(dtf.columns):
        try:
            dtf[c] = dtf[c].astype(int)
        except:
            pass
change_num_type(df)
# Checking for correlations
# Cheacking for correlation:
# Numeric variables correlation
cmap = sns.diverging_palette(13, 13, s=90, n=5, as_cmap=True)
fig, ax = plt.subplots(figsize=(30,10))
style.use('ggplot')
corr = df.corr()
mask = np.triu(np.ones_like(corr, dtype=np.bool))
ax = sns.heatmap(corr,mask=mask, annot=True, 
            cmap=cmap, vmin = -1, vmax = 1)
plt.title("Numerical Features Correlation Matrix", loc="left", fontsize=14)
plt.show()
df.head()
df['age'].hist(edgecolor='k')
df['country'].value_counts().plot.bar()
df['batting_side'].value_counts().plot.bar()
df['batters_faced']
df['runs-allowed']
sns.jointplot(x='height', y='weight', data =df)
df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12596284549161757, 'fraction_numerical': 0.014952424105120073, 'mean_word_length': 3.7777777777777777, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '458386', 'license': 'MIT', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2070, 'n_words': 874, 'path': 'tests/mlb_current_roster.ipynb', 'repo_name': 'jm1988/baseball-runs-prediction', 'size': '6621'}