Kaggle competition: Santander Customer Satisfaction
<SOS> metric: AUC Kaggle data source: santander-customer-satisfaction<predict_on_test>
%matplotlib inline
results_ens = np.zeros(( X_val2.shape[0],10))
for j in range(nets):
results_ens = results_ens + model[j].predict(X_val2)
 results_ens = np.argmax(results_ens,axis =1 )<compute_test_metric>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train.head()
Y_val2=np.argmax(Y_val2,axis =1)
 ens_acc=accuracy_score(results_ens,Y_val2)
<compute_test_metric>
train['TARGET'].value_counts()
print('Ensemble accuracy: {:.4f}'.format(ens_acc))<save_to_csv>
print(len(train['TARGET'][train.var15<23]))
print(sum(train['TARGET'][train.var15<23]))
results = np.zeros(( test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("predictions.csv",index=False )<set_options>
labels = train['TARGET'].values
df_train = train.drop(['TARGET'], axis=1)
 df_test = test.copy()
id_test = test['ID']
piv_train = train.shape[0]
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "99"<set_options>
df_all = pd.concat(( df_train, df_test), axis=0, ignore_index=True)
 df_all = df_all.drop(['ID'], axis=1)
 print(df_all.isnull().sum() / df_all.shape[0])
 df_all.head()
device_lib.list_local_devices()<set_options>
df_all['n0'] =(df_all==0 ).sum(axis=1)
 print(df_all.shape )
tf.logging.set_verbosity(tf.logging.INFO)
 sess = tf.InteractiveSession()
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 print('Shape of the train data with all features:', train.shape)
 train = train.select_dtypes(exclude=['object'])
 print("")
 print('Shape of the train data with numerical features:', train.shape)
 train.drop('Id',axis = 1, inplace = True)
 train.fillna(0,inplace=True )<load_from_csv>
remove = []
for col in df_all.columns:
if df_all[col].std() == 0:
remove.append(col)
 df_all.drop(remove, axis=1, inplace=True)
 print(df_all.shape )
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 test = test.select_dtypes(exclude=['object'])
 ID = test.Id
test.fillna(0,inplace=True)
 test.drop('Id',axis = 1, inplace = True)
 print("")
 print("List of features contained our dataset:",list(train.columns))<predict_on_test>
constants = []
for f in df_all.columns:
print(len(np.unique(df_all[f])) , '/', len(df_all[f]))
if len(np.unique(df_all[f])) == 1:
constants.append(f)
 display(constants)
 df_all = df_all.drop(constants, axis=1)
 print(df_all.shape )
warnings.filterwarnings('ignore')
 clf = IsolationForest(max_samples = 100, random_state = 42)
 clf.fit(train)
 y_noano = clf.predict(train)
 y_noano = pd.DataFrame(y_noano, columns = ['Top'])
 y_noano[y_noano['Top'] == 1].index.values
train = train.iloc[y_noano[y_noano['Top'] == 1].index.values]
train.reset_index(drop = True, inplace = True)
 print("Number of Outliers:", y_noano[y_noano['Top'] == -1].shape[0])
 print("Number of rows without outliers:", train.shape[0] )<normalization>
print(( df_all.num_var13_medio ).value_counts())
 print(( df_all.saldo_medio_var13_medio_ult1 ).value_counts() )
col_train = list(train.columns)
 col_train_bis = list(train.columns)
 col_train_bis.remove('SalePrice')
 mat_train = np.matrix(train)
 mat_test = np.matrix(test)
 mat_new = np.matrix(train.drop('SalePrice',axis = 1))
mat_y = np.array(train.SalePrice ).reshape(( 1314,1))
prepro_y = MinMaxScaler()
prepro_y.fit(mat_y)
 prepro = MinMaxScaler()
prepro.fit(mat_train)
 prepro_test = MinMaxScaler()
prepro_test.fit(mat_new)
 train = pd.DataFrame(prepro.transform(mat_train),columns = col_train)
 test = pd.DataFrame(prepro_test.transform(mat_test),columns = col_train_bis)
 train.head()<split>
remove += ['num_var13_medio', 'saldo_medio_var13_medio_ult1']
len(remove )
COLUMNS = col_train
FEATURES = col_train_bis
LABEL = "SalePrice"
feature_cols = [tf.contrib.layers.real_valued_column(k)for k in FEATURES]
training_set = train[COLUMNS]
prediction_set = train.SalePrice
x_train, x_test, y_train, y_test = train_test_split(training_set[FEATURES] , prediction_set, test_size=0.33, random_state=42)
 y_train = pd.DataFrame(y_train, columns = [LABEL])
 training_set = pd.DataFrame(x_train, columns = FEATURES ).merge(y_train, left_index = True, right_index = True)
 training_set.head()
training_sub = training_set[col_train]<merge>
df_all.drop(remove, axis=1, inplace=True)
 print(df_all.shape )
y_test = pd.DataFrame(y_test, columns = [LABEL])
 testing_set = pd.DataFrame(x_test, columns = FEATURES ).merge(y_test, left_index = True, right_index = True)
 testing_set.head()<choose_model_class>
print('Setting min-max lims on test data...')
 st = time.time()
for f in df_all.columns[:-1]:
lim_min = np.min(df_train[f])
 df_all.loc[:, f][df_all[f] < lim_min] = lim_min
lim_max = np.max(df_train[f])
 df_all.loc[:, f][df_all[f] > lim_max] = lim_max
print(time.time() - st )
tf.logging.set_verbosity(tf.logging.ERROR)
 regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
activation_fn = tf.nn.relu, hidden_units=[256, 128, 64, 32, 16])
 training_set.reset_index(drop = True, inplace =True )<categorify>
vals = df_all.values
X = vals[:piv_train]
X_test_submit = vals[piv_train:]
y = labels
print(vals.shape)
 print(X.shape)
 print(X_test_submit.shape )
def input_fn(data_set, pred = False):
if pred == False:
feature_cols = {k: tf.constant(data_set[k].values)for k in FEATURES}
labels = tf.constant(data_set[LABEL].values)
 return feature_cols, labels
if pred == True:
feature_cols = {k: tf.constant(data_set[k].values)for k in FEATURES}
return feature_cols<train_model>
df_norm = normalize(df_all, axis=0 )
regressor.fit(input_fn=lambda: input_fn(training_set), steps=2000 )<compute_test_metric>
vals = df_norm
X = vals[:piv_train]
X_test_submit = vals[piv_train:]
y = labels
ev = regressor.evaluate(input_fn=lambda: input_fn(testing_set), steps=1 )<compute_test_metric>
ratio = float(np.sum(y == 1)) / np.sum(y==0)
 est = XGBClassifier(max_depth=5, learning_rate=0.0202, n_estimators=556,
objective='binary:logistic', subsample=0.69, colsample_bytree=0.81,
scale_pos_weight=ratio, seed=1776)
 param = {
'objective':'multi:softprob',
'max_depth':6,
'learning_rate':0.25,
'min_child_weight': 3,
'n_estimators':43,
'subsample':0.6,
'colsample_bytree':0.6,
'num_class' :12
}
loss_score1 = ev["loss"]
print("Final Loss on the testing set: {0:f}".format(loss_score1))<predict_on_test>
eval_set = [(X_train,y_train),(X_valid,y_valid)]
est = XGBClassifier()
est.fit(X_train, y_train, eval_set=eval_set, early_stopping_rounds=100, eval_metric='logloss' )
y = regressor.predict(input_fn=lambda: input_fn(testing_set))
predictions = list(itertools.islice(y, testing_set.shape[0]))<compute_test_metric>
print(metrics.classification_report(y, est.predict(X)) )
def leaky_relu(x):
return tf.nn.relu(x)- 0.01 * tf.nn.relu(-x )<train_model>
ypred = est.predict_proba(X)
 print(metrics.roc_auc_score(y, ypred[:,1]))
regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
activation_fn = leaky_relu, hidden_units=[256, 128, 64, 32, 16])
 regressor.fit(input_fn=lambda: input_fn(training_set), steps=2000)
 ev = regressor.evaluate(input_fn=lambda: input_fn(testing_set), steps=1)
 loss_score2 = ev["loss"]
print("Final Loss on the testing set with Leaky Relu: {0:f}".format(loss_score2))
y_predict = regressor.predict(input_fn=lambda: input_fn(test, pred = True))<train_model>
var15 = test['var15']
saldo_medio_var5_hace2 = test['saldo_medio_var5_hace2']
saldo_var33 = test['saldo_var33']
var38 = test['var38']
var21 = test['var21']
num_var30 = test['num_var30']
num_var13_0 = test['num_var13_0']
num_var33_0 = test['num_var33_0']
imp_ent_var16_ult1 = test['imp_ent_var16_ult1']
imp_op_var39_comer_ult3 = test['imp_op_var39_comer_ult3']
saldo_medio_var5_ult3 = test['saldo_medio_var5_ult3']
regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
activation_fn = tf.nn.elu, hidden_units=[200, 100, 50, 25, 12])
 regressor.fit(input_fn=lambda: input_fn(training_set), steps=2000)
 ev = regressor.evaluate(input_fn=lambda: input_fn(testing_set), steps=1)
 loss_score3 = ev["loss"]<predict_on_test>
ypred_submit = est.predict_proba(X_test_submit)
 print(X_test_submit.shape)
 print(ypred_submit.shape)
 print(ypred_submit[:7] )
print("Final Loss on the testing set with Elu: {0:f}".format(loss_score3))
y_predict = regressor.predict(input_fn=lambda: input_fn(test, pred = True))<load_from_csv>
zero_feats = [('saldo_medio_var5_hace2', 160000), 
('saldo_var33', 0), 
('var38', 3988596), 
('var21', 7500), 
('num_var30', 9), 
('num_var13_0', 6), 
('num_var33_0', 0), 
('imp_ent_var16_ult1', 51003), 
('imp_op_var39_comer_ult3', 13184), 
('saldo_medio_var5_ult3', 108251),
]
for x,y in zero_feats:
print(x, train[(train[x] > y)&(train.TARGET==1)].shape )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.drop('Id',axis = 1, inplace = True)
 train_numerical = train.select_dtypes(exclude=['object'])
 train_numerical.fillna(0,inplace = True)
 train_categoric = train.select_dtypes(include=['object'])
 train_categoric.fillna('NONE',inplace = True)
 train = train_numerical.merge(train_categoric, left_index = True, right_index = True)
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 ID = test.Id
test.drop('Id',axis = 1, inplace = True)
 test_numerical = test.select_dtypes(exclude=['object'])
 test_numerical.fillna(0,inplace = True)
 test_categoric = test.select_dtypes(include=['object'])
 test_categoric.fillna('NONE',inplace = True)
 test = test_numerical.merge(test_categoric, left_index = True, right_index = True )<predict_on_test>
ypred_submit[list(var15.index[var15 < 23]), 1] = 0
ypred_submit[list(saldo_var33.index[saldo_var33 > 0]), 1] = 0
ypred_submit[list(var38.index[var38 > 3988596]), 1] = 0
ypred_submit[list(var21.index[var21 > 7500]), 1] = 0
ypred_submit[list(num_var30.index[num_var30 > 9]), 1] = 0
ypred_submit[list(num_var13_0.index[num_var13_0 > 6]), 1] = 0
ypred_submit[list(num_var33_0.index[num_var33_0 > 0]), 1] = 0
ypred_submit[list(imp_ent_var16_ult1.index[imp_ent_var16_ult1 > 51003]), 1] = 0
ypred_submit[list(imp_op_var39_comer_ult3.index[imp_op_var39_comer_ult3 > 13184]), 1] = 0
ypred_submit[list(saldo_medio_var5_ult3.index[saldo_medio_var5_ult3 > 108251]), 1] = 0
ypred_submit[:7]
<normalization><EOS> 
sub = pd.concat([id_test, pd.Series(ypred_submit[:,1], name='TARGET')], axis=1)
 sub.to_csv('submission.csv',index=False, float_format='%.16f' )