Kaggle competition: Two Sigma Connect: Rental Listing Inquiries
<SOS> metric: MulticlassLoss Kaggle data source: two-sigma-connect:-rental-listing-inquiries<choose_model_class>
import os
import sys
import operator
import numpy as np
import pandas as pd
from scipy import sparse
import xgboost as xgb
from sklearn import model_selection, preprocessing, ensemble
from sklearn.metrics import log_loss
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
knn_prams = [{'n_neighbors': [1 , 2, 3, 4, 5, 6, 7],
'metric': ['minkowski'],
'p': [2]}]
grid_search_KNN = GridSearchCV(estimator = classifier_KNN,
param_grid = knn_prams,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
 grid_search_KNN.fit(X_train, y_train)
 best_accuracy_KNN = grid_search_KNN.best_score_
best_parameters_KNN = grid_search_KNN.best_params_
print("Best Accuracy: {:.2f} %".format(best_accuracy_KNN*100))
print("Best Parameters:", best_parameters_KNN )<train_model>
def runXGB(train_X, train_y, test_X, test_y=None, feature_names=None, seed_val=0, num_rounds=1000):
param = {}
param['objective'] = 'multi:softprob'
param['eta'] = 0.1
param['max_depth'] = 6
param['silent'] = 1
param['num_class'] = 3
param['eval_metric'] = "mlogloss"
param['min_child_weight'] = 1
param['subsample'] = 0.7
param['colsample_bytree'] = 0.7
param['seed'] = seed_val
num_rounds = num_rounds
plst = list(param.items())
 xgtrain = xgb.DMatrix(train_X, label=train_y)
 if test_y is not None:
xgtest = xgb.DMatrix(test_X, label=test_y)
 watchlist = [(xgtrain,'train'),(xgtest, 'test')]
model = xgb.train(plst, xgtrain, num_rounds, watchlist, early_stopping_rounds=20)
 else:
xgtest = xgb.DMatrix(test_X)
 model = xgb.train(plst, xgtrain, num_rounds)
 pred_test_y = model.predict(xgtest)
 return pred_test_y, model
classifier_KNN = KNeighborsClassifier(n_neighbors = 2,
metric = 'minkowski',
p = 2)
 classifier_KNN.fit(X_train, y_train)
 y_pred_KNN = classifier_KNN.predict(X_test)
 accuracy_score(y_test, y_pred_KNN )<compute_train_metric>
data_path = ".. /input/"
train_file = data_path + "train.json"
test_file = data_path + "test.json"
train_df = pd.read_json(train_file)
 test_df = pd.read_json(test_file)
 print(train_df.shape)
 print(test_df.shape )
classifier_SVM = SVC(kernel = 'linear',
random_state = r_state)
 classifier_SVM.fit(X_train, y_train)
 y_pred_SVM = classifier_SVM.predict(X_test)
 accuracy_score(y_test, y_pred_SVM )<choose_model_class>
features_to_use = ["bathrooms", "bedrooms", "latitude", "longitude", "price"]
svm_params = [{'C': [0.25, 0.5, 0.75, 0.85, 1.0],
'kernel': ['rbf'],
'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]
grid_search_SVM = GridSearchCV(estimator = classifier_SVM,
param_grid = svm_params,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
 grid_search_SVM.fit(X_train, y_train)
 best_accuracy_SVM = grid_search_SVM.best_score_
best_parameters_SVM = grid_search_SVM.best_params_
print("Best Accuracy: {:.2f} %".format(best_accuracy_SVM*100))
print("Best Parameters:", best_parameters_SVM)
 classifier_KSVM = SVC(C = 0.75,
kernel = 'rbf',
random_state = r_state,
gamma = 0.3)
 classifier_KSVM.fit(X_train, y_train)
 y_pred_KSVM = classifier_KSVM.predict(X_test)
 accuracy_score(y_test, y_pred_KSVM )<predict_on_test>
train_df["num_photos"] = train_df["photos"].apply(len)
 test_df["num_photos"] = test_df["photos"].apply(len)
 train_df["num_features"] = train_df["features"].apply(len)
 test_df["num_features"] = test_df["features"].apply(len)
 train_df["num_description_words"] = train_df["description"].apply(lambda x: len(x.split(" ")))
 test_df["num_description_words"] = test_df["description"].apply(lambda x: len(x.split(" ")))
 train_df["created"] = pd.to_datetime(train_df["created"])
 test_df["created"] = pd.to_datetime(test_df["created"])
 train_df["created_year"] = train_df["created"].dt.year
test_df["created_year"] = test_df["created"].dt.year
train_df["created_month"] = train_df["created"].dt.month
test_df["created_month"] = test_df["created"].dt.month
train_df["created_day"] = train_df["created"].dt.day
test_df["created_day"] = test_df["created"].dt.day
train_df["created_hour"] = train_df["created"].dt.hour
test_df["created_hour"] = test_df["created"].dt.hour
features_to_use.extend(["num_photos", "num_features", "num_description_words","created_year", "created_month", "created_day", "listing_id", "created_hour"] )
classifier_NB = GaussianNB()
classifier_NB.fit(X_train, y_train)
 y_pred_NB = classifier_NB.predict(X_test)
 accuracy_score(y_test, y_pred_NB )<predict_on_test>
categorical = ["display_address", "manager_id", "building_id", "street_address"]
for f in categorical:
if train_df[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(train_df[f].values)+ list(test_df[f].values))
train_df[f] = lbl.transform(list(train_df[f].values))
test_df[f] = lbl.transform(list(test_df[f].values))
features_to_use.append(f )
classifier_DT = DecisionTreeClassifier(criterion = 'gini',
random_state = r_state)
 classifier_DT.fit(X_train, y_train)
 y_pred_DT = classifier_DT.predict(X_test)
 accuracy_score(y_test, y_pred_DT )<train_model>
train_df['features'] = train_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))
test_df['features'] = test_df["features"].apply(lambda x: " ".join(["_".join(i.split(" ")) for i in x]))
print(train_df["features"].head())
 tfidf = CountVectorizer(stop_words='english', max_features=200)
 tr_sparse = tfidf.fit_transform(train_df["features"])
 te_sparse = tfidf.transform(test_df["features"] )
classifier_RF = RandomForestClassifier(n_estimators = 10,
criterion = 'gini',
random_state = r_state)
 classifier_RF.fit(X_train, y_train)
 y_pred_RF = classifier_RF.predict(X_test)
 accuracy_score(y_test, y_pred_RF )<predict_on_test>
train_X = sparse.hstack([train_df[features_to_use], tr_sparse] ).tocsr()
test_X = sparse.hstack([test_df[features_to_use], te_sparse] ).tocsr()
target_num_map = {'high':0, 'medium':1, 'low':2}
train_y = np.array(train_df['interest_level'].apply(lambda x: target_num_map[x]))
print(train_X.shape, test_X.shape )
classifier_XGB = XGBClassifier()
classifier_XGB.fit(X_train.to_numpy() , y_train)
 y_pred_XGB = classifier_XGB.predict(X_test.to_numpy())
 accuracy_score(y_test, y_pred_XGB)
<choose_model_class>
cv_scores = []
kf = model_selection.KFold(n_splits=5, shuffle=True, random_state=2016)
 for dev_index, val_index in kf.split(range(train_X.shape[0])) :
dev_X, val_X = train_X[dev_index,:], train_X[val_index,:]
dev_y, val_y = train_y[dev_index], train_y[val_index]
preds, model = runXGB(dev_X, dev_y, val_X, val_y)
 cv_scores.append(log_loss(val_y, preds))
print(cv_scores)
 break
classifier_GB = GradientBoostingClassifier()
classifier_GB.fit(X_train, y_train)
 y_pred_GB = classifier_GB.predict(X_test)
 accuracy_score(y_test, y_pred_GB )<choose_model_class>
preds, model = runXGB(train_X, train_y, test_X, num_rounds=400)
 out_df = pd.DataFrame(preds)
 out_df.columns = ["high", "medium", "low"]
out_df["listing_id"] = test_df.listing_id.values
out_df.to_csv("xgb_starter2.csv", index=False )
classifier_NN = MLPClassifier(random_state = r_state)
 classifiers = [classifier_LR,
classifier_KNN,
classifier_SVM,
classifier_KSVM,
classifier_NB,
classifier_DT,
classifier_RF,
classifier_XGB,
classifier_GB,
classifier_NN]
classifiers_names = ['Linear Regression',
'KNN',
'SVM',
'Kernel SVM',
'Naive Bayes',
'Decision Tree',
'Random Forest',
'XG Boost',
'Gradient Boost',
'NN']
accuracy_mean = []
for cl in classifiers :
accuracies = cross_val_score(estimator = cl,
X = X_train.to_numpy() ,
y = y_train,
cv = 10)
 accuracy_mean.append(accuracies.mean() *100)
 accuracy_df = pd.DataFrame({'Classifier': classifiers_names,
'Accuracy Mean': accuracy_mean})
 accuracy_df 
<train_model>
out_df.to_csv("xgb_starter2.csv", index=False )