Kaggle competition: CareerCon 2019 - Help Navigate Robots
<SOS> metric: categorizationaccuracy Kaggle data source: career-con-2019<categorify>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import math
import sklearn as sk
from sklearn import ensemble
from sklearn import model_selection
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from imblearn import under_sampling, over_sampling
from imblearn.over_sampling import SMOTE
lbl_enc = LabelEncoder()
def one_hot_encode_columns(df: pd.DataFrame, cols: [str])-> pd.DataFrame:
for col in cols:
df[col] = df[col].astype(CategoricalDtype(categories=FEATURES['categorical'][col]))
return pd.get_dummies(df, columns=cols, prefix=cols)
 def norm_dataset(df, cols):
df = df.copy()
for c in cols:
df[c] =(df[c] - train_stats['mean'][c])/ train_stats['std'][c]
return df
def denorm_dataset(df, cols):
df = df.copy()
for c in cols:
df[c] =(df[c] * train_stats['std'][c])+ train_stats['mean'][c]
return df
def clean_dataset(df: pd.DataFrame, is_training=True)-> pd.DataFrame:
df = df.copy()
if is_training:
df = df[df['GrLivArea'] < 4600]
df = df[~(( df['GrLivArea'] > 3200)&(df['SalePrice'] < 300_000)) ]
df = df[df['GarageArea'] < 1200]
df = df[df['TotalBsmtSF'] < 4000]
df = df[df['1stFlrSF'] < 2800]
if is_training:
id_col = df.pop('Id')
 df = norm_dataset(df, [f for f in FEATURES_NUM if f in df.columns])
 if is_training:
df['SalePrice'] = df['SalePrice'].astype('float64')
 df = one_hot_encode_columns(df, FEATURES['categorical'].keys())
 for col in FEATURES['ordinal'].keys() :
df[col].fillna('None', inplace=True)
 df[col] = df[col].astype(str)
 df[col] = lbl_enc.fit_transform(df[col])
 for col in FEATURES['binary'].keys() :
def_val = FEATURES['binary'][col][0]
df[col] = df[col].apply(lambda x: 1 if x == def_val else 0 ).astype('uint8')
 df = df.fillna(df.mean())
 return df
clean_train_set = clean_dataset(train_csv_no)
 clean_test_set = clean_dataset(test_csv, is_training=False)
 clean_train_set.info(verbose=True, null_counts=True )<count_missing_values>
x_test = pd.read_csv('.. /input/X_test.csv')
 x_train = pd.read_csv('.. /input/X_train.csv')
 y_train = pd.read_csv('.. /input/y_train.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv')
 print('Train X: {}
Train Y: {}
Test X: {}
Submission: {}'.format(x_train.shape,y_train.shape,x_test.shape,submission.shape))
print("Number of nulls in Dataset:", clean_train_set.isnull().sum().sum() )<prepare_x_and_y>
x_train = normalize(x_train)
 x_train = add_euler_angles(x_train)
 x_test = normalize(x_test)
 x_test = add_euler_angles(x_test )
x = clean_train_set.drop(['SalePrice'], axis=1)
 y = clean_train_set['SalePrice']
print(f"len(x)={len(x)}, len(y)={len(y)}, shape(x)={x.shape}" )<choose_model_class>
train_features = pd.DataFrame()
test_features = pd.DataFrame()
lr_schedule = tf.keras.optimizers.schedules.InverseTimeDecay(
 0.001,
decay_steps=2000,
decay_rate=1,
staircase=False)
 def get_optimizer() :
return keras.optimizers.Adam(lr_schedule)
 LAYERS = 3
model = keras.Sequential([
Input(x.iloc[0].shape),
*deepflatten([
[
Dense(64, activation=keras.activations.tanh),
Dropout(1/LAYERS),
]
for _ in range(LAYERS)
 ]),
Dense(1),
])
 model.compile(
 optimizer=get_optimizer() ,
loss=keras.losses.MeanSquaredError() , 
)
 model.summary()
try:
history = model.fit(
 x, y,
epochs=1000,
validation_split=.2,
verbose=0,
callbacks=[
keras.callbacks.ModelCheckpoint("best_val_loss.h5", monitor='val_loss'),
keras.callbacks.EarlyStopping(monitor='val_loss', patience=100, min_delta=0.0005),
tfdocs.modeling.EpochDots() ,
], 
)
 except KeyboardInterrupt:
pass
print('
-------------- Training Done --------------')
 print(f"Last: loss={history.history['loss'][-1]}, val_loss={history.history['val_loss'][-1]}")
<drop_column>
x_train = add_direction_vectors(x_train)
 x_test = add_direction_vectors(x_test)
 x_train.head()
x_test = clean_test_set.drop(['Id'], axis=1)
 x_test.sample(10 )<predict_on_test>
stats = ['norm_orientation_X', 'norm_orientation_Y', 'norm_orientation_Z',
'norm_orientation_W',
'norm_velocity_X', 'norm_velocity_Y', 'norm_velocity_Z',
'norm_acceleration_X', 'norm_acceleration_Y', 'norm_acceleration_Z',
'mod_linear_acceleration',
'ratio_velocity-acceleration',
'orientation_vector_X', 'orientation_vector_Y', 'orientation_vector_Z']
train_features = descriptive_features(train_features, x_train, stats)
 test_features = descriptive_features(test_features, x_test, stats )
y_test_predictions = model.predict(x_test)
 y_test_predictions =(y_test_predictions * train_stats['std']['SalePrice'])+ train_stats['mean']['SalePrice']
y_test_predictions[:10]<save_to_csv>
train_features = eng_features(train_features)
 test_features = eng_features(test_features)
 print("Train features: ", train_features.shape)
 print("Test features: ", test_features.shape )
submission = clean_test_set[['Id']]
submission['SalePrice'] = y_test_predictions
submission.to_csv("submission.csv", index=False)
 submission<import_modules>
drops = []
threshold =.95
for i in range(len(top_corr)) :
if top_corr.iloc[i]["raw corr"] > threshold:
if top_corr.iloc[i]["var1"] not in drops and top_corr.iloc[i]["var2"] not in drops:
drops.append(top_corr.iloc[i]["var2"])
 train_features = train_features.drop(columns=drops)
 test_features = test_features.drop(columns=drops )
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.impute import SimpleImputer
from scipy import stats
from scipy.stats import norm, skew
from scipy.special import boxcox1p
from scipy.stats import boxcox_normmax
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor, BaggingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.linear_model import Ridge, RidgeCV
from sklearn.linear_model import ElasticNet, ElasticNetCV
from sklearn.svm import SVR
from sklearn.ensemble import StackingRegressor
from mlxtend.regressor import StackingCVRegressor
import lightgbm as lgb
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import scale
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import RobustScaler
from sklearn.decomposition import PCA<load_from_csv>
seed = 1920348
sm = SMOTE(sampling_strategy='not majority', random_state=seed, k_neighbors=10)
 y_train_rs = pd.DataFrame()
train_features_rs, y_train_rs['surface'] = sm.fit_resample(train_features, y_train['surface'] )
train = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
 sample = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv" )<feature_engineering>
resample = True
if resample:
x, y = train_features_rs, y_train_rs['surface']
final_model = model_rs
else:
x, y = train_features, y_train['surface']
final_model = model
train["SalePrice"] = np.log1p(train["SalePrice"] )<drop_column>
sk.metrics.confusion_matrix(y,final_model.predict(x), y.unique() )
train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<12.5)].index, inplace=True)
 train.reset_index(drop=True, inplace=True )<drop_column>
submission['surface'] = final_model.predict(test_features)
 submission.head()
<concatenate><EOS> 
submission.to_csv('final_submission_smote.csv', index = False )