Kaggle competition: Forest Cover Type Prediction
<SOS> metric: CategorizationAccuracy Kaggle data source: forest-cover-type-prediction<statistical_test>
%matplotlib inline
warnings.filterwarnings('ignore' )
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(200):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
x1[:,c+200] = x1[ids][:,c+200]
x1[:,c+400] = x1[ids][:,c+400]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(200):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
x1[:,c+200] = x1[ids][:,c+200]
x1[:,c+400] = x1[ids][:,c+400]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y<load_from_csv>
train = pd.read_csv('.. /input/forest-cover-type-prediction/train.csv')
 train.head()
train_data=pd.read_csv(path+"train.csv")
 test_data=pd.read_csv(path+"test.csv" )<define_variables>
test = pd.read_csv('.. /input/forest-cover-type-prediction/test.csv')
 test.head()
features = [x for x in train_data.columns if x not in ['ID_code', "target"]]
label = "target"<count_unique_values>
test['Euclidian_Distance_To_Hydrology'] =(test['Horizontal_Distance_To_Hydrology']**2 + test['Vertical_Distance_To_Hydrology']**2)**0.5
test['Mean_Elevation_Vertical_Distance_Hydrology'] =(test['Elevation'] + test['Vertical_Distance_To_Hydrology'])/2
test['Mean_Distance_Hydrology_Firepoints'] =(test['Horizontal_Distance_To_Hydrology'] + test['Horizontal_Distance_To_Fire_Points'])/2
test['Mean_Distance_Hydrology_Roadways'] =(test['Horizontal_Distance_To_Hydrology'] + test['Horizontal_Distance_To_Roadways'])/2
test['Mean_Distance_Firepoints_Roadways'] =(test['Horizontal_Distance_To_Fire_Points'] + test['Horizontal_Distance_To_Roadways'])/2
test
df_test = test_data.values
unique_samples = []
unique_count = np.zeros_like(df_test)
 for feature in tqdm_notebook(range(df_test.shape[1])) :
if feature in [0]:
print('ok')
 continue
_, index_, count_ = np.unique(df_test[:, feature], return_counts=True, return_index=True)
 unique_count[index_[count_ == 1], feature] += 1
real_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)> 0)[:, 0]
synthetic_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)== 0)[:, 0]<concatenate>
train['Euclidian_Distance_To_Hydrology'] =(train['Horizontal_Distance_To_Hydrology']**2 + train['Vertical_Distance_To_Hydrology']**2)**0.5
train['Mean_Elevation_Vertical_Distance_Hydrology'] =(train['Elevation'] + train['Vertical_Distance_To_Hydrology'])/2
train['Mean_Distance_Hydrology_Firepoints'] =(train['Horizontal_Distance_To_Hydrology'] + train['Horizontal_Distance_To_Fire_Points'])/2
train['Mean_Distance_Hydrology_Roadways'] =(train['Horizontal_Distance_To_Hydrology'] + train['Horizontal_Distance_To_Roadways'])/2
train['Mean_Distance_Firepoints_Roadways'] =(train['Horizontal_Distance_To_Fire_Points'] + train['Horizontal_Distance_To_Roadways'])/2
train
idx_ = list(real_samples_indexes)
 data=pd.concat([train_data,test_data.loc[idx_]])
<count_values>
for col in tqdm_notebook(features):
count=data[col].value_counts()
rank=len(data[col].unique())
 train_data["rank_"+col]=train_data[col].map(count.rank() /rank)
 test_data["rank_"+col]=test_data[col].map(count.rank() /rank )<feature_engineering>
for col in tqdm_notebook(features):
unique_value = train_data['rank_'+col].unique().tolist() +test_data['rank_'+col].unique().tolist()
uq_v = np.min(sorted(list(set(unique_value))))
train_data['cut_'+col] = train_data[col]
test_data['cut_'+col] = test_data[col]
mean1=train_data['cut_'+col].mean()
mean2=test_data['cut_'+col].mean()
train_data['cut_'+col][(train_data['rank_'+col]==uq_v)]=mean1
test_data['cut_'+col][(test_data['rank_'+col]==uq_v)]=mean2<drop_column>
X_cat = test.iloc[:,11:55].values
X_num = test.iloc[:, np.r_[0:11, 55:60]].values
train=train_data
test=test_data
del train_data,test_data,data
gc.collect()<define_variables>
scaler = StandardScaler()
scaler.fit(X_num)
 X_num = scaler.transform(X_num)
 print(f'Categorical Shape: {X_cat.shape}')
 print(f'Numerical Shape: {X_num.shape}')
features = [x for x in train.columns if x not in ['ID_code', "target"]]
label = "target"<count_values>
X = np.hstack(( X_num, X_cat))
print(X.shape )
"feature count:",len(features )<define_variables>
features = [
'Elevation','Horizontal_Distance_To_Roadways', 'Horizontal_Distance_To_Hydrology',
'Vertical_Distance_To_Hydrology','Aspect','Slope','Euclidian_Distance_To_Hydrology',
'Mean_Elevation_Vertical_Distance_Hydrology','Mean_Distance_Hydrology_Firepoints',
'Mean_Distance_Hydrology_Roadways','Mean_Distance_Firepoints_Roadways'
]
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=99999)
 oof = train[['ID_code']]
oof['target'] = 0
predictions = test[['ID_code']]
val_aucs = []<split>
y = train["Cover_Type"]
X = pd.get_dummies(train[features])
 X_test = pd.get_dummies(test[features])
 model = ExtraTreesClassifier(n_estimators=1000, random_state=42, max_features='log2')
 model.fit(X,y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'Id': test.Id, 'Cover_Type': predictions})
 output.to_csv('forest_cover_submission.csv', index=False)
 print("Your submission was successfully saved!" )