<jupyter_start><jupyter_text>Basic Prediction cheated by mixing the past with the future.  In this example we predict the winners of district matches using the regionals. Load the training data into a pandas data frame.<jupyter_code>import pandas as pd

train_auburn = pd.read_csv('../event_features_2019waahs.txt', sep='\t', header=None, names=['Label','RedAlliance','BlueAlliance'])
train_glacier = pd.read_csv('../event_features_2019wasno.txt', sep='\t', header=None, names=['Label','RedAlliance','BlueAlliance'])

test_districts = pd.read_csv('../event_features_2019pncmp.txt', sep='\t', header=None, names=['Label','RedAlliance','BlueAlliance'])<jupyter_output><empty_output><jupyter_text>Preview the first 10 rows<jupyter_code>train_auburn[0:10]<jupyter_output><empty_output><jupyter_text>We can leverage some concepts from https://stackabuse.com/text-classification-with-python-and-scikit-learn/ to build our model.  The basic idea is to use the team names as features.  Suppose frc492 is a really strong team- when it appears in the RedAlliance column it will add some weight to the probability that Red wins, and vice-versa if it appear in the BlueAlliance column.  So we want to build a predictor that figures out how much it matters when frc492 appears in a column (and the same for any other team).<jupyter_code>from sklearn.feature_extraction.text import CountVectorizer  
from sklearn.pipeline import FeatureUnion
from sklearn.compose import ColumnTransformer

# two count vectorizers. This transforms the alliance lists into vector encodings
redVectorizer = CountVectorizer(max_features=1500, min_df=1, max_df=1.0, stop_words=None)  
blueVectorizer = CountVectorizer(max_features=1500, min_df=1, max_df=1.0, stop_words=None)  

ct = ColumnTransformer([('RedFeatures',redVectorizer,'RedAlliance'), ('BlueFeatures',blueVectorizer,'BlueAlliance')])

# shuffle the data first
train = pd.concat([train_auburn,train_glacier])


#train = train.sample(frac=1.0)

# produce the training features and labels.
X = ct.fit_transform(train)
y = train.Label

Xtest = ct.transform(test_districts)
Ytest = test_districts.Label

#print(X.size, Xtest.size)
X[0,:].todense()<jupyter_output><empty_output><jupyter_text>We have the data in a state where we can start to build models. First we'll try a basic random forest with 100 trees.<jupyter_code>from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators=100, random_state=0, min_samples_split=3)  
classifier
# Run four-fold cross-validation
classifier.fit(X,y)
predictions= classifier.predict(Xtest)
import numpy as np
from sklearn.metrics import accuracy_score
#np.sum(np.abs(scores-Ytest))
accuracy_score(Ytest, predictions)<jupyter_output><empty_output><jupyter_text>Let's also try logistic regression.<jupyter_code>from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial')
classifier.fit(X,y)
predictions= classifier.predict(Xtest)
import numpy as np
from sklearn.metrics import accuracy_score
#np.sum(np.abs(scores-Ytest))
accuracy_score(Ytest, predictions)
classifier.coef_<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>from sklearn.linear_model import RidgeClassifier
classifier = RidgeClassifier()
classifier.fit(X,y)
predictions= classifier.predict(Xtest)
import numpy as np
from sklearn.metrics import accuracy_score
#np.sum(np.abs(scores-Ytest))
accuracy_score(Ytest, predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09954622802041974, 'fraction_numerical': 0.013896766874645491, 'mean_word_length': 7.519323671497585, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '119365', 'license': 'MIT', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1114, 'n_words': 290, 'path': '2019/machinelearning/Predict Districts.ipynb', 'repo_name': 'trc492/FrcDataAnalytics', 'size': '3526'}