Kaggle competition: Two Sigma Connect: Rental Listing Inquiries
X_initial = X.copy()
X_initial['family_size'] = X_initial['SibSp'] + X_initial['Parch'] + 1
X_initial['embarked_class'] = X_initial['Embarked'] + '_' + X_initial['Pclass'].astype(str)
 X_initial = X_initial.drop(columns=['Name', 'Cabin', 'Ticket'], axis=1)
 numerical_cols = [cname for cname in X_initial.columns if
X_initial[cname].dtype in ['int64', 'float64']]
categorical_cols = [cname for cname in X_initial.columns if
X_initial[cname].nunique() < 18 and
X_initial[cname].dtype in ['object', 'bool']]
numerical_transformer = SimpleImputer(strategy='constant')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ])
 model = XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1.0, gamma=1.5, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.02, max_delta_step=0, max_depth=6,
min_child_weight=1, monotone_constraints='() ',
n_estimators=400, n_jobs=1, nthread=1, num_parallel_tree=1,
random_state=0, reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
subsample=1.0, tree_method='exact', validate_parameters=1,
verbosity=None)
 clf = Pipeline(steps=[('preprocessor', preprocessor), 
('model', model)
 ])
 X_train, X_valid, y_train, y_valid = train_test_split(X_initial, y, random_state=0)
 clf.fit(X_train, y_train)
 preds = clf.predict(X_valid)
 print('Accuracy Score:', accuracy_score(y_valid, preds)) 
<save_to_csv>
index=list(range(train_df.shape[0]))
	
random.shuffle(index)
 a=[np.nan]*len(train_df)
 b=[np.nan]*len(train_df)
 c=[np.nan]*len(train_df)
 for i in range(5):
building_level={}
	
for j in train_df['manager_id'].values:
building_level[j]=[0,0,0]
	
test_index=index[int(( i*train_df.shape[0])/5):int(((i+1)*train_df.shape[0])/5)]
train_index=list(set(index ).difference(test_index))
for j in train_index:
temp=train_df.iloc[j]
if temp['interest_level']=='low':
building_level[temp['manager_id']][0]+=1
if temp['interest_level']=='medium':
building_level[temp['manager_id']][1]+=1
if temp['interest_level']=='high':
building_level[temp['manager_id']][2]+=1
for j in test_index:
temp=train_df.iloc[j]
if sum(building_level[temp['manager_id']])!=0:
a[j]=building_level[temp['manager_id']][0]*1.0/sum(building_level[temp['manager_id']])
 b[j]=building_level[temp['manager_id']][1]*1.0/sum(building_level[temp['manager_id']])
 c[j]=building_level[temp['manager_id']][2]*1.0/sum(building_level[temp['manager_id']])
 train_df['manager_level_low']=a
train_df['manager_level_medium']=b
train_df['manager_level_high']=c
a=[]
b=[]
c=[]
building_level={}
for j in train_df['manager_id'].values:
building_level[j]=[0,0,0]
for j in range(train_df.shape[0]):
temp=train_df.iloc[j]
if temp['interest_level']=='low':
building_level[temp['manager_id']][0]+=1
if temp['interest_level']=='medium':
building_level[temp['manager_id']][1]+=1
if temp['interest_level']=='high':
building_level[temp['manager_id']][2]+=1
for i in test_df['manager_id'].values:
if i not in building_level.keys() :
a.append(np.nan)
 b.append(np.nan)
 c.append(np.nan)
 else:
a.append(building_level[i][0]*1.0/sum(building_level[i]))
b.append(building_level[i][1]*1.0/sum(building_level[i]))
c.append(building_level[i][2]*1.0/sum(building_level[i]))
test_df['manager_level_low']=a
test_df['manager_level_medium']=b
test_df['manager_level_high']=c
features_to_use.append('manager_level_low')
 features_to_use.append('manager_level_medium')
 features_to_use.append('manager_level_high')
 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)
 print(train_df["features"].head())
 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["manager_level_low"].head() )
X_test = pd.read_csv('.. /input/titanic/test.csv')
 X_test['family_size'] = X_test['SibSp'] + X_test['Parch'] + 1
X_test['embarked_class'] = X_initial['Embarked'] + '_' + X_test['Pclass'].astype(str)
 X_test = X_test.drop(columns=['Name', 'Cabin', 'Ticket'], axis=1)
 preds = clf.predict(X_test)
 output = pd.DataFrame({'PassengerId': X_test.PassengerId,
'Survived': preds})
 output.to_csv('submission.csv', index=False )<set_options>
tfidf = CountVectorizer(min_df=5)
 tr_sparse = tfidf.fit_transform(train_df["features"])
 te_sparse = tfidf.transform(test_df["features"])
 print(te_sparse )
warnings.filterwarnings("ignore")
<load_from_csv>
print(test_df[features_to_use].head())
 print(train_df[features_to_use].head())
 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 )
df_train = pd.read_csv("/kaggle/input/titanic/train.csv")
 df_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 df_sample_sub = pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 df_all = df_train.append(df_test, ignore_index=True)
 print("Titanic Dataset Summary:")
 display(df_all.head())
 print("Stats of some features:")
 display(df_all.describe())
 print(df_all.info())
 plt.figure(figsize=(8,5))
sns.heatmap(df_all.isnull())
 plt.show()
m = df_all.shape[0]
print('Number of missing values in',m, 'examples')
 display(df_all.isnull().sum())
<feature_engineering>
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
<data_type_conversions><EOS> 
preds, model = runXGB(train_X, train_y, test_X, num_rounds=1500)
 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 )