Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<categorify>
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")
 train
train_data = df_transform(train_data)
 train_data.head()<categorify>
train['SalePrice'] = np.log1p(train['SalePrice'] )
test_data = df_transform(test_data)
 test_data.head()<train_model>
corr["SalePrice"].sort_values(ascending = False )
model = DecisionTreeClassifier(max_depth=5, random_state=42)
 model.fit(train_data, target )<predict_on_test>
data = pd.concat([train, test], axis = 0, sort = False)
 data.drop(['Id', 'SalePrice'], axis = 1)
 data
y_train = model.predict(train_data ).astype(int )<compute_test_metric>
missing = data.isnull().sum().sort_values(ascending = False)
 missing
confusion_matrix(target, y_train )<predict_on_test>
missingg = missing*100/len(data)
 missing_data = pd.concat([missing, missingg], axis=1, keys=['missing', 'missing_%'])
 missing_data
y_pred = model.predict(test_data ).astype(int )<save_to_csv>
data.drop(( missing_data[missing_data['missing'] > 5] ).index, axis = 1, inplace = True )
submission["Survived"] = y_pred
submission.to_csv('submission.csv', index=False)
 submission['Survived'].hist()<load_from_csv>
numeric = ['BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath', 'GarageArea', 'GarageCars']
for feature in numeric: data[feature] = data[feature].fillna(0)
 categorical = ['Exterior1st', 'Exterior2nd', 'SaleType', 'MSZoning', 'Electrical', 'KitchenQual']
for feature in categorical: data[feature] = data[feature].fillna(data[feature].mode() [0] )
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 train.head()<load_from_csv>
data['Functional'] = data['Functional'].fillna('Typ' )
test = pd.read_csv('/kaggle/input/titanic/test.csv')
 test.head()<concatenate>
data.drop(['Utilities'], axis = 1, inplace = True )
Id = test["PassengerId"]
all_data = pd.concat([train,test],ignore_index = True )<categorify>
numeric_feats = data.dtypes[data.dtypes != 'object'].index
skewed_feats = data[numeric_feats].apply(lambda x: x.skew() ).sort_values(ascending = False)
 high_skew = skewed_feats[abs(skewed_feats)> 0.5]
high_skew
all_data['Name']
all_data["Title"] = all_data["Name"].apply(lambda x:x.split(",")[1].split(".")[0].strip())
 Title_Dict = {}
Title_Dict.update(dict.fromkeys(['Capt', 'Col', 'Major', 'Dr', 'Rev'],"Officer"))
Title_Dict.update(dict.fromkeys(['Don', 'Sir', 'the Countess', 'Dona', 'Lady'], 'Royalty'))
Title_Dict.update(dict.fromkeys(['Mme', 'Ms', 'Mrs'], 'Mrs'))
Title_Dict.update(dict.fromkeys(['Mlle', 'Miss'], 'Miss'))
Title_Dict.update(dict.fromkeys(['Mr'], 'Mr'))
Title_Dict.update(dict.fromkeys(['Master','Jonkheer'], 'Master'))
all_data["Title"] = all_data["Title"].map(Title_Dict )<categorify>
for feature in high_skew.index: data[feature] = np.log1p(data[feature] )
all_data['FamilySize']=all_data['SibSp']+all_data['Parch']+1
def SizeToLabel(s):
if(s>=2)&(s<=4):
return 2
elif(( s>=5)&(s<=7)) |(s == 1):
return 1
elif(s>7):
return 0
all_data["FamilyLabel"] = all_data["FamilySize"].apply(SizeToLabel )<feature_engineering>
data = pd.get_dummies(data)
 data
all_data["Cabin"] = all_data["Cabin"].fillna("Unknown")
 all_data["Deck"] = all_data["Cabin"].str.get(0 )<count_values>
y_train = train["SalePrice"]
x_train = data[:len(y_train)]
x_test = data[len(y_train):]
Tick_Counts = dict(all_data['Ticket'].value_counts())
 all_data["TicketGroup"] = all_data["Ticket"].apply(lambda x:Tick_Counts[x] )<categorify>
scorer = make_scorer(mean_squared_error, greater_is_better = False)
 def rmse_CV_train(model):
kf = KFold(5, shuffle = True, random_state = 42 ).get_n_splits(x_train.values)
 rmse = np.sqrt(-cross_val_score(model, x_train, y_train, scoring = "neg_mean_squared_error", cv = kf))
return(rmse)
 def rmse_CV_test(model):
kf = KFold(5, shuffle = True, random_state = 42 ).get_n_splits(train.values)
 rmse = np.sqrt(-cross_val_score(model, x_test, y_test,scoring = "neg_mean_squared_error", cv = kf))
return(rmse )
def GroupToLabel(s):
if(s>=2)&(s<=4):
return 2
elif(( s>=5)&(s<=8)) |(s==1):
return 1
elif(s>8):
return 0
all_data["TicketLabel"] = all_data["TicketGroup"].apply(GroupToLabel )<feature_engineering>
model = XGB.XGBRegressor(colsample_bytree = 0.4603, gamma = 0.0468, learning_rate = 0.05, max_depth = 3, min_child_weight = 1.7817, n_estimators = 2200, reg_alpha = 0.4640, reg_lambda = 0.8571, subsample = 0.5213, random_state = 7, nthread = -1)
 model.fit(x_train, y_train )
def sex(s):
if s=="male":
return 1
else:
return 0
all_data["Sex"] = all_data["Sex"].apply(sex )<feature_engineering>
prediction = np.floor(np.expm1(model.predict(x_test)) )
def title(s):
if s=="Officer":
return 0
elif s=="Mr":
return 1
elif s=="Mrs":
return 2
elif s=="Miss":
return 3
elif s=="Master":
return 4
elif s=="Royalty":
return 5
all_data["Title"] = all_data["Title"].apply(title )<feature_engineering>
submission = pd.DataFrame({'Id': test.Id, 'SalePrice': prediction})
 submission
<prepare_x_and_y><EOS> 
submission.to_csv('submission.csv', index = False )