text_html_output_1.png image_output_1.png
from scipy import stats

import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

games = pd.read_csv('../input/nfl-big-data-bowl-2021/games.csv')
players = pd.read_csv('../input/nfl-big-data-bowl-2021/players.csv')
plays = pd.read_csv('../input/nfl-big-data-bowl-2021/plays.csv')
week_1 = pd.read_csv('../input/nfl-big-data-bowl-2021/week1.csv')
week_2 = pd.read_csv('../input/nfl-big-data-bowl-2021/week2.csv')
week_3 = pd.read_csv('../input/nfl-big-data-bowl-2021/week3.csv')
week_4 = pd.read_csv('../input/nfl-big-data-bowl-2021/week4.csv')
week_5 = pd.read_csv('../input/nfl-big-data-bowl-2021/week5.csv')
week_6 = pd.read_csv('../input/nfl-big-data-bowl-2021/week6.csv')
week_7 = pd.read_csv('../input/nfl-big-data-bowl-2021/week7.csv')
week_8 = pd.read_csv('../input/nfl-big-data-bowl-2021/week8.csv')
week_9 = pd.read_csv('../input/nfl-big-data-bowl-2021/week9.csv')
week_10 = pd.read_csv('../input/nfl-big-data-bowl-2021/week10.csv')
week_11 = pd.read_csv('../input/nfl-big-data-bowl-2021/week11.csv')
week_12 = pd.read_csv('../input/nfl-big-data-bowl-2021/week12.csv')
week_13 = pd.read_csv('../input/nfl-big-data-bowl-2021/week13.csv')
week_14 = pd.read_csv('../input/nfl-big-data-bowl-2021/week14.csv')
week_15 = pd.read_csv('../input/nfl-big-data-bowl-2021/week15.csv')
week_16 = pd.read_csv('../input/nfl-big-data-bowl-2021/week16.csv')
week_17 = pd.read_csv('../input/nfl-big-data-bowl-2021/week17.csv')

all_col = list(games.columns) + list(plays.columns) + list(players.columns) + list(week_1.columns)

week_1 = week_1[(week_1['event'] == 'ball_snap') & (week_1['position'] == 'CB')]

week1 = pd.merge(week_1, plays, how='inner', on=['gameId', 'playId'])

def calculate_distance(absYdline, x):
    distance = abs(absYdline - x)
    return distance

week1['distance'] = week1.apply(lambda y: calculate_distance(y['absoluteYardlineNumber'], y['x']), axis=1)

week1 = week1[week1['distance'] <= 20]
week1 = week1.drop_duplicates(subset=['playId'])

x = week1['distance']
y = week1['offensePlayResult']
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
plt.figure(figsize=(20, 10))
plt.plot(x, y, '.')
plt.plot(x, slope * x + intercept, '#EB6E1F')
x_list = x.tolist()
y_list = y.tolist()
plt.xlabel('distance')
plt.ylabel('yards gained by offense')
plt.show()
summary_frame = pd.DataFrame(index=['slope', 'intercept', 'r_value', 'p_value', 'std_err'])
summary_stats = ['{:.7f}'.format(slope), '{:.7f}'.format(intercept), '{:.7f}'.format(r_value), '{:.7f}'.format(p_value), '{:.7f}'.format(std_err)]
summary_frame['summary stats'] = summary_stats
summary_frame