1
\$\begingroup\$

The project: create a model that can (somewhat) accurately predict a win/loss given prior game stats. Wanted a review of code in general, in particular my use of the VotingClassifier. In the end, I get an accuracy score of about 87.5%.

from __future__ import print_function, division
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns ; sns.set()
import numpy as np 
from sklearn import cross_validation, linear_model, metrics,    preprocessing, ensemble, svm, neighbors
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV

data = pd.read_excel('data/ncsu-bball-stats.xlsx', index_col=0)

data.drop('season', axis=1, inplace=True)

# Set the exploratory features and the classification target (a win or a loss)
y = data.target
X = data[['fg_att', 'fg_made', 'fg_prcnt', '3pt_att', '3pt_prcnt', 'ft_att', 'ft_made', 'ft_prcnt', 'reb', 'ast', 'opp_bpi']]

# Preliminary visualization: Visualize the relationship between the      exploratory features and points scored
plt.title('NC State Basketball Stats')
plt.xlabel('Stats')
plt.ylabel('Points Scored')
plt.scatter(X['fg_att'], data.pts, c='#ff9900', edgecolors='None', label='Shots Attempted', alpha=0.7)
plt.scatter(X['fg_made'], data.pts, c='#3399ff', edgecolors='None', label='Shots Made', alpha=0.7)
plt.scatter([i * 100 for i in X['fg_prcnt']], data.pts, c='#ff0066', edgecolors='None', label='Shooting Percentage', alpha=0.7)
plt.scatter(X['ft_made'], data.pts, c='#66ff99', edgecolors='None', label='Assists', alpha=0.7)
plt.scatter(X['reb'], data.pts, c='#ffff00', edgecolors='None', label='Rebounds', alpha=0.7)
plt.axis([0, 100, 0, 100])
plt.legend(loc='lower right')
plt.show()

# Shuffle and split the data into training and testing sets
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, random_state=42, test_size=0.1)

# Use the default Logistic Regression model to establish a baseline accuracy score
regressor = linear_model.LogisticRegression()
regressor.fit(X_train, y_train)
predictions = regressor.predict(X_test)

print('Baseline:', metrics.accuracy_score(y_test, predictions), metrics.classification_report(y_test, predictions), '\n')

# Because some features are larger integers, and some are floating point numbers, a scaler needs to be used on the features 
scaler = preprocessing.StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Created a function that could get all the data that I would want to know, should I want to try various classifiers
def accuracy(classifier):
    classifier.fit(X_train, y_train)
    predictions = classifier.predict(X_test)
    print('True values:', y_test.tolist())
    print('Predictions:', predictions.tolist())
    predict_proba = classifier.predict_proba(X_test)
    accuracy_score = metrics.accuracy_score(y_test, predictions)
    print('Accuracy score: %.2f' % (accuracy_score))
    print('\n')
    classification_report = metrics.classification_report(y_test, predictions)
    print('Report:', classification_report)
    confusion_matrix = metrics.confusion_matrix(y_test, predictions)
    print('Confusion matrix:', confusion_matrix)

    # Visualize the confusion matrix
    plt.matshow(confusion_matrix)
    plt.xlabel('Predicted Outcomes')
    plt.ylabel('Actual Outcomes')
    plt.show()

    # See how the model perfroms against random guessing, by visualizing the ROC Curve
    false_positive_rate, recall, threshold = metrics.roc_curve(y_test, predict_proba[:, 1])
    roc_auc = metrics.auc(false_positive_rate, recall)

    # Visualize the classifier's fallout (false positive rate) in relation to its recall
    plt.title('Receiver Operating Characteristic')
    plt.plot(false_positive_rate, recall, c='#7da7d9', label='AUC = %.2f' % roc_auc)
    plt.plot([0, 1], [0, 1], c='#ff6666', linestyle='dashed', label='Random Guesser')
    plt.xlabel('Fallout')
    plt.ylabel('Recall')
    plt.legend(loc='lower right')
    plt.show()

    cross_val_score = cross_validation.cross_val_score(classifier, X_train, y_train, cv=5, scoring='f1')
    print('Cross val score: %.2f (+/-) %.2f' % (cross_val_score.mean(), cross_val_score.std()))
    print('\n')

    return round(accuracy_score, 2), classification_report, confusion_matrix

accuracy(linear_model.LogisticRegression())


clf_1 = linear_model.LogisticRegression(random_state=42)
clf_2 = linear_model.LogisticRegressionCV(random_state=42)
e_clf = ensemble.VotingClassifier(estimators=[('lr', clf_1), ('lrcv',    clf_2)], voting='hard')
e_clf.fit(X_train, y_train)
predictions = e_clf.predict(X_test)
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! Could you add example data so we can see the code in action? \$\endgroup\$ – Mast Jan 11 '16 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.