Kaggle competition: JAMP Hackathon Drive 1
cols_with_missing = [col for col in X_train.columns
if X_train[col].isnull().any() ]
len(cols_with_missing )<train_model>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
def score_dataset(X_train, X_valid, y_train, y_valid):
model = RandomForestRegressor(n_estimators=10, random_state=0)
 model.fit(X_train, y_train)
 preds = model.predict(X_valid)
 return mean_absolute_error(y_valid, preds )<drop_column>
wd = "/kaggle/input/jamp-hackathon-drive-1/train_set/"
X.drop(columns=bad_cols,axis=1,inplace=True)
 X_test.drop(columns=bad_cols,axis=1,inplace=True )<define_variables>
images_dir=os.listdir(wd )
category_colums=[colum for colum in X.columns if X[colum].dtype == 'object']
len(category_colums )<rename_columns>
df=pd.DataFrame()
df['Images']=data
classes=df["Images"].str.split("/", n = 6, expand = True)[5]
df['Label']=classes
df = df.sample(frac=1 ).reset_index(drop=True )
my_imputer = SimpleImputer(strategy='most_frequent')
 imputed_X=pd.DataFrame(my_imputer.fit_transform(X))
imputed_X_test = pd.DataFrame(my_imputer.transform(X_test))
imputed_X.columns = X.columns
imputed_X_test.columns = X_test.columns 
<categorify>
df['Label'].value_counts()
imputed_X_lable = X.copy()
imputed_X_test_lable = X_test.copy()
label_encoder = LabelEncoder()
for col in category_colums:
imputed_X_lable[col] = label_encoder.fit_transform(imputed_X[col])
 imputed_X_test_lable[col] = label_encoder.transform(imputed_X_test[col])
<define_search_space>
train, test = train_test_split(df, test_size=0.2,stratify=df['Label'] )
parameters = [{
'n_estimators': list(range(100, 1001, 100)) ,
'learning_rate': [x / 100 for x in(range(5, 100, 10)) ],
'max_depth': list(range(6, 70, 6))
}]
parameters<train_on_grid>
get_ipython().magic('matplotlib inline')
gsearch = GridSearchCV(estimator=XGBRegressor() ,
param_grid = parameters,
scoring='neg_mean_absolute_error',
n_jobs=4,
cv=5,
verbose=7)
 gsearch.fit(imputed_X_lable,y)
 gsearch.best_params_.get('n_estimators'),gsearch.best_params_.get('learning_rate'),gsearch.best_params_.get('max_depth' )<find_best_params>
from tqdm import tqdm
import pickle
best_n_estimators=gsearch.best_params_.get('n_estimators')
 best_learning_rate=gsearch.best_params_.get('learning_rate')
 best_max_depth=gsearch.best_params_.get('max_depth')
 best_n_estimators,best_learning_rate,best_max_depth<train_model>
train_img=[]
for i in tqdm(df['Images']):
temp_img=image.load_img(i,target_size=(224,224))
temp_img=image.img_to_array(temp_img)
 train_img.append(temp_img)
 train_img=np.array(train_img)
 train_img=preprocess_input(train_img)
best_model=XGBRegressor(n_estimators=best_n_estimators,
learning_rate=best_learning_rate,
max_depth=best_max_depth)
 best_model.fit(imputed_X_lable,y )<predict_on_test>
test_wd = "/kaggle/input/jamp-hackathon-drive-1/test_set/"
test_dir=os.listdir(test_wd)
 test_data=[]
for i in test_dir:
test_data.append(os.path.join(test_wd,i))
test_df=pd.DataFrame()
test_df['Images']=test_data
test_df.head()
my_preds_test=best_model.predict(imputed_X_test_lable)
 my_preds_test 
<save_to_csv>
test_img=[]
for i in tqdm(test_df['Images']):
temp_img=image.load_img(i,target_size=(224,224))
temp_img=image.img_to_array(temp_img)
 test_img.append(temp_img)
 test_img=np.array(test_img)
 test_img=preprocess_input(test_img )
my_output=pd.DataFrame(
 {'Id':X_test.index,
'SalePrice':my_preds_test})
 my_output.to_csv('submission.csv',index=False)
 print('create output done!' )<load_from_csv>
model = VGG16(weights='imagenet', include_top=False )
url="https://github.com/thisisjasonjafari/my-datascientise-handcode/raw/master/005-datavisualization/titanic.csv"
s=requests.get(url ).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
 test_data_with_labels = c
test_data = pd.read_csv('.. /input/titanic/test.csv' )<set_options>
features_train=model.predict(train_img )
warnings.filterwarnings('ignore' )<feature_engineering>
features_test=model.predict(test_img )
for i, name in enumerate(test_data_with_labels['name']):
if '"' in name:
test_data_with_labels['name'][i] = re.sub('"', '', name)
 for i, name in enumerate(test_data['Name']):
if '"' in name:
test_data['Name'][i] = re.sub('"', '', name )<filter>
train_x=features_train.reshape(1027,-1 )
survived = []
for name in test_data['Name']:
survived.append(int(test_data_with_labels.loc[test_data_with_labels['name'] == name]['survived'].values[-1]))<save_to_csv>
test_x=features_test.reshape(256,-1 )
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = survived
submission.to_csv('submission.csv', index=False )<import_modules>
train_y=np.asarray(df['Label'])
 train_y=pd.get_dummies(train_y)
 train_y=np.array(train_y)
 X_train, X_valid, Y_train, Y_valid=train_test_split(train_x,train_y,test_size=0.3, random_state=42)
import numpy as np
import pandas as pd
import copy
from functools import partial
from pandas import DataFrame
import xgboost as xgb
import lightgbm as lgb
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, BatchNormalization, Dropout
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import mean_squared_log_error
import optuna<load_from_csv>
test_y=np.asarray(test['Label'])
 test_y=pd.get_dummies(test_y)
 test_y=np.array(test_y )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 train.shape, test.shape<load_from_csv>
model=Sequential()
model.add(Dense(1000, input_dim=25088, activation='relu',kernel_initializer='uniform'))
keras.layers.core.Dropout(0.3, noise_shape=None, seed=None)
 model.add(Dense(500,input_dim=1000,activation='sigmoid'))
keras.layers.core.Dropout(0.4, noise_shape=None, seed=None)
 model.add(Dense(150,input_dim=500,activation='sigmoid'))
keras.layers.core.Dropout(0.2, noise_shape=None, seed=None)
 model.add(Dense(units=2))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
X_train = np.array(pd.read_csv('.. /input/houseprice-ppdata/train_x.csv'))
X_train = X_train[:, 1:]
y_train = np.array(pd.read_csv('.. /input/houseprice-ppdata/train_y.csv'))
y_train = y_train[:, 1]
XX_TEST = np.array(pd.read_csv('.. /input/houseprice-ppdata/test_x.csv'))
XX_TEST = XX_TEST[:, 1:]
std1 = X_train.std(axis=0)
 X_train = X_train[:, std1 != 0]
XX_TEST = XX_TEST[:, std1 != 0]
mean1 = X_train.mean(axis=0)
 std1 = X_train.std(axis=0)
 print(X_train.shape, XX_TEST.shape )<split>
model.fit(X_train, Y_train, epochs=20, batch_size=128,validation_data=(X_valid,Y_valid))
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, train_size= 0.90, random_state=2020)
 print(X_train.shape, y_train.shape)
 print(X_test.shape, y_test.shape )<compute_train_metric>
model.predict(X_valid )
def objective_xgb(X_train, X_test, y_train, y_test, trial):
params = {
'objective': 'reg:squarederror',
'max_depth': trial.suggest_int('max_depth', 1, 9),
'n_estimators': trial.suggest_int('n_estimators', 4000, 7000),
'learning_rate': trial.suggest_loguniform('learning_rate', 1e-3, 1.0),
'gamma': trial.suggest_uniform('gamma', 0.0, 1.0),
'min_child_weight': trial.suggest_uniform('min_child_weight', 0.0, 1.0),
'subsample': trial.suggest_uniform('subsample', 0.0, 1.0),
'colsample_bytree': trial.suggest_uniform('colsample_bytree', 0.0, 1.0)
 }
model = xgb.XGBRegressor(**params)
 model.fit(X_train, y_train)
 pred = model.predict(X_test)
 mse = mean_squared_error(y_test, pred)
 return mse<choose_model_class>
model.evaluate(X_valid,Y_valid )
mod_xgb_opt = xgb.XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.7245610853936947,
gamma=0.005388388541926817, gpu_id=-1, importance_type='gain',
interaction_constraints='', learning_rate=0.004021246542657597,
max_delta_step=0, max_depth=3, min_child_weight=0.5256214626878104,
monotone_constraints='() ', n_estimators=5880,
n_jobs=0, num_parallel_tree=1, random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=0.793174165882878,
tree_method='exact', validate_parameters=1, verbosity=None)
 mod_xgb_opt.fit(X_train, y_train)
<compute_test_metric>
scores=model.evaluate(test_x,test_y )
y_train_pred_xgb_opt = mod_xgb_opt.predict(X_train)
 y_test_pred_xgb_opt = mod_xgb_opt.predict(X_test)
 print('RMSLE train : %.6f, test : %.6f' %(np.sqrt(mean_squared_log_error(np.exp(y_train)-1, np.exp(y_train_pred_xgb_opt)-1)) , np.sqrt(mean_squared_log_error(np.exp(y_test)-1, np.exp(y_test_pred_xgb_opt)-1)))) 
<compute_train_metric>
print(f"Accuracy is {scores[1]*100} %" )
def objective_lgbm(X_train, X_test, y_train, y_test, trial):
params = {
'objective':'regression',
'metric':'mse',
'num_leaves': trial.suggest_int('num_leaves', 2, 64),
'max_bin': trial.suggest_int('max_bin', 128, 512),
'n_estimators': trial.suggest_int('n_estimators', 4000, 7000),
'learning_rate': trial.suggest_loguniform('learning_rate', 1e-3, 1.0),
'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.0, 1.0),
'bagging_freq': trial.suggest_int('bagging_freq', 0, 10),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.0, 1.0),
'min_sum_hessian_in_leaf': trial.suggest_loguniform('min_sum_hessian_in_leaf', 1e-3, 10.0)
 }
model = lgb.LGBMRegressor(**params)
 model.fit(X_train, y_train)
 pred = model.predict(X_test)
 mse = mean_squared_error(y_test, pred)
 return mse<train_model>
output=np.argmax(model.predict(test_x),axis=1 )
mod_lgb_opt = lgb.LGBMRegressor(bagging_fraction=0.9724444503168079, bagging_freq=2,
feature_fraction=0.572706177440646,
learning_rate=0.007117847154213432, max_bin=199,
min_sum_hessian_in_leaf=4.837478916656753, n_estimators=6157,
num_leaves=5)
 mod_lgb_opt.fit(X_train, y_train)
<compute_test_metric>
submission=pd.DataFrame()
submission['name']=test_df['Images'].str.split("/", n = 6, expand = True)[5].str.split(".", n = 3, expand = True)[0]
submission['class']=output
y_train_pred_lgbm_opt = mod_lgb_opt.predict(X_train)
 y_test_pred_lgbm_opt = mod_lgb_opt.predict(X_test)
 print('RMSLE train : %.6f, test : %.6f' %(np.sqrt(mean_squared_log_error(np.exp(y_train)-1, np.exp(y_train_pred_lgbm_opt)-1)) , np.sqrt(mean_squared_log_error(np.exp(y_test)-1, np.exp(y_test_pred_lgbm_opt)-1)))) 
<feature_engineering>
submission['class'] = submission['class'].replace({0:1, 1:0} )
X_train_nn = copy.deepcopy(X_train)
 X_test_nn = copy.deepcopy(X_test)
 y_train_nn = copy.deepcopy(y_train)
 y_test_nn = copy.deepcopy(y_test)
 X_train_nn =(X_train_nn - mean1)/ std1
X_test_nn =(X_test_nn - mean1)/ std1
mean2 = y_train_nn.mean(axis=0)
 std2 = y_train_nn.std(axis=0)
 y_train_nn =(y_train_nn - mean2)/ std2
y_test_nn =(y_test_nn - mean2)/ std2
print(np.any(std1 == 0))
print(np.any(std2 == 0)) 
<set_options>
submission.to_csv("submission.csv", index=False )
<choose_model_class><EOS> 
submission.to_csv("submission.csv", index=False )