Kaggle competition: Two Sigma Connect: Rental Listing Inquiries
age_missing_model1 = classifier_KSVM
age_missing_model1.fit(X_train, y_train)
 age_missing_model2 = classifier_SVM
age_missing_model2.fit(X_train, y_train)
 age_missing_model3 = classifier_NB
age_missing_model3.fit(X_train, y_train)
 voting_cl_MD = VotingClassifier(estimators = [('KSVM', age_missing_model1), 
('SVM',age_missing_model2), 
('NB',age_missing_model3)],
voting = 'hard')
 voting_cl_MD.fit(X_train, y_train)
<split>
train_df=pd.read_json('.. /input/train.json')
 test_df=pd.read_json('.. /input/test.json')
 train_df.head()
test_df["bathrooms"].loc[19671] = 1.5
test_df["bathrooms"].loc[22977] = 2.0
test_df["bathrooms"].loc[63719] = 2.0
ulimit = np.percentile(train_df.price.values, 99)
 train_df['price'].ix[train_df['price']>ulimit] = ulimit
ulimit = np.percentile(test_df.price.values, 99)
 test_df['price'].ix[test_df['price']>ulimit] = ulimit
train_df["logprice"] = np.log(train_df["price"])
 test_df["logprice"] = np.log(test_df["price"])
 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["pos"] = train_df.longitude.round(3 ).astype(str)+ '_' + train_df.latitude.round(3 ).astype(str)
 test_df["pos"] = test_df.longitude.round(3 ).astype(str)+ '_' + test_df.latitude.round(3 ).astype(str)
 vals = train_df['pos'].value_counts()
dvals = vals.to_dict()
train_df["density"] = train_df['pos'].apply(lambda x: dvals.get(x, vals.min()))
test_df["density"] = test_df['pos'].apply(lambda x: dvals.get(x, vals.min()))
train_df["price_t"] =train_df["price"]/train_df["bedrooms"]
test_df["price_t"] = test_df["price"]/test_df["bedrooms"]
train_df["room_sum"] = train_df["bedrooms"]+train_df["bathrooms"]
test_df["room_sum"] = test_df["bedrooms"]+test_df["bathrooms"]
train_df['price_per_room'] = train_df['price']/train_df['room_sum']
test_df['price_per_room'] = test_df['price']/test_df['room_sum']
features_to_use=["bathrooms", "bedrooms", "price_t","room_sum","latitude","longitude","num_photos","density","logprice","num_features","num_description_words","price_per_room","listing_id"]
print(train_df['price'].head() )
X_NMD = nonmissing_val_data.drop(['Survived'], axis = 1 ).values
y_NMD = nonmissing_val_data['Survived'].values.astype('int')
 r_state = 3
md_colnames = nonmissing_val_data.drop(['Survived'], axis = 1 ).columns
X_train, X_test, y_train, y_test = train_test_split(X_NMD,
y_NMD,
test_size = 0.3,
random_state = r_state)
 X_train<create_dataframe>
def runXGB(train_X, train_y, test_X, test_y=None, feature_names=None, seed_val=321, num_rounds=2000):
param = {}
param['objective'] = 'multi:softprob'
param['eta'] = 0.03
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)
 else:
xgtest = xgb.DMatrix(test_X)
 model = xgb.train(plst, xgtrain, num_rounds)
 pred_test_y = model.predict(xgtest)
 return pred_test_y, model
pd.DataFrame(X_train )<prepare_x_and_y>
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"]
sc_NMD = StandardScaler()
X_train[:, 5:] = sc_NMD.fit_transform(X_train[:, 5:])
 X_test[:, 5:] = sc_NMD.transform(X_test[:, 5:])
 X_train = pd.DataFrame(X_train,
columns = md_colnames)
 X_test = pd.DataFrame(X_test,
columns = md_colnames)
 print(X_train.head(3))
print(X_test.head(3))<choose_model_class>
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_NN = MLPClassifier(random_state = r_state)
 classifiers = [classifier_LR,
classifier_KNN,
classifier_SVM,
classifier_KSVM,
classifier_NB,
classifier_DT,
classifier_RF,
classifier_XGB,
optimal_GB_classifier,
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 
<load_from_csv>
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]))
test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data = test_data.drop(['Ticket','Cabin'], axis = 1)
 test_data.info()<categorify>
def factorize(df1, df2, column):
ps = df1[column].append(df2[column])
 factors = ps.factorize() [0]
df1[column] = factors[:len(df1)]
df2[column] = factors[len(df1):]
return df1, df2
obj_imputer_test = SimpleImputer(missing_values = np.nan,
strategy = 'most_frequent')
 fare_imputer_test = SimpleImputer(missing_values = np.nan,
strategy = 'mean')
 test_data['Embarked'] = obj_imputer.fit_transform(test_data[['Embarked']])
 test_data['Fare'] = fare_imputer_test.fit_transform(test_data[['Fare']])
 test_data<feature_engineering>
for col in('building_id', 'display_address', 'manager_id', 'street_address'):
train_df,test_df = factorize(train_df, test_df,col )
split_one = test_data['Name'].str.split('.', n=1, expand = True)
 test_data['Name'] = split_one[0]
split_two = test_data['Name'].str.split(',', n=1, expand = True)
 test_data['Name'] = split_two[1]
test_data['Title'] = test_data['Name'].apply(set_title)
 test_data<categorify>
tfidf = CountVectorizer(stop_words='english', max_features=200)
 tfidf.fit(list(train_df['features'])+ list(test_df['features']))
tr_sparse = tfidf.transform(train_df["features"])
 te_sparse = tfidf.transform(test_df["features"])
 print(te_sparse )
col_names = ['Female','Male','St_C','St_Q','St_S']
col_names.extend(( list(test_data.columns)))
 col_names.remove('Embarked')
 col_names.remove('Sex')
 ct_test = ColumnTransformer(transformers=[('encoder', OneHotEncoder() , [2,7])],
remainder='passthrough')
 test_data = pd.DataFrame(data = np.array(ct_test.fit_transform(test_data.values)) ,
columns = col_names)
 test_data 
<filter>
train_X = sparse.hstack([train_df[features_to_use], tr_sparse] ).tocsr()
missing_val_data_test = test_data[test_data['Age'].isnull() ]
nonmissing_val_data_test = test_data[test_data['Age'].notnull() ]
nonmissing_val_data_test.info()<predict_on_test>
test_X = sparse.hstack([test_df[features_to_use], te_sparse] ).tocsr()
MD_ID = missing_val_data_test['PassengerId']
NMD_ID = nonmissing_val_data_test['PassengerId']
X_MD_test = missing_val_data_test.drop(['Age','PassengerId'], axis = 1 ).values
X_NMD_test = nonmissing_val_data_test.drop(['PassengerId'], axis = 1 ).values
X_MD_test[:, 5:] = sc_MD.transform(X_MD_test[:, 5:])
 X_NMD_test[:, 5:] = sc_NMD.transform(X_NMD_test[:, 5:])
 y_pred_missing_age = []
y_pred_nonmissing_age = []
y_pred_nonmissing = voting_cl_NMD.predict(X_NMD_test)
 y_pred_missing_age = voting_cl_MD.predict(X_MD_test)
 pred_df1 = pd.DataFrame(
 {"PassengerId" : MD_ID,
"Survived" : y_pred_missing_age})
 pred_df2 = pd.DataFrame(
 {"PassengerId" : NMD_ID,
"Survived" : y_pred_nonmissing})
 frames = [pred_df1, pred_df2]
result = pd.concat(frames)
 file_name = "Submission_New.csv"
result.to_csv(file_name, index = False )<set_options>
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 )
<load_from_csv><EOS> 
preds, model = runXGB(train_X, train_y, test_X, num_rounds=1000)
 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 )