<jupyter_start><jupyter_text>Exploratory Data Analysis (EDA)https://data.world/dataquest/mlb-game-logs/workspace/file?filename=game_logs.csv<jupyter_code>#import the useful libraries.
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
#import the useful libraries.
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

sns.set(color_codes=True)
sns.set(rc={'figure.figsize':(10,6)})
#read the file
#https://data.world/dataquest/mlb-game-logs/workspace/file?filename=game_logs.csv

data = pd.read_csv('game_logs.csv')
#print the head of the data frame
data.head()
#print the head of the data frame
data.tail()
# Checking the missing values.  There are no missing values ("additional info" column is an optional field that does not impact EDA
data.isnull().sum()
#Let's calculate the percentage of each  team was played
data.v_name.value_counts(normalize=True)
#plot the pie chart of opponents 
data.v_name.value_counts(normalize=True).plot.pie()
plt.show()
#plot the bar graph of percentage home team wins categories
data.W.value_counts(normalize=True).plot.barh()
plt.show()
data.describe()
#understand data (attendance feature)
data.attendance.describe()
#plot the scatter plot of W (wins) and attendance variable in data.  Overall, the more wins the stronger the attendance.
plt.scatter(data.W,data.attendance)
plt.show()
#plot the scatter plot of wins and game length.  Overall longer games equate to a loss

data.plot.scatter(x="W",y="length_minutes")
plt.show()
#understand data (runs)
data.length_minutes.describe()
#Calculating game time length
data.length_minutes.mean()
df = pd.DataFrame(data, columns = ['W', 'length_minutes'])
print(df)
data.shape
data.describe
#let's plot our data points on 2-D graph to eyeball our dataset and see if we can manually find any relationship between the data.

data.plot(x='length_minutes', y='W', style='o')
plt.title('length_minutes vs Wins')
plt.xlabel('length_minutes')
plt.ylabel('Wins')
plt.show()
#To extract the attributes and labels, execute the following script

X = data.length_minutes.values.reshape((-1, 1))
y = data.W.values
print(X)
#train our algorithm. We have split our data into training and testing sets

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
#train our algorithm (importing library)

from sklearn.linear_model import LinearRegression
#train our algorithm by defining "regressor"

regressor = LinearRegression()
#train our algorithm by creating "fit"
regressor.fit(X_train, y_train)
print(regressor.intercept_)
print(regressor.coef_)
#make  predictions.  Use  test data and see how accurately our algorithm predicts the percentage score

y_pred = regressor.predict(X_test)
#To compare the actual output values for X_test with the predicted values

df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
df
from sklearn import metrics
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print(df)<jupyter_output>Actual  Predicted
0         0   0.022362
1         0   0.461983
2         1   0.552839
3         1   0.491292
4         0   0.453191
..      ...        ...
189       0   0.532323
190       0   0.602662
191       1   0.629040
192       0   0.520600
193       1   0.558700

[194 rows x 2 columns]
{'fraction_non_alphanumeric': 0.1014781125639568, 'fraction_numerical': 0.03212052302444571, 'mean_word_length': 4.759410801963994, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '90145', 'license': 'MIT', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1136, 'n_words': 365, 'path': 'EDA_Baseball_v4.ipynb', 'repo_name': 'dylee95/MLB', 'size': '3518'}