Kaggle competition: INGV - Volcanic Eruption Prediction
X_test = test_df
X_train = train_df<import_modules>
train = pd.read_csv('.. /input/ingv-tsfresh-7730/train.csv', sep = ';')
 train.set_index('Unnamed: 0', inplace = True)
 test = pd.read_csv('.. /input/ingv-tsfresh-7730/test.csv', sep = ';')
 test.set_index('Unnamed: 0', inplace = True )
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV<choose_model_class>
test_index = test.index
rfc = RandomForestClassifier()<define_search_space>
train_rf = train.copy()
param_grid = {
'n_estimators': [200, 500, 1000],
'max_features': ['auto'],
'max_depth': [6, 7, 8],
'criterion': ['entropy']
}<train_on_grid>
train_rf = train_rf.fillna(0 )
CV = GridSearchCV(estimator = rfc, param_grid = param_grid, cv = 5)
 CV.fit(X_train, y)
 CV.best_estimator_<choose_model_class>
x = train_rf.drop('time_to_eruption', axis=1)
 y = train_rf['time_to_eruption']
rfc = RandomForestClassifier(criterion = 'entropy', max_depth = 8, n_estimators = 500, random_state = 42 )<train_model>
model = RandomForestRegressor(random_state=1, max_depth=7)
 model.fit(x,y )
rfc.fit(X_train, y )<predict_on_test>
selected_feature = feature_scores[:350].index
y_pred = rfc.predict(X_test )<train_model>
target = train['time_to_eruption']
all_data = pd.concat([train, test], ignore_index = True)
 all_data.head()
y_pred<create_dataframe>
all_data = pd.concat([all_data[selected_feature], all_data['time_to_eruption']], axis=1)
 all_data.head()
submission = y_pred.reshape(-1, 1 )<create_dataframe>
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
 mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
 mis_val_table_ren_columns = mis_val_table.rename(
 columns = {0 : 'Missing Values', 1 : '% of Total Values'})
 mis_val_table_ren_columns = mis_val_table_ren_columns[
mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(
 '% of Total Values', ascending=False ).round(1)
 print("Your selected dataframe has " + str(df.shape[1])+ " columns.
 "
"There are " + str(mis_val_table_ren_columns.shape[0])+
" columns that have missing values.")
 return mis_val_table_ren_columns
sub_df = pd.DataFrame(submission )<prepare_output>
missing_values = missing_values_table(all_data)
 missing_values
sub_df['PassengerId'] = test['PassengerId']
sub_df['Survived'] = submission
cols = ['PassengerId',
'Survived']
sub_df.drop(0, axis = 1, inplace = True)
 sub_df.columns = [i for i in cols]
sub_df = sub_df.set_index('PassengerId' )<save_to_csv>
all_data = all_data.fillna(all_data.mode() )
sub_df.to_csv(r'submission.csv' )<set_options>
header = all_data.columns
scaler = StandardScaler()
all_data[header] = scaler.fit_transform(all_data)
 all_data.head()
warnings.filterwarnings("ignore")
 pd.set_option('display.max_columns', None)
 sns.set() 
<load_from_csv>
all_data = all_data.drop('time_to_eruption', axis=1)
 all_data.var()
home = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<drop_column>
missing_values = missing_values_table(all_data)
 missing_values
test_Id = test['Id']
home.drop('Id', axis=1, inplace=True)
 test.drop('Id', axis=1, inplace=True)
<feature_engineering>
pca = PCA()
all_data_pca = pca.fit_transform(all_data)
 all_data_pca = pd.DataFrame(all_data_pca)
 all_data_pca.head()
home['SalePrice'] = np.log1p(home['SalePrice'] )<drop_column>
train = all_data_pca[:train.shape[0]]
test = all_data_pca[train.shape[0]:]
numerical_features = home.select_dtypes(exclude='object' ).drop(['SalePrice'], axis=1 )<drop_column>
Y = target
X = train
X_test = test
n_fold = 5
cv = KFold(n_splits=n_fold, shuffle=True, random_state=123)
 oof = np.zeros(len(X))
cat_prediction = np.zeros(len(X_test))
mae, r2 = [], []
PARAMS = {
'random_seed': 123,
'eval_metric': 'MAE',
'task_type': 'GPU',
'bagging_temperature': 0.41010395885331385,
'border_count': 186,
'depth': 10,
'iterations': 700,
'l2_leaf_reg': 30,
'learning_rate': 0.067,
'random_strength': 3.230824361824754e-06,
}
for fold_n,(train_index, valid_index)in enumerate(cv.split(X)) :
print('
Fold', fold_n, 'started at', ctime())
 X_train = X.iloc[train_index,:]
X_valid = X.iloc[valid_index,:]
Y_train = Y.iloc[train_index]
Y_valid = Y.iloc[valid_index]
best_model = CatBoostRegressor(**PARAMS, thread_count = -1)
 train_dataset = Pool(data=X_train,
label=Y_train, 
)
 eval_dataset = Pool(data=X_valid,
label=Y_valid, 
)
 best_model.fit(train_dataset,
use_best_model=True,
verbose = False,
plot = True,
eval_set=eval_dataset,
early_stopping_rounds=70)
 y_pred = best_model.predict(Pool(data=X_valid))
mae.append(mean_absolute_error(Y_valid, y_pred))
r2.append(r2_score(Y_valid, y_pred))
print('MAE: ', mean_absolute_error(Y_valid, y_pred))
print('R2: ', r2_score(Y_valid, y_pred))
cat_prediction += best_model.predict(Pool(data=X_test))
cat_prediction /= n_fold
print('='*45)
 print('CV mean MAE: {0:.4f}, std: {1:.4f}.'.format(np.mean(mae), np.std(mae)))
 print('CV mean R2: {0:.4f}, std: {1:.4f}.'.format(np.mean(r2), np.std(r2)) )
pos = [1298,523, 297]
home.drop(home.index[pos], inplace=True )<prepare_x_and_y>
y = home.SalePrice.values
home.drop(['SalePrice'], axis=1, inplace=True)
 dataset = pd.concat(( home, test)).reset_index(drop=True )<create_dataframe>
<data_type_conversions><EOS> 
submission = pd.DataFrame()
submission['segment_id'] = test_index
submission['time_to_eruption'] = cat_prediction
submission.to_csv('submission.csv', header=True, index=False )