Kaggle competition: CareerCon 2019 - Help Navigate Robots
<SOS> metric: categorizationaccuracy Kaggle data source: career-con-2019<create_dataframe>
import os
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold
import itertools
import xgboost as xgb
import time
from sklearn.preprocessing import StandardScaler
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import *
all_data_na =(all_features.isnull().sum() / len(all_features)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head(20 )<count_values>
X_train = pd.read_csv(".. /input/X_train.csv")
 X_test = pd.read_csv(".. /input/X_test.csv")
 y_train = pd.read_csv(".. /input/y_train.csv")
 sub = pd.read_csv(".. /input/sample_submission.csv" )
all_features['MSSubClass'].value_counts()<count_values>
def fe(data):
df = pd.DataFrame()
data['totl_anglr_vel'] =(data['angular_velocity_X']**2 + data['angular_velocity_Y']**2 +
data['angular_velocity_Z']**2)** 0.5
data['totl_linr_acc'] =(data['linear_acceleration_X']**2 + data['linear_acceleration_Y']**2 +
data['linear_acceleration_Z'])**0.5
data['totl_xyz'] =(data['orientation_X']**2 + data['orientation_Y']**2 +
data['orientation_Z'])**0.5
data['acc_vs_vel'] = data['totl_linr_acc'] / data['totl_anglr_vel']
for col in data.columns:
if col in ['row_id','series_id','measurement_number']:
continue
df[col + '_mean'] = data.groupby(['series_id'])[col].mean()
df[col + '_median'] = data.groupby(['series_id'])[col].median()
df[col + '_max'] = data.groupby(['series_id'])[col].max()
df[col + '_min'] = data.groupby(['series_id'])[col].min()
df[col + '_std'] = data.groupby(['series_id'])[col].std()
df[col + '_range'] = df[col + '_max'] - df[col + '_min']
df[col + '_maxtoMin'] = df[col + '_max'] / df[col + '_min']
df[col + '_mean_abs_chg'] = data.groupby(['series_id'])[col].apply(lambda x: np.mean(np.abs(np.diff(x))))
df[col + '_abs_max'] = data.groupby(['series_id'])[col].apply(lambda x: np.max(np.abs(x)))
 df[col + '_abs_min'] = data.groupby(['series_id'])[col].apply(lambda x: np.min(np.abs(x)))
 df[col + '_abs_avg'] =(df[col + '_abs_min'] + df[col + '_abs_max'])/2
return df
all_features.OverallCond.value_counts()<data_type_conversions>
le = LabelEncoder()
y_train['surface'] = le.fit_transform(y_train['surface'])
all_features['MSSubClass'] = all_features['MSSubClass'].apply(str)
 all_features['YrSold'] = all_features['YrSold'].astype(str)
 all_features['MoSold'] = all_features['MoSold'].astype(str)
 all_features['OverallCond'] = all_features['OverallCond'].astype(str )<categorify>
X_train.fillna(0, inplace = True)
 X_test.fillna(0, inplace = True)
 X_train.replace(-np.inf, 0, inplace = True)
 X_train.replace(np.inf, 0, inplace = True)
 X_test.replace(-np.inf, 0, inplace = True)
 X_test.replace(np.inf, 0, inplace = True )
all_features["PoolQC"] = all_features["PoolQC"].fillna("None")
 all_features["MiscFeature"] = all_features["MiscFeature"].fillna("None")
 all_features["Alley"] = all_features["Alley"].fillna("None")
 all_features["Fence"] = all_features["Fence"].fillna("None")
 all_features["FireplaceQu"] = all_features["FireplaceQu"].fillna("None")
 all_features["LotFrontage"] = all_features.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_features[col] = all_features[col].fillna('None')
 for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_features[col] = all_features[col].fillna(0)
 for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_features[col] = all_features[col].fillna(0)
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_features[col] = all_features[col].fillna('None')
 all_features["MasVnrType"] = all_features["MasVnrType"].fillna("None")
 all_features["MasVnrArea"] = all_features["MasVnrArea"].fillna(0)
 all_features['MSZoning'] = all_features['MSZoning'].fillna(all_features['MSZoning'].mode() [0])
 all_features = all_features.drop(['Utilities'], axis=1)
 all_features["Functional"] = all_features["Functional"].fillna("Typ")
 all_features['Electrical'] = all_features['Electrical'].fillna(all_features['Electrical'].mode() [0])
 all_features['KitchenQual'] = all_features['KitchenQual'].fillna(all_features['KitchenQual'].mode() [0])
 all_features['Exterior1st'] = all_features['Exterior1st'].fillna(all_features['Exterior1st'].mode() [0])
 all_features['Exterior2nd'] = all_features['Exterior2nd'].fillna(all_features['Exterior2nd'].mode() [0])
 all_features['SaleType'] = all_features['SaleType'].fillna(all_features['SaleType'].mode() [0])
 all_features['MSSubClass'] = all_features['MSSubClass'].fillna("None")
<sort_values>
kfold = StratifiedKFold(n_splits=5)
 train_x,val_x,train_y,val_y = train_test_split(data, target, test_size = 0.10, random_state=14)
 train_x.shape,val_x.shape,train_y.shape,val_y.shape
all_features_na =(all_features.isnull().sum() / len(all_features)) * 100
all_features_na = all_features_na.drop(all_features_na[all_features_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_features_na})
 missing_data.head()<categorify>
train_data = lightgbm.Dataset(train_x, label=train_y)
 test_data = lightgbm.Dataset(val_x, label=val_y )
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_features[c].values))
all_features[c] = lbl.transform(list(all_features[c].values))
print('Shape all_features: {}'.format(all_features.shape))<feature_engineering>
para={'boosting_type': 'gbdt',
'colsample_bytree': 0.85,
'learning_rate': 0.1,
'max_bin': 512,
'max_depth': -1,
'metric': 'multi_error',
'min_child_samples': 8,
'min_child_weight': 1,
'min_split_gain': 0.5,
'nthread': 3,
'num_class': 9,
'num_leaves': 31,
'objective': 'multiclass',
'reg_alpha': 0.8,
'reg_lambda': 1.2,
'scale_pos_weight': 1,
'subsample': 0.7,
'subsample_for_bin': 200,
'subsample_freq': 1}
model = lightgbm.train(para,
train_data,
valid_sets=test_data,
num_boost_round=5000,
early_stopping_rounds=50,
)
all_features['TotalSF'] = all_features['TotalBsmtSF'] + all_features['1stFlrSF'] + all_features['2ndFlrSF']<define_variables>
y_pred = model.predict(X_test )
numeric_dtypes = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numeric = []
for i in all_features.columns:
if all_features[i].dtype in numeric_dtypes:
numeric.append(i )<feature_engineering>
class_prediction=pd.DataFrame(y_pred ).idxmax(axis=1 )
lam = 0.15
for i in skew_index:
all_features[i] = boxcox1p(all_features[i],lam )<categorify>
submission1 = pd.DataFrame({
"series_id": X_test.index,
"surface": class_prediction,
})
 submission1.surface.value_counts()
all_features = pd.get_dummies(all_features)
 print(all_features.shape )<count_duplicates>
submission1.surface=le.inverse_transform(submission1.surface )
all_features = all_features.loc[:,~all_features.columns.duplicated() ]<compute_train_metric>
submission1.to_csv('submissionlgb.csv', index=False )