Kaggle competition: Forest Cover Type Prediction
lean_den161.unfreeze()<train_model>
train=pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv', index_col='Id')
 test=pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv', index_col='Id')
 submission=pd.read_csv('/kaggle/input/forest-cover-type-prediction/sampleSubmission.csv', index_col='Id' )
lean_den161.fit_one_cycle(2, max_lr=slice(1e-5))<define_variables>
Ytrain=train['Cover_Type']
train=train[list(test)]
file=[]
for root, dirs, files in os.walk(path):
for filename in files:
file.append(filename )<create_dataframe>
train.isnull().sum()
submission_df=pd.DataFrame({'id': file, 'has_cactus': 1} )<predict_on_test>
train.isnull().sum()
i=0
for image in submission_df.id:
img=open_image(path+'/'+image)
 pred_class,pred_idx,outputs = lean_den161.predict(img)
 submission_df.iloc[i,1]=pred_class
i=i+1<save_to_csv>
test.isnull().sum()
submission_df.to_csv('submission.csv', header=True, index=False )<import_modules>
test.isnull().sum()
import numpy as np
import pandas as pd
import torch
from fastai import *
from fastai.vision import *<load_from_csv>
for col in ['Wilderness_Area1', 'Wilderness_Area2', 'Wilderness_Area3', 'Wilderness_Area4', 'Soil_Type1', 'Soil_Type2', 'Soil_Type3', 'Soil_Type4', 'Soil_Type5', 'Soil_Type6',
'Soil_Type7', 'Soil_Type8', 'Soil_Type9', 'Soil_Type10', 'Soil_Type11', 'Soil_Type12', 'Soil_Type13', 'Soil_Type14', 'Soil_Type15', 'Soil_Type16', 'Soil_Type17',
'Soil_Type18', 'Soil_Type19', 'Soil_Type20', 'Soil_Type21', 'Soil_Type22', 'Soil_Type23', 'Soil_Type24', 'Soil_Type25', 'Soil_Type26', 'Soil_Type27', 'Soil_Type28',
'Soil_Type29', 'Soil_Type30', 'Soil_Type31', 'Soil_Type32', 'Soil_Type33', 'Soil_Type34', 'Soil_Type35', 'Soil_Type36', 'Soil_Type37', 'Soil_Type38', 'Soil_Type39',
'Soil_Type40']:
if train[col].nunique() != test[col].nunique() :
print(f'Different Values train-test : {col}' )
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/sample_submission.csv" )<prepare_x_and_y>
train=train.drop(columns=['Soil_Type7','Soil_Type15'])
 test=test.drop(columns=['Soil_Type7','Soil_Type15'] )
test_img = ImageList.from_df(test_df, path='.. /input/test', folder='test' )<feature_engineering>
sns.countplot(Ytrain)
 Ytrain.value_counts()
trfm2 = get_transforms(do_flip=True, flip_vert=True, max_rotate=0, max_zoom=0, max_lighting=0.2, max_warp=0.2, p_affine=0.5, p_lighting=0.5 )<categorify>
print(f"Num_Kind of plants living in water? : {Ytrain.loc[train.loc[train['Distance_To_Hydrology']==0].index].nunique() }" )
train_img2 =(ImageList.from_df(train_df, path='.. /input/train', folder='train')
.split_by_rand_pct(0.01)
.label_from_df() 
.add_test(test_img)
.transform(trfm2, size=128)
.databunch(path='.', bs=64, device= torch.device('cuda:0')) 
.normalize(imagenet_stats)
 )<choose_model_class>
sns.distplot(np.log(train['Distance_To_Hydrology']+1), fit=norm)
 train['Distance_To_Hydrology']=np.log(train['Distance_To_Hydrology']+1)
 test['Distance_To_Hydrology']=np.log(test['Distance_To_Hydrology']+1)
 train.loc[train['Distance_To_Hydrology']==0,'Distance_To_Hydrology']=np.nan
test.loc[test['Distance_To_Hydrology']==0,'Distance_To_Hydrology']=np.nan
learn2 = cnn_learner(train_img2, models.densenet161, metrics=[error_rate, accuracy] )<train_model>
qut=QuantileTransformer(output_distribution='normal')
 pipeline=make_pipeline(qut, PCA(n_components=1))
learn2.fit_one_cycle(5, slice(3e-02))<predict_on_test>
train['Elevation_Roadways']=pipeline.fit_transform(train[['Elevation','Horizontal_Distance_To_Roadways']])
 test['Elevation_Roadways']=pipeline.transform(test[['Elevation','Horizontal_Distance_To_Roadways']] )
preds,_ = learn2.get_preds(ds_type=DatasetType.Test )<define_variables>
alpha=train[['Hillshade_9am','Hillshade_3pm']]
alpha2=test[['Hillshade_9am','Hillshade_3pm']]
test_df.has_cactus = preds.numpy() [:, 0]<save_to_csv>
train['Hillshade9am_Hillshade_3pm']=pipeline.fit_transform(alpha)
 test['Hillshade9am_Hillshade_3pm']=pipeline.transform(alpha2 )
test_df.to_csv('submission.csv', index=False)
<import_modules>
alpha=train[['Aspect','Hillshade9am_Hillshade_3pm']]
alpha2=test[['Aspect','Hillshade9am_Hillshade_3pm']]
import numpy as np
import pandas as pd 
<import_modules>
train['Aspect_Hillshade']=pipeline.fit_transform(alpha)
 test['Aspect_Hillshade']=pipeline.transform(alpha2 )
from fastai import *
from fastai.vision import *
import torch
from pathlib import *<load_from_csv>
alpha=train[['Slope','Hillshade_Noon']]
alpha2=test[['Slope','Hillshade_Noon']]
train=pd.read_csv('.. /input/train.csv')
 train.head()<load_from_csv>
train['Slope_Hillshade']=pipeline.fit_transform(alpha)
 test['Slope_Hillshade']=pipeline.transform(alpha2 )
test=pd.read_csv('.. /input/sample_submission.csv' )<create_dataframe>
Xtrain=train
Xtest=test
test_img=ImageList.from_df(test, path=path/"test", folder="test")
 tfms=get_transforms(flip_vert=True )<load_pretrained>
rf=RandomForestClassifier(n_estimators=5000, random_state=18, n_jobs=-1)
 rf.fit(Xtrain.fillna(-999), Ytrain)
 feature_importance_df=pd.DataFrame(data=None, columns=['feature','importances'])
 feature_importance_df['importances']=rf.feature_importances_
feature_importance_df['feature']=Xtrain.columns
feature_importance_df=feature_importance_df.sort_values(by='importances', ascending=False)
 feature_importance_df.tail()
train_img=(ImageList.from_df(train, path=path/'train', folder="train")
.split_by_rand_pct(0.01)
.label_from_df() 
.add_test(test_img)
.transform(tfms, size=256)
.databunch(path='.. /input', bs=64)
.normalize(imagenet_stats)
 )<define_variables>
params={'n_estimators': 1222, 'learning_rate': 0.07307234151834806, 'num_leaves': 96, 'colsample_bytree': 0.8972376156262298,
'subsample': 0.9312856106293543, 'min_child_samples': 1}
lightgbm=LGBMClassifier(random_state=18, subsample_freq=1, silent=False, **params )
<choose_model_class><EOS> 
lightgbm.fit(Xtrain, Ytrain)
 predictions=lightgbm.predict(Xtest)
 submission['Cover_Type']=predictions
submission.to_csv('LGBSingleModel.csv')
 submission.head()