<kaggle_start><data_title>Premier League 2016-2017 and 2017-2018<data_description>The players included in this dataset played in both the 2016-2017 and 2017-2018 seasons<data_name>premier-league-20162017-and-20172018
<code>import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
import os

for dirname, _, filenames in os.walk("/kaggle/input"):
    for filename in filenames:
        print(os.path.join(dirname, filename))
filepath = "/kaggle/input/premier-league-20162017-and-20172018/PremierLeagueData2016-2017and2017-2018.csv"
raw_data = pd.read_csv(filepath, encoding="cp1252")
data = raw_data.loc[1:, :].reset_index(drop=True).copy()
desc = raw_data.loc[0, :].copy()
desc


def preprocess_df(df: pd.DataFrame):
    """
    Preprocess input dataframe - feature engineering.
    Input:
        df -> pandas.DataFrame
    Output:
        df -> pandas.DataFrame (preprocessed)
    """
    pass


df = data.copy()
# drop `Variable`
if "Variable" in df.columns:
    df = df.drop("Variable", axis=1)
# clean column `Nationality`
df.loc[:, "Nationality"] = df["Nationality"].str.replace("\xa0", "")
df.loc[:, "Nationality"] = df["Nationality"].str.replace(" ", "_")
# turn rare nationalities into `Other`
max_count_nats = 3
other_nationalities = (
    df["Nationality"]
    .value_counts()[df["Nationality"].value_counts() <= max_count_nats]
    .index.tolist()
)
df.loc[df["Nationality"].isin(other_nationalities), "Nationality"] = "Other"
# one hot encode nationalities
nationalities_df = pd.get_dummies(df["Nationality"], prefix="NAT").astype(bool)
df = df.drop("Nationality", axis=1)
df = pd.concat([df, nationalities_df], axis=1)
# data = preprocess_df(data)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

target = "Position"
le = LabelEncoder()
y = le.fit_transform(df[target])
X = df.loc[:, df.columns != target]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=2023
)
# le.classes_
# le.inverse_transform([0, 0, 1, 2])
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

# Define a list of transformers to use in the pipeline
transformers = [("scaler", StandardScaler()), ("clf", RandomForestClassifier())]
# Create a classification pipeline
pipeline = Pipeline(transformers)
# Train the pipeline on the training data
pipeline.fit(X_train, y_train)
# Use the pipeline to make predictions on the test data
predictions = pipeline.predict(X_test)
# Create a Confusion Matrix
cm = confusion_matrix(y_test, predictions)
cm
y_hat = le.inverse_transform(predictions)
y_hat
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold

k = 5
cv = StratifiedKFold(n_splits=k, random_state=2023, shuffle=True)
params = {
    "n_estimators": 100,
    "criterion": "gini",
    "max_depth": 8,
    "min_samples_split": 2,
    "min_samples_leaf": 1,
    "min_weight_fraction_leaf": 0.0,
    "max_features": "sqrt",
    "max_leaf_nodes": None,
    "min_impurity_decrease": 0.0,
    "bootstrap": True,
    "oob_score": False,
    "n_jobs": 4,
    "random_state": None,
    "verbose": 0,
    "warm_start": False,
    "class_weight": None,
    "ccp_alpha": 0.0,
    "max_samples": None,
}
clf = RandomForestClassifier(**params)
scores = []
for (train, test), i in zip(cv.split(X, y), range(k)):
    X_train = X.iloc[train]
    y_train = y[train]
    X_test = X.iloc[test]
    y_test = y[test]
    clf.fit(X_train, y_train)
    preds = clf.predict(X_test)
    scores.append(accuracy_score(y_test, preds))
scores = np.array(scores)
np.mean(scores), np.std(scores)
<|endoftext|>
[{"Id": 113664182, "ScriptId": 33162490, "ParentScriptVersionId": NaN, "ScriptLanguageId": 9, "AuthorUserId": 1141571, "CreationDate": "12/13/2022 01:05:22", "VersionNumber": 2.0, "Title": "DSBA2022-23", "EvaluationDate": "12/13/2022", "IsChange": true, "TotalLines": 135.0, "LinesInsertedFromPrevious": 125.0, "LinesChangedFromPrevious": 0.0, "LinesUnchangedFromPrevious": 10.0, "LinesInsertedFromFork": NaN, "LinesDeletedFromFork": NaN, "LinesChangedFromFork": NaN, "LinesUnchangedFromFork": NaN, "TotalVotes": 0}]
[{"Id": 158934569, "KernelVersionId": 113664182, "SourceDatasetVersionId": 193628}]
[{"Id": 193628, "DatasetId": 83417, "DatasourceVersionId": 204764, "CreatorUserId": 2192630, "LicenseName": "Unknown", "CreationDate": "11/27/2018 23:24:10", "VersionNumber": 1.0, "Title": "Premier League 2016-2017 and 2017-2018", "Slug": "premier-league-20162017-and-20172018", "Subtitle": "2016-2017 and 2017-2018 Seasons", "Description": "The players included in this dataset played in both the 2016-2017 and 2017-2018 seasons", "VersionNotes": "Initial release", "TotalCompressedBytes": 80644.0, "TotalUncompressedBytes": 80644.0}]
[{"Id": 83417, "CreatorUserId": 2192630, "OwnerUserId": 2192630.0, "OwnerOrganizationId": NaN, "CurrentDatasetVersionId": 193628.0, "CurrentDatasourceVersionId": 204764.0, "ForumId": 92884, "Type": 2, "CreationDate": "11/27/2018 23:24:10", "LastActivityDate": "11/27/2018", "TotalViews": 2249, "TotalDownloads": 109, "TotalVotes": 3, "TotalKernels": 2}]
import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
import os

for dirname, _, filenames in os.walk("/kaggle/input"):
    for filename in filenames:
        print(os.path.join(dirname, filename))
filepath = "/kaggle/input/premier-league-20162017-and-20172018/PremierLeagueData2016-2017and2017-2018.csv"
raw_data = pd.read_csv(filepath, encoding="cp1252")
data = raw_data.loc[1:, :].reset_index(drop=True).copy()
desc = raw_data.loc[0, :].copy()
desc


def preprocess_df(df: pd.DataFrame):
    """
    Preprocess input dataframe - feature engineering.
    Input:
        df -> pandas.DataFrame
    Output:
        df -> pandas.DataFrame (preprocessed)
    """
    pass


df = data.copy()
# drop `Variable`
if "Variable" in df.columns:
    df = df.drop("Variable", axis=1)
# clean column `Nationality`
df.loc[:, "Nationality"] = df["Nationality"].str.replace("\xa0", "")
df.loc[:, "Nationality"] = df["Nationality"].str.replace(" ", "_")
# turn rare nationalities into `Other`
max_count_nats = 3
other_nationalities = (
    df["Nationality"]
    .value_counts()[df["Nationality"].value_counts() <= max_count_nats]
    .index.tolist()
)
df.loc[df["Nationality"].isin(other_nationalities), "Nationality"] = "Other"
# one hot encode nationalities
nationalities_df = pd.get_dummies(df["Nationality"], prefix="NAT").astype(bool)
df = df.drop("Nationality", axis=1)
df = pd.concat([df, nationalities_df], axis=1)
# data = preprocess_df(data)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

target = "Position"
le = LabelEncoder()
y = le.fit_transform(df[target])
X = df.loc[:, df.columns != target]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=2023
)
# le.classes_
# le.inverse_transform([0, 0, 1, 2])
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

# Define a list of transformers to use in the pipeline
transformers = [("scaler", StandardScaler()), ("clf", RandomForestClassifier())]
# Create a classification pipeline
pipeline = Pipeline(transformers)
# Train the pipeline on the training data
pipeline.fit(X_train, y_train)
# Use the pipeline to make predictions on the test data
predictions = pipeline.predict(X_test)
# Create a Confusion Matrix
cm = confusion_matrix(y_test, predictions)
cm
y_hat = le.inverse_transform(predictions)
y_hat
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold

k = 5
cv = StratifiedKFold(n_splits=k, random_state=2023, shuffle=True)
params = {
    "n_estimators": 100,
    "criterion": "gini",
    "max_depth": 8,
    "min_samples_split": 2,
    "min_samples_leaf": 1,
    "min_weight_fraction_leaf": 0.0,
    "max_features": "sqrt",
    "max_leaf_nodes": None,
    "min_impurity_decrease": 0.0,
    "bootstrap": True,
    "oob_score": False,
    "n_jobs": 4,
    "random_state": None,
    "verbose": 0,
    "warm_start": False,
    "class_weight": None,
    "ccp_alpha": 0.0,
    "max_samples": None,
}
clf = RandomForestClassifier(**params)
scores = []
for (train, test), i in zip(cv.split(X, y), range(k)):
    X_train = X.iloc[train]
    y_train = y[train]
    X_test = X.iloc[test]
    y_test = y[test]
    clf.fit(X_train, y_train)
    preds = clf.predict(X_test)
    scores.append(accuracy_score(y_test, preds))
scores = np.array(scores)
np.mean(scores), np.std(scores)
