Kaggle competition: Forest Cover Type Prediction
<SOS> metric: CategorizationAccuracy Kaggle data source: forest-cover-type-prediction<concatenate>
warnings.filterwarnings("ignore")
 print(os.listdir(".. /input"))
cat_eda(df2[set(df2.columns ).difference(ordinal_vars)], cat_length=17 )<concatenate>
train = pd.read_csv(".. /input/train.csv")
 train = train.drop(['Id'], axis=1)
 y = train.iloc[:,-1]
train = train.iloc[:,:-1]
test = pd.read_csv(".. /input/test.csv")
 test_Id = test.iloc[:,0]
test = test.drop(['Id'], axis=1)
cat_eda(df2[set(df2.columns ).difference(ordinal_vars)], cat_length=17 )<categorify>
all_data = train.append(test)
 train.head()
def label_encoder(dataframe):
labelencoder = preprocessing.LabelEncoder()
label_cols = [col for col in dataframe.columns if dataframe[col].dtypes == "O"
and len(dataframe[col].value_counts())== 2]
for col in label_cols:
dataframe[col] = labelencoder.fit_transform(dataframe[col])
 return dataframe
df2 = label_encoder(df2)
 cat_cols = [col for col in df2.columns if df2[col].dtypes == 'O']
def one_hot_encoder(dataframe, categorical_cols, nan_as_category=True):
original_columns = list(dataframe.columns)
 dataframe = pd.get_dummies(dataframe, columns=categorical_cols, dummy_na=nan_as_category, drop_first=True)
 new_columns = [c for c in dataframe.columns if c not in original_columns]
return dataframe, new_columns
df2, new_cols_ohe = one_hot_encoder(df2, cat_cols, nan_as_category=False)
 df2.shape<split>
train = train.drop(['Soil_Type7','Soil_Type15'], axis=1 )
train_model = df2[df2.Id.isin(train.Id)]
test_model = df2[df2.Id.isin(test.Id)]
test_model.drop("SalePrice", axis = 1, inplace = True)
 X = train_model.drop(['SalePrice', "Id"], axis=1)
 y = train_model[["SalePrice"]]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=46)
 lgb_param = {
"nthread": -1,
"objective": "regression",
"metric": "rmse",
"verbose": -1,
}
reg = lgb.LGBMRegressor(
 random_state=46, **lgb_param 
)
 reg.fit(X_train,y_train,
eval_set=[(X_train, y_train),(X_test, y_test)],
eval_metric = ["rmse", "mae"],
eval_names=["Train", "Valid"],
early_stopping_rounds=10,
verbose=10,
categorical_feature='auto')
 print("")
 print("Train RMSE:", "{:,.2f}".format(np.sqrt(mean_squared_error(y_train, reg.predict(X_train)))))
 print("Train RMSE:", "{:,.2f}".format(np.sqrt(mean_squared_error(y_test, reg.predict(X_test)))) , "
")
 print("Train MAE:", "{:,.2f}".format(mean_absolute_error(y_train, reg.predict(X_train))))
print("Valid MAE:", "{:,.2f}".format(mean_absolute_error(y_test, reg.predict(X_test))), "
")
 print("Train MAPE:", "{:,.2f}".format(mean_absolute_percentage_error(y_train, reg.predict(X_train))))
print("Valid MAPE:", "{:,.2f}".format(mean_absolute_percentage_error(y_test, reg.predict(X_test))), "
")
 print("Train R^2:", "{:,.2f}".format(r2_score(y_train, reg.predict(X_train))))
print("Valid R^2:", "{:,.2f}".format(r2_score(y_test, reg.predict(X_test))))<compute_train_metric>
a = all_data['Horizontal_Distance_To_Hydrology']
b = all_data['Vertical_Distance_To_Hydrology']
all_data['distance_to_hydrology'] = np.sqrt(np.power(a,2)+ np.power(b,2))
all_data['Horizontal_distance'] =(all_data['Horizontal_Distance_To_Hydrology'] + all_data['Horizontal_Distance_To_Roadways'] + all_data['Horizontal_Distance_To_Fire_Points'])/3
all_data['average_hillshade'] =(all_data['Hillshade_3pm'] + all_data['Hillshade_Noon'] + all_data['Hillshade_9am'])/3
all_data['Aspect_hillshade'] =(all_data['Aspect']*all_data['Hillshade_9am'])/255
all_data['slope_hillshade'] =(all_data['Slope']*all_data['Hillshade_Noon'])/255
all_data['Elevation'] = [math.floor(v/50.0)for v in all_data['Elevation']]
lgb_param = {
"nthread": -1,
"objective": "regression",
"metric": "rmse",
"verbose": -1,
}
reg = lgb.LGBMRegressor(
 random_state=46, **lgb_param 
)
 reg.fit(X,y)
 print("Train RMSE:","{:,.2f}".format(np.sqrt(mean_squared_error(y, reg.predict(X)))))
 print("Train MAE:", "{:,.2f}".format(mean_absolute_error(y, reg.predict(X))), "
")
 print("CV RMSE:", "{:,.2f}".format(np.sqrt(-cross_val_score(reg, X, y, cv=10, scoring="neg_mean_squared_error")).mean()))
print("CV MAE:", "{:,.2f}".format(-cross_val_score(reg, X, y, cv=10, scoring="neg_mean_absolute_error" ).mean()))<compute_train_metric>
all_data['EVDtH'] = all_data.Elevation-all_data.Vertical_Distance_To_Hydrology
all_data['EHDtH'] = all_data.Elevation-all_data.Horizontal_Distance_To_Hydrology*0.2
all_data['Distanse_to_Hydrolody'] =(all_data['Horizontal_Distance_To_Hydrology']**2+all_data['Vertical_Distance_To_Hydrology']**2)**0.5
all_data['Hydro_Fire_1'] = all_data['Horizontal_Distance_To_Hydrology']+all_data['Horizontal_Distance_To_Fire_Points']
all_data['Hydro_Fire_2'] = abs(all_data['Horizontal_Distance_To_Hydrology']-all_data['Horizontal_Distance_To_Fire_Points'])
 all_data['Hydro_Road_1'] = abs(all_data['Horizontal_Distance_To_Hydrology']+all_data['Horizontal_Distance_To_Roadways'])
 all_data['Hydro_Road_2'] = abs(all_data['Horizontal_Distance_To_Hydrology']-all_data['Horizontal_Distance_To_Roadways'])
 all_data['Fire_Road_1'] = abs(all_data['Horizontal_Distance_To_Fire_Points']+all_data['Horizontal_Distance_To_Roadways'])
 all_data['Fire_Road_2'] = abs(all_data['Horizontal_Distance_To_Fire_Points']-all_data['Horizontal_Distance_To_Roadways'])
lgb_param = {
"nthread": -1,
"objective": "regression",
"metric": "rmse",
"verbose": -1
}
reg = lgb.LGBMRegressor(
 random_state=46, **lgb_param 
)
 reg.fit(X_train,y_train)
 print("Train RMSE:", "{:,.2f}".format(np.sqrt(mean_squared_error(y_train, reg.predict(X_train)))))
 print("Train MAE:", "{:,.2f}".format(mean_absolute_error(y_train, reg.predict(X_train))), "
")
 print("CV RMSE:", "{:,.2f}".format(np.sqrt(-cross_val_score(reg, X_train, y_train, cv=10, scoring="neg_mean_squared_error")).mean()))
print("CV MAE:", "{:,.2f}".format(-cross_val_score(reg, X_train, y_train, cv=10, scoring="neg_mean_absolute_error" ).mean()), "
")
 print("Valid RMSE:", "{:,.2f}".format(np.sqrt(mean_squared_error(y_test, reg.predict(X_test)))))
 print("Valid MAE:", "{:,.2f}".format(mean_absolute_error(y_test, reg.predict(X_test))), "
" )<define_search_space>
train_data = all_data.iloc[:train.shape[0],:]
test_data = all_data.iloc[train.shape[0]:,:]
params = {
'learning_rate': [0.1, 0.09, 0.08, 0.07, 0.05],
'max_depth': [-1] + np.arange(5, 36, 1),
'num_leaves':np.arange(20, 81, 1),
'n_estimators':[50, 100, 500, 1000, 3000, 5000, 10000],
'max_bin':np.arange(150, 301, 1),
'min_data_in_leaf':np.arange(15, 61, 1),
"feature_fraction_seed":np.arange(2, 1001, 1),
"feature_fraction":np.arange(0.1, 1.01, 0.01),
"bagging_freq":[0],
"bagging_fraction":[1],
"min_sum_hessian_in_leaf":[1e-2,1e-3, 1e-4, 1e-5, 1e-6],
"lambda_l1":np.arange(0.0, 1.1, 0.1),
"lambda_l2":np.arange(0.0, 1.1, 0.1),
"min_gain_to_split ": np.arange(0, 101, 1)
 }
reg = lgb.LGBMRegressor(
 random_state=46, objective = "rmse", metric = "rmse" 
)
 rs = RandomizedSearchCV(reg, params, random_state=46, n_jobs=-1, cv = 10, scoring = "neg_mean_squared_error")
 rs_lgbm = rs.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_test, y_test)],
eval_metric = ["rmse", "mae"],
eval_names=["Train"],
early_stopping_rounds=10,
verbose=10,
categorical_feature='auto')
 print("")
 print("Train RMSE:", "{:,.2f}".format(np.sqrt(mean_squared_error(y_train, rs_lgbm.predict(X_train)))))
 print("Train MAE:", "{:,.2f}".format(mean_absolute_error(y_train, rs_lgbm.predict(X_train))), "
")
 print("CV RMSE:", "{:,.2f}".format(np.sqrt(-rs_lgbm.cv_results_["mean_test_score"] ).mean()))
print("CV RMSE STD:", "{:,.2f}".format(np.sqrt(rs_lgbm.cv_results_["std_test_score"] ).mean()), "
")
 print("Test RMSE:", "{:,.2f}".format(np.sqrt(mean_squared_error(y_test, rs_lgbm.predict(X_test)))))
 print("Test MAE:", "{:,.2f}".format(mean_absolute_error(y_test, rs_lgbm.predict(X_test))), "
")
 print("--------------------------------------")
 print("
print("--------------------------------------")
 print(rs_lgbm.best_params_ )<sort_values>
res.sort_values("AbsoluteError", ascending = False ).head(20 )<train_model>
rs = RobustScaler()
rs.fit(train_data)
 train_data = rs.transform(train_data)
 test_data = rs.transform(test_data)
 train_data = pd.DataFrame(train_data, columns = all_data.columns)
 test_data = pd.DataFrame(test_data, columns = all_data.columns )
train_model = df2[df2.Id.isin(train.Id)]
test_model = df2[df2.Id.isin(test.Id)]
test_model.drop("SalePrice", axis = 1, inplace = True)
 X = train_model.drop(['SalePrice', "Id"], axis=1)
 y = train_model[["SalePrice"]]
final = lgb.LGBMRegressor(
 random_state=46, objective = "regression", metric = "rmse",
**rs_lgbm.best_params_ 
)
 final.fit(X,y,
eval_set=[(X,y)],
eval_metric = ["rmse", "mae"],
eval_names=["Train"],
early_stopping_rounds=10,
verbose=1000,
categorical_feature='auto')
 submission = pd.DataFrame({"Id": test_model.Id, "SalePrice":final.predict(test_model.drop("Id", axis = 1)) })
 submission.to_csv("submission.csv", index = None )<set_options>
warnings.filterwarnings('ignore')
 %matplotlib inline<load_from_csv>
x_train, x_test, y_train, y_test = train_test_split(train_data, y, test_size = 0.1, shuffle= True )
df_train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 df_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 ids = df_test['Id'].values<prepare_x_and_y>
def fit(model, X, y, X_test):
model.fit(X, y)
 pred = model.predict(X_test)
 return pred
def accuracy(y_actuals, y_predicted):
print(accuracy_score(y_actuals, y_predicted))
y_train = df_train.SalePrice.values
x_train = df_train.drop('SalePrice', 1 )<sort_values>
etc = ExtraTreesClassifier(n_estimators=400)
 print(cross_val_score(etc,x_train,y_train,cv = 5 ).mean() )
total = df_train.isnull().sum().sort_values(ascending=False)
 percent =(df_train.isnull().sum() /df_train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<count_missing_values>
lgb = LGBMClassifier(num_leaves = 70)
x_train = x_train.drop(( missing_data[missing_data['Total'] > 81] ).index,1)
 x_train = x_train.apply(lambda x:x.fillna(x.value_counts().index[0]))
x_train.isnull().sum().max()<count_values>
print(cross_val_score(lgb,x_train,y_train,cv = 5 ).mean() )
df_test = df_test.drop(( missing_data[missing_data['Total'] > 81] ).index,1)
 df_test = df_test.apply(lambda x:x.fillna(x.value_counts().index[0]))<drop_column>
etc.fit(train_data, y)
 test_predictions = etc.predict(test_data )
<categorify><EOS> 
solutions = pd.DataFrame({'Id':test_Id, 'Cover_Type':test_predictions})
 solutions.to_csv('submission.csv',index=False )