Kaggle competition: HEROZ Internal Competition Extra2
trainX,testX,trainY,testY=train_test_split(featuresNN,input_data,test_size=0.3 )<define_variables>
!pip install category_encoders
numfeat=trainX.shape[1]
numfeat<import_modules>
train = pd.read_csv("/kaggle/input/heroz-internal-competition-extra2/train.csv")
 test = pd.read_csv("/kaggle/input/heroz-internal-competition-extra2/test_features.csv")
 ans = pd.read_csv("/kaggle/input/heroz-internal-competition-extra2/sample_submission.csv")
 print("Train shape : ", train.shape)
 print("Test shape : ", test.shape)
 print("ans shape : ", ans.shape)
 train
import keras
from keras.layers import *
from keras.models import Sequential 
<choose_model_class>
train.isnull().sum()
model=keras.models.Sequential()
model.add(Dense(300,input_dim=numfeat,activation='relu'))
model.add(Dense(500,activation='relu'))
model.add(Dense(400,activation='relu'))
model.add(Dense(20,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer = 'adam',)
 model.fit(featuresNN,input_data,epochs=50,shuffle=True,batch_size=100 )<predict_on_test>
test.isnull().sum()
a=model.predict_classes(test_TDF )<categorify>
train['reviews_per_month'] = train['reviews_per_month'].fillna(0)
 test['reviews_per_month'] = test['reviews_per_month'].fillna(0 )
predicted=le.inverse_transform(a)
 predicted<save_to_csv>
train['last_review'] = train['last_review'].fillna('2019-12-31')
 test['last_review'] = test['last_review'].fillna('2019-12-31' )
sub=pd.DataFrame({'id':test_data.id,'cuisine':predicted})
 sub.to_csv('sample_submission.csv',index=False )<data_type_conversions>
train['last_review'] = pd.to_datetime(train['last_review'])
 test['last_review'] = pd.to_datetime(test['last_review'])
 train['year'] = train['last_review'].dt.year-2010
test['year'] = test['last_review'].dt.year-2010
train['month'] = train['last_review'].dt.month-1
test['month'] = test['last_review'].dt.month-1
<load_from_disk>
train['latitude'] = train['latitude']-40
train['longitude'] = train['longitude']+75
test['latitude'] = test['latitude']-40
test['longitude'] = test['longitude']+75
train['waru'] = train['latitude'] / train['longitude']
train['kakeru'] = train['latitude'] * train['longitude']
test['waru'] = test['latitude'] / test['longitude']
test['kakeru'] = test['latitude'] * test['longitude']
start_time = time.time()
df_train = pd.read_json('.. /input/train.json')
 display(df_train)
 trainIngredients = df_train.drop(['cuisine','id'], axis=1)
 display(trainIngredients)
 trainID = df_train.id
trainCuisine = df_train.cuisine
df_test = pd.read_json('.. /input/test.json')
 display(df_test)
 testIngredients = df_test.drop(['id'], axis=1)
 testID = df_test.id<prepare_x_and_y>
train['minimum_nights_log'] = np.log1p(train['minimum_nights'])
 test['minimum_nights_log'] = np.log1p(test['minimum_nights'])
 train['calculated_host_listings_count_log'] = np.log1p(train['calculated_host_listings_count'])
 test['calculated_host_listings_count_log'] = np.log1p(test['calculated_host_listings_count'])
 train['availability_365_log'] = np.log1p(train['availability_365'])
 test['availability_365_log'] = np.log1p(test['availability_365'])
 train['number_of_reviews_log'] = np.log1p(train['number_of_reviews'])
 test['number_of_reviews_log'] = np.log1p(test['number_of_reviews'])
 train['reviews_per_month_log'] = np.log1p(train['reviews_per_month'])
 test['reviews_per_month_log'] = np.log1p(test['reviews_per_month'] )
with open('.. /input/train.json')as f:
data = json.load(f)
 train_df = pd.DataFrame(data)
 final=[]
for el in train_df['ingredients']:
test= ' '.join(el ).lower()
final.append(test)
 vectorizer = TfidfVectorizer(binary = True)
 X = vectorizer.fit_transform(final)
 with open('.. /input/test.json')as f:
data_test = json.load(f)
 test_df = pd.DataFrame(data_test)
 final_test=[]
for el in test_df['ingredients']:
test= ' '.join(el ).lower()
final_test.append(test)
 X_test = vectorizer.transform(final_test )<split>
target = 'price'
list_cols = ['neighbourhood_group', 'neighbourhood', 'room_type',]
target_columns = ['neighbourhood_group', 'neighbourhood', 'latitude', 'longitude', 'room_type',
'minimum_nights', 'number_of_reviews', 'reviews_per_month', 'calculated_host_listings_count', 'availability_365', 'waru', 'kakeru', 'minimum_nights_log',
'calculated_host_listings_count_log', 'availability_365_log',
'number_of_reviews_log', 'reviews_per_month_log', 'year', 'month']
ce_ohe = ce.OneHotEncoder(cols=list_cols)
 train_onehot = ce_ohe.fit_transform(train[target_columns] )
y=train_df['cuisine']
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.2 )<import_modules>
train_onehot.isnull().sum()
print('The scikit-learn version is {}.'.format(sklearn.__version__))<set_options>
test_onehot = ce_ohe.transform(test[target_columns] )
models=[];accs=[];times=[]
def running(mod):
mod_name=mod[:-2]
print(mod_name)
 if mod_name not in models:
models.append(mod_name)
 start=time.time()
model=eval(mod)
 model.fit(X_train,y_train)
 y_model=model.predict(X_validation)
 end=time.time()
accs.append(metrics.accuracy_score(y_validation, y_model))
times.append(end-start)
 lista=['BernoulliNB() ',
'DecisionTreeClassifier() ',
'ExtraTreeClassifier() ',
'ExtraTreesClassifier() ',
'LogisticRegression() ',
'LinearSVC() ',
'RidgeClassifier() ',
'RandomForestClassifier() ',
'NearestCentroid() ']
rrun=False
if rrun==True:
for el in lista:
running(el)
 for_df = [('Model', models), 
('Accuracy', accs), 
('Time', times)]
df_models = pd.DataFrame.from_items(for_df)
 print(df_models )<train_model>
pca2 = PCA(n_components=5)
 pca2_results = pca2.fit_transform(train_onehot)
 train_onehot['pca0']=pca2_results[:,0]
train_onehot['pca1']=pca2_results[:,1]
train_onehot['pca2']=pca2_results[:,2]
train_onehot['pca3']=pca2_results[:,3]
train_onehot['pca4']=pca2_results[:,4]
pca2_results = pca2.transform(test_onehot)
 test_onehot['pca0']=pca2_results[:,0]
test_onehot['pca1']=pca2_results[:,1]
test_onehot['pca2']=pca2_results[:,2]
test_onehot['pca3']=pca2_results[:,3]
test_onehot['pca4']=pca2_results[:,4]
rrun=True
if rrun==True:
start=time.time()
sdg = SGDClassifier().fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, sdg), 'in', end-start,'sec')
<train_model>
y_train = np.log1p(train["price"] )
rrun=False
if rrun==True:
start=time.time()
test = XGBClassifier().fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, test), 'in', end-start,'sec')
<compute_test_metric>
kf = KFold(n_splits=5, shuffle=True, random_state=1)
 s = list(kf.split(train_onehot, y_train))
rrun=False
if rrun==True:
start=time.time()
mod=LinearSVC()
linsvc = mod.fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, linsvc), 'in', end-start,'sec')
<compute_train_metric>
gbk_models = []
for train_i, val_i in s:
gbk = GradientBoostingRegressor()
gbk.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = gbk.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 gbk_models.append(gbk )
rrun=False
if rrun==True:
clf1 = LogisticRegression()
clf2 = LinearSVC()
clf3 = SGDClassifier()
eclf = VotingClassifier(estimators=[('lr', clf1),('LinearSVC', clf2),('SGD', clf3)], voting='hard')
 for clf, label in zip([clf1, clf2, clf3, eclf], ['Logistic Regression', 'LinearSVC', 'SGDClassifier', 'Ensemble']):
scores = cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy')
 print("Accuracy: %0.2f(+/- %0.2f)[%s]" %(scores.mean() , scores.std() , label)) 
<train_on_grid>
mlp_models = []
for train_i, val_i in s:
mlp = MLPRegressor(max_iter=100, hidden_layer_sizes=(100,100),
activation='relu', learning_rate_init=0.01)
 mlp.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = mlp.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 mlp_models.append(mlp )
rrun=False
if rrun==True:
param_grid = {'C':[1,50,75,100],'gamma':[1], 'kernel':['rbf']}
grid = GridSearchCV(SVC() ,param_grid,refit = True, verbose=2)
 grid.fit(X_train,y_train)
 grid.best_params_
predic = grid.predict(X_validation)
 print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, predic)) 
<compute_train_metric>
rfc_models = []
for train_i, val_i in s:
rfc = RandomForestRegressor()
rfc.fit(train_onehot.iloc[train_i], y_train.iloc[train_i])
 y_pred = rfc.predict(train_onehot.iloc[val_i])
 acc_gbk = round(mean_squared_error(np.expm1(y_pred), np.expm1(y_train[val_i])))
 print(acc_gbk)
 rfc_models.append(rfc )
rrun=True
if rrun==True:
start=time.time()
svc=SVC(C=50,gamma=1,kernel='rbf')
 onevsall=OneVsRestClassifier(svc ).fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, onevsall), 'in', end-start,'sec')
 conf_mat = confusion_matrix(y_validation, onevsall)
 fig, ax = plt.subplots(figsize=(10,10))
sns.heatmap(conf_mat, annot=True, fmt='d',xticklabels=set(list(y_validation)) , yticklabels=set(list(y_validation)))
 plt.ylabel('Actual')
 plt.xlabel('Predicted')
 plt.show()
print(metrics.classification_report(y_validation, onevsall, target_names=set(list(y_validation))))<train_model>
models = gbk_models + rfc_models
preds = np.array([np.expm1(model.predict(test_onehot)) for model in models])
 preds = preds.mean(axis=0)
 preds = np.where(preds < 0 , 0, preds )
<find_best_model_class><EOS> 
ans["price"] = preds
ans.to_csv("heroz_nakai.csv", index=False )