Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<prepare_x_and_y>
import pandas as pd
import numpy as np
from sklearn.metrics import cohen_kappa_score,confusion_matrix
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestRegressor
from functools import partial
import scipy as sp
X_train = sparse_merge[:nrow_train]
X_test = sparse_merge[nrow_train:]<compute_test_metric>
df = pd.read_csv('.. /input/train/train.csv' )
def model_testing(model,X_test, y_test):
y_pred = model.predict(X_test)
 error = rmsle(y_test, y_pred)
 print(error)
 def rmsle(y, y0):
assert len(y)== len(y0)
 return np.sqrt(np.mean(np.power(np.log1p(y)-np.log1p(y0), 2)))
<choose_model_class>
dfDropped = df.drop(['Name','RescuerID','Description','PetID','AdoptionSpeed'],axis=1)
 X = dfDropped.values
y = df["AdoptionSpeed"]
ridge_model_1 = Ridge(alpha=5.0, fit_intercept=True, normalize=False, copy_X=True,
max_iter=None, tol=0.001, solver='auto', random_state=None)
 ridge_model_2 = Ridge(alpha=5.0, fit_intercept=True, normalize=False, copy_X=True,
max_iter=None, tol=0.001, solver='sag', random_state=None)
 ridge_model_3 = Ridge(alpha=5.0, fit_intercept=True, normalize=False, copy_X=True,
max_iter=None, tol=0.001, solver='lsqr', random_state=None)
 gbrt = GradientBoostingRegressor(max_depth = 2, n_estimators = 5,
learning_rate = 0.9,subsample=0.9 )<train_model>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = cohen_kappa_score(y, X_p,weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
start_time = time.time()
print("train test splitting...")
 X_t, X_v, y_t, y_v = train_test_split(X_train, y_train,test_size = 0.20)
 print("training model...")
 print("1")
 ridge_model_1.fit(X_train, y_train)
 model_testing(ridge_model_1, X_test = X_v, y_test = y_v)
 print("training model...")
 print("2")
 print("training model...")
 print("3")
 print("TIME:", time.time() - start_time )<save_to_csv>
rfr = RandomForestRegressor(max_depth=10, n_estimators=300)
 rfr.fit(X,y )
def create_submission(model,test = X_test, submission=submission,path="./predictions.csv"):
predictions = model.predict(test)
 predictions = pd.Series(np.exp(predictions)- 1)
 submission['price'] = predictions
submission.to_csv(path, index=False)
 print(submission.describe() )<train_model>
optr = OptimizedRounder()
optr.fit(rfr.predict(X),y )
start_time = time.time()
create_submission(ridge_model_1)
 print("TIME:", time.time() - start_time)
 print("TOTAL TIME:", time.time() - Time_0)
<import_modules>
df_t = pd.read_csv('.. /input/test/test.csv')
 X_t = df_t.drop(['Name','RescuerID','Description','PetID'],axis=1 ).values
<load_from_csv><EOS> 
submission = pd.DataFrame(dict(PetID=df_t['PetID'], AdoptionSpeed=optr.predict(rfr.predict(X_t), optr.coefficients() ).astype(int)))
 submission.to_csv('submission.csv', index=False )