Kaggle competition: Forest Cover Type Prediction
<SOS> metric: CategorizationAccuracy Kaggle data source: forest-cover-type-prediction<load_pretrained>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
model.load_state_dict(torch.load('best_model.pt'))<save_to_csv>
test = pd.read_csv(".. /input/forest-cover-type-prediction/test.csv")
 train = pd.read_csv(".. /input/forest-cover-type-prediction/train.csv" )
model.eval()
preds = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.cuda() , target.cuda()
output = model(data)
 pr = output[:,1].detach().cpu().numpy()
for i in pr:
preds.append(i)
 sample_sub['has_cactus'] = preds
sample_sub.to_csv('submission.csv', index=False )<load_from_csv>
X_train = train.drop(['Id', 'Cover_Type'], axis=1)
 y_train = train.Cover_Type
X_test = test.drop('Id', axis=1)
 test_id = test.Id
print(X_train.shape)
 print(X_test.shape )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/sample_submission.csv')
 print(train_df.shape, test_df.shape )<count_values>
scaler = MinMaxScaler()
Xs_train = scaler.fit_transform(X_train)
 Xs_test = scaler.transform(X_test )
train_df['has_cactus'].value_counts()<define_variables>
lr_model = LogisticRegression(solver='liblinear')
 param_grid = {
'penalty': ['l1', 'l2'],
'C': [10, 100, 1000],
}
np.random.seed(1)
 grid_search = GridSearchCV(lr_model, param_grid, cv=5, refit='True', verbose=15, n_jobs=-1)
 grid_search.fit(Xs_train, y_train)
 print(grid_search.best_score_)
 print(grid_search.best_params_ )
train_path = '.. /input/train/train/'
test_path = '.. /input/test/test/'<categorify>
rf_model = RandomForestClassifier(n_estimators=500)
 param_grid = {
'min_samples_leaf' : [2, 4, 8, 16],
'max_depth' : [12, 16, 24]
}
np.random.seed(1)
 grid_search = GridSearchCV(rf_model, param_grid, cv=5, refit='True', verbose=15, n_jobs=-1)
 grid_search.fit(X_train, y_train)
 print(grid_search.best_score_)
 print(grid_search.best_params_ )
def prep_cnn_data(df, n_x, n_c, path):

 tensors = np.zeros(( df.shape[0], n_x, n_x, n_c))
for i in range(df.shape[0]):
pic = load_img(path+df.iloc[i]['id'])
 pic_array = img_to_array(pic)
 tensors[i,:] = pic_array
tensors = tensors / 255.
 return tensors<prepare_x_and_y>
test_pred = grid_search.predict(X_test )
train_pic_array = prep_cnn_data(train_df, 32, 3, path='.. /input/train/train/')
 train_Y = train_df['has_cactus'].values<define_variables>
for i in range(1,8):
print(list(test_pred ).count(i))
test_pic_array = prep_cnn_data(test_df, 32, 3, path='.. /input/test/test/' )<set_options>
submission = pd.DataFrame({
'Id':test_id,
'Cover_Type':test_pred
})
 submission.head()
<choose_model_class><EOS> 
submission.to_csv('my_submission.csv', index=False )