import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('https://jonghank.github.io/ase1302/files/FanGraphs_Leaderboard_2019.csv') df # your code here data = np.array(df) df['FIP'] = (13 * df['HR'] + 3*(df['BB']-df['IBB']+df['HBP']) -2*df['SO'])/df['IP'] + 3.20 plt.figure(figsize=(14,5), dpi=100) plt.stem(df['Name'], df['FIP'], use_line_collection=True) plt.xticks(rotation='vertical') plt.ylabel('FIP') plt.title('Fielding independent pitching') plt.grid() plt.show() AL = ['Yankees', 'Rays', 'Red Sox', 'Blue Jays', 'Orioles', 'Twins', 'Indians', 'White Sox', 'Royals', 'Tigers', \ 'Astros', 'Athletics', 'Rangers', 'Angels', 'Mariners'] NL = ['Braves', 'Nationals', 'Mets', 'Phillies', 'Marlins', 'Cardinals', 'Brewers', 'Cubs', 'Reds', 'Pirates', 'Dodgers', \ 'Diamondbacks', 'Giants', 'Padres', 'Rockies'] # your code here league = np.zeros(df.shape[0]) for i in range(df.shape[0]): if len(set(AL) & set([df['Team'][i]])): league[i] = 1 elif len(set(NL) & set([df['Team'][i]])): league[i] = 2 else: league[i] = 0 df['league'] = league AL_index = np.where(df['league'] == 1) NL_index = np.where(df['league'] == 2) AL_name = df['Name'].values[AL_index] NL_name = df['Name'].values[NL_index] AL_top5 = AL_name[np.argsort(df['FIP'].values[AL_index])[:5]] NL_top5 = NL_name[np.argsort(df['FIP'].values[NL_index])[:5]] print(f'AL top 5 pitchers for FIP: {AL_top5}') print(f'NL top 5 pitchers for FIP: {NL_top5}') # your code here CYP_TT = (df['IP']/2-df['ER']) + df['SO']/10 + df['W'] CYP_ESPN = 5*df['IP']/9 - df['ER'] + df['SO']/12 + 2.5*df['SV'] + df['ShO'] + 6*df['W'] - 2*df['L'] df['CYP_TT'] = CYP_TT df['CYP_ESPN'] = CYP_ESPN AL_TT = AL_name[np.argmax(df['CYP_TT'].values[AL_index])] NL_TT = NL_name[np.argmax(df['CYP_TT'].values[NL_index])] AL_ESPN = AL_name[np.argmax(df['CYP_ESPN'].values[AL_index])] NL_ESPN = NL_name[np.argmax(df['CYP_ESPN'].values[NL_index])] print(f'By Tom Tango`s model, {AL_TT} in AL and {NL_TT} in NL will win the Cy Young Award') print(f'By ESPN`s model, {AL_ESPN} in AL and {NL_ESPN} in NL will win the Cy Young Award') plt.figure(figsize=(14,5), dpi=100) plt.stem(df['Name'], df['CYP_TT'], use_line_collection=True) plt.xticks(rotation='vertical') plt.ylabel('Cy Young Award score (Tom Tango)') plt.title('Cy Young Award prediction by Tom Tango') plt.grid() plt.show() plt.figure(figsize=(14,5), dpi=100) plt.stem(df['Name'], df['CYP_ESPN'], use_line_collection=True) plt.xticks(rotation='vertical') plt.ylabel('Cy Young Award score (ESPN)') plt.title('Cy Young Award prediction by ESPN') plt.grid() plt.show()