<jupyter_start><jupyter_text>Quick Classification Model Predicting if I would win a game on Chess.com<jupyter_code>from pandas import read_csv
from pandas import get_dummies as gd
from pandas.plotting import scatter_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectPercentile, f_classif<jupyter_output><empty_output><jupyter_text>Step  1: Load Dataset - Remove all games that ended in a draw<jupyter_code>df = read_csv('../data/use_for_predictions.csv')
df = df[df['result'] != 0.5]<jupyter_output><empty_output><jupyter_text>Step 2: Inspect the data - Remove any rows missing values - Create dummy columns for categorical values<jupyter_code>df.describe()
df.info()
df.dropna(inplace=True)
df = gd(df, prefix='gt', drop_first=True, columns=['game_time'])
scatter_matrix(df, alpha=0.25, diagonal='kde', grid=True, figsize=(20,20))
df.corr()<jupyter_output><empty_output><jupyter_text>Step 3: Get X & y Values from the dataset - Round 1: Tested on all the following X values:     - Difference in ELO Rating     - Opponents ELO Rating     - My ELO rating     - Game Length     - Assigned Color     - Start Time     - Day of the Month     - Day of the Week     - Game Number of the Day      - Dropped following columns based on p-value and feature importance:     - Day of the Month     - Day of the Week     - Game Number of the Day     - Start Time     - My ELO rating<jupyter_code>y = df['result'].values
X = df.drop(columns=['result', 'day', 'day_game_num', 'weekday', 'elo', 'start_time']).values<jupyter_output><empty_output><jupyter_text>Step 4: Get Training and Test Split, Scale Values<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05, random_state=5, shuffle=False)
mmx = StandardScaler().fit(X_train)
X_train = mmx.transform(X_train)
X_test = mmx.transform(X_test)<jupyter_output><empty_output><jupyter_text>Step 5: Use F Classifier to check for p-values- Go back and remove any columns where p-value is high<jupyter_code>fs = SelectPercentile(f_classif, 10).fit(X, y)
print(fs.pvalues_)
print(fs.scores_)<jupyter_output>[1.26919956e-68 2.43730194e-41 6.62896057e-03 3.83763512e-05
 2.82107231e-06]
[326.04166139 188.11362772   7.38314543  17.01075818  22.03500758]<jupyter_text>Step 5: Start with Random Forest Ensemble Model - Fit the data - Predict values<jupyter_code>model = RandomForestClassifier(n_estimators=500, max_depth=10,
                               n_jobs=-1, random_state=5)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_pred_prob = model.predict_proba(X_test)<jupyter_output><empty_output><jupyter_text>Step 6: Inspect Results<jupyter_code>print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
print(round(roc_auc_score(y_test, y_pred_prob[:, 1])*100, 2))<jupyter_output>precision    recall  f1-score   support

         0.0       0.64      0.90      0.75        63
         1.0       0.84      0.49      0.62        63

   micro avg       0.70      0.70      0.70       126
   macro avg       0.74      0.70      0.69       126
weighted avg       0.74      0.70      0.69       126

[[57  6]
 [32 31]]
74.45<jupyter_text>Step 8: Check Feature Importance<jupyter_code>model.feature_importances_<jupyter_output><empty_output><jupyter_text>Step 9: Try Another Logistic Model - Logistic Regression<jupyter_code>model = LogisticRegression(solver='lbfgs', max_iter=500, n_jobs=-1, random_state=5)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_pred_prob = model.predict_proba(X_test)
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
print(round(roc_auc_score(y_test, y_pred_prob[:, 1])*100, 2))<jupyter_output>precision    recall  f1-score   support

         0.0       0.68      0.62      0.65        63
         1.0       0.65      0.71      0.68        63

   micro avg       0.67      0.67      0.67       126
   macro avg       0.67      0.67      0.67       126
weighted avg       0.67      0.67      0.67       126

[[39 24]
 [18 45]]
80.07<jupyter_text>Step 10: Try Another Logistic Model - Stochastic Gradient Descent Classifier<jupyter_code>model = SGDClassifier(loss='log', max_iter=100000, tol=1e-3,
                      n_jobs=-1, random_state=5)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_pred_prob = model.predict_proba(X_test)
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
print(round(roc_auc_score(y_test, y_pred_prob[:, 1])*100, 2))<jupyter_output>precision    recall  f1-score   support

         0.0       0.72      0.65      0.68        63
         1.0       0.68      0.75      0.71        63

   micro avg       0.70      0.70      0.70       126
   macro avg       0.70      0.70      0.70       126
weighted avg       0.70      0.70      0.70       126

[[41 22]
 [16 47]]
80.42
{'fraction_non_alphanumeric': 0.09953434225844005, 'fraction_numerical': 0.07644547923942568, 'mean_word_length': 3.3797790994052677, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '295003', 'license': 'Unlicense', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1827, 'n_words': 431, 'path': 'src/quick_classification_model.ipynb', 'repo_name': 'Data-is-life/lots-of-data', 'size': '5154'}