Kaggle competition: NBA Rookies
<SOS> metric: categorizationaccuracy Kaggle data source: nba-rookies<train_model>
import pandas as pd
import numpy as np
import sklearn
from sklearn import preprocessing
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn import svm
import statistics
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
m = RandomForestClassifier(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(df, y)
 print(m.oob_score_ )<compute_test_metric>
t_data = pd.read_csv(".. /input/train.csv")
 ts_data = pd.read_csv(".. /input/test.csv" )
fi = rf_feat_importance(m, df); fi<prepare_x_and_y>
ddata = t_data.drop(["PlayerID","Name"], axis=1)
 sdata = ts_data.drop(["PlayerID","Name"], axis=1 )
df, y, _ = proc_df(df_raw, 'fare_amount', skip_flds=["key"] )<create_dataframe>
ddata = ddata.interpolate()
ddata = ddata.replace([np.inf], np.float64.max)
 ddata = ddata.replace([-np.inf], np.float64.min)
 features = ddata.loc[:, ddata.columns.values[:len(ddata.columns.values)-1]].values
labels = ddata.loc[:, ['TARGET_5Yrs']].values
st_features = preprocessing.StandardScaler().fit_transform(features)
 sdata = sdata.interpolate()
sdata = sdata.replace([np.inf], np.float64.max)
 sdata = sdata.replace([-np.inf], np.float64.min)
 sfeatures = sdata.loc[:, sdata.columns.values].values
st_sfeatures = preprocessing.StandardScaler().fit_transform(sfeatures )
df = df[features_keep].copy()<split>
pca = PCA(n_components=10)
 pca.fit(features)
 principalComponents = pca.transform(features)
 test_principalComponenta = pca.transform(sfeatures)
 print(principalComponents.shape, "
", test_principalComponenta.shape )
X_train, X_valid, y_train, y_valid = train_test_split(df, y, test_size=0.2)
 print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape )<train_model>
testf = sfeatures
%%time
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=False)
 m.fit(X_train, y_train)
 print_score(m )<define_variables>
names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Gaussian Process",
"Decision Tree", "Random Forest", "Neural Net", "AdaBoost",
"Naive Bayes", "QDA"]
classifiers = [
KNeighborsClassifier(3),
SVC(kernel="linear", C=0.025),
SVC(gamma=2, C=1),
GaussianProcessClassifier(1.0 * RBF(1.0)) ,
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
MLPClassifier(alpha=1),
AdaBoostClassifier() ,
GaussianNB() ,
QuadraticDiscriminantAnalysis() ]
res = []
res2 = []
for name, clf in zip(names, classifiers):
clf.fit(inputf, labels)
 res.append(clf.predict(testf))
temp = clf.predict(inputf)
 res2.append(temp)
 print(name, " : ", sklearn.metrics.accuracy_score(labels, temp))
keys = df_test_raw["key"]<feature_engineering>
res2 = np.array(res2)
 res3 = []
for i in range(res2.shape[1]):
s = 0
for j in res2:
s = s + j[i]
if s >= 5:
res3.append(1)
 else:
res3.append(0 )
add_datepart(df_test_raw, 'pickup_datetime', time=True )<feature_engineering>
sklearn.metrics.accuracy_score(labels, res3 )
add_travel_vector_features(df_test_raw )<feature_engineering>
res = np.array(res)
 res3 = []
for i in range(res.shape[1]):
s = 0
for j in res:
s = s + j[i]
if s >= 5:
res3.append(1)
 else:
res3.append(0 )
<predict_on_test><EOS> 
cols = { 'PlayerID': [i+901 for i in range(440)] , 'TARGET_5Yrs': res3 }
submission = pd.DataFrame(cols)
 submission.to_csv("submission.csv", index=False)
 print(submission )