Kaggle competition: Forest Cover Type Prediction
n_train = len(train)
 features = pd.concat([train, test], sort=False ).reset_index(drop=True )<data_type_conversions>
train_data=pd.read_csv("/kaggle/input/forest-cover-type-prediction/train.csv")
 train_data.head()
<feature_engineering>
test_data=pd.read_csv("/kaggle/input/forest-cover-type-prediction/test.csv" )
<feature_engineering>
train_data['Cover_Type'].value_counts()
<prepare_x_and_y>
x=train_data.drop(["Id","Cover_Type"],axis=1 )
test = features[n_train:].reset_index(drop=True)
 train = features[:n_train]<categorify>
y=train_data['Cover_Type']
def label_encoding(train: pd.DataFrame, test: pd.DataFrame, col_definition: list):
n_train = len(train)
 train = pd.concat([train, test], sort=False ).reset_index(drop=True)
 for column in col_definition:
try:
lbl = preprocessing.LabelEncoder()
train[column] = lbl.fit_transform(list(train[column].values))
except:
print(column)
 test = train[n_train:].reset_index(drop=True)
 train = train[:n_train]
return train, test<categorify>
from sklearn.model_selection import train_test_split
categorical_cols = list(train.select_dtypes(include=['object'] ).columns)
 print(categorical_cols)
 train, test = label_encoding(train, test, categorical_cols )<prepare_x_and_y>
from sklearn.model_selection import train_test_split
X = train.drop(['Id', 'SalePrice'], axis=1)
 y = np.log1p(train['SalePrice'])
 X_test = test.drop(['Id','SalePrice'], axis=1 )<drop_column>
x_train,x_val,y_train,y_val=train_test_split(x,y,random_state=40 )
outliers = [30, 88, 462, 631, 1322]
X = X.drop(X.index[outliers])
 X.reset_index(inplace=True, drop=True)
 y = y.drop(y.index[outliers])
 y.reset_index(inplace=True, drop=True )<choose_model_class>
from sklearn.ensemble import RandomForestClassifier
cv = KFold(n_splits=5, shuffle=True, random_state=0 )<split>
rfc=RandomForestClassifier(n_estimators=70)
 rfc.fit(x_train,y_train )
y_preds = []
models = []
oof_train = np.zeros(( len(X),))
cv = KFold(n_splits=5, shuffle=True, random_state=0)
 params = {
'num_leaves': 24,
'max_depth': 6,
'objective': 'regression',
'metric': 'rmse',
'learning_rate': 0.05
}
for fold_id,(train_index, valid_index)in enumerate(cv.split(X)) :
X_tr = X.loc[train_index, :]
X_val = X.loc[valid_index, :]
y_tr = y[train_index]
y_val = y[valid_index]
lgb_train = lgb.Dataset(X_tr,
y_tr,
categorical_feature=categorical_cols)
 lgb_eval = lgb.Dataset(X_val,
y_val,
reference=lgb_train,
categorical_feature=categorical_cols)
 model = lgb.train(params,
lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=10,
num_boost_round=1000,
early_stopping_rounds=10)
 oof_train[valid_index] = model.predict(X_val,
num_iteration=model.best_iteration)
 y_pred = model.predict(X_test,
num_iteration=model.best_iteration)
 y_preds.append(y_pred)
 models.append(model)
<compute_test_metric>
rfc.score(x_val,y_val )
print(f'CV: {np.sqrt(mean_squared_error(y, oof_train)) }' )<predict_on_test>
predict=rfc.predict(test_data.drop("Id",axis=1))
y_pred = np.expm1(model.predict(X_test,num_iteration=model.best_iteration))<save_to_csv>
submission=pd.DataFrame(data=predict,columns=['Cover_Type'] )
y_sub = sum(y_preds)/ len(y_preds)
 y_sub = np.expm1(y_sub)
 sub['SalePrice'] = y_sub
sub.to_csv('submission.csv', index=False)
 sub.head()<import_modules>
submission["Id"]=test_data["Id"]
submission.set_index("Id",inplace=True )
import numpy as np
import pandas as pd
from pathlib import Path
from fastai import *
from fastai.vision import *
from fastai.callbacks import *
import torch<define_variables>
submission.to_csv("Submission.csv" )