Kaggle competition: Mercedes-Benz Greener Manufacturing
<SOS> metric: R2Score Kaggle data source: mercedes-benz-greener-manufacturing<drop_column>
rcParams['figure.figsize'] = 50,20
img=cv2.imread(".. /input/private-score/score.JPG")
 plt.imshow(img )
predictors = data[data.Age > 0]
predictors.drop(['Age'], axis=1, inplace=True)
 targets = np.array(data[data.Age > 0].Age)
 predictors.shape, targets.shape<normalization>
train = pd.read_csv('.. /input/mercedes-benz-greener-manufacturing/train.csv')
 test = pd.read_csv('.. /input/mercedes-benz-greener-manufacturing/test.csv')
 df = train
predictors = StandardScaler().fit_transform(predictors )<compute_train_metric>
for i in df_cat.columns:
print('The unique values in '+i+' are: ',df[i].nunique() ,'
',df_cat[i].unique() ,'
',"--------------------------------------------------------------------------------" )
mlp = MLPRegressor(hidden_layer_sizes=(150, 100))
mlp.fit(predictors, targets)
 y_pred = mlp.predict(predictors)
 mean_squared_error(y_pred, targets), mlp<predict_on_test>
print(df.isnull().sum().sum(axis=0))
train.loc[train.Age < 0, 'Age'] = mlp.predict(StandardScaler().fit_transform(train[train['Age'] < 0][['Alone', 'Cabin', 'Fare', 'Pclass', 'Sex','Ticket', 'Title']]))<predict_on_test>
temp=df.y.values
df_cat['y']=temp
print(df_cat.head() )
test.loc[test.Age < 0, 'Age'] = mlp.predict(StandardScaler().fit_transform(test[test['Age'] < 0][['Alone', 'Cabin', 'Fare', 'Pclass', 'Sex','Ticket', 'Title']]))<feature_engineering>
print(( df.loc[df.y>150,'y'].values))
df=df[df.y<150]
print("Removing outliers based on above information and setting 150 as a threshold value.................... ")
 print(df.shape)
 df_cat=df_cat[df_cat.y<150]
df_num=df_num[df_num.y<150]
test.loc[test.Age < 0, 'Age'] = 0.1
train.loc[train.Age < 0, 'Age'] = 0.1
train.head()<normalization>
rcParams['figure.figsize'] = 15, 7
res = stats.probplot(df['y'], plot=plt )
predictors = train.drop(['PassengerId', 'Survived'], axis=1)
 targets = train[['Survived']]
predictors = StandardScaler().fit_transform(predictors )<split>
temp = []
for i in df_num.columns:
if df[i].var() ==0:
temp.append(i)
 print(len(temp))
print(temp )
x_train, x_test, y_train, y_test = train_test_split(predictors, targets, test_size = 0.05, random_state = 0 )<compute_train_metric>
count=0
low_var_col=[]
for i in test.columns:
if test[i].dtype == 'int64':
if test[i].var() <0.01:
low_var_col.append(i)
 count+=1
print(count)
 df.drop(low_var_col,axis=1,inplace=True)
 df_num.drop(low_var_col,axis=1,inplace=True)
 test.drop(low_var_col,axis=1,inplace=True )
mlp = MLPRegressor(batch_size=50, hidden_layer_sizes=(140))
mlp.fit(x_train, y_train)
 y_pred = mlp.predict(x_train ).round()
test_pred = mlp.predict(x_test ).round()
score = accuracy_score(y_train, y_pred)
 test_score = accuracy_score(y_test, test_pred)
 mlp, score, test_score<save_to_csv>
dic={}
for i in df_num.columns:
if i!='y':
if df[i].corr(df.y)>0.25 or df[i].corr(df.y)<-0.25:
dic[i]=df[i].corr(df.y)
 print("Important Features with there respective correlations are ",'
','---------------------------------------------------------','
',dic )
ids = test['PassengerId']
predictions = np.abs(mlp.predict(StandardScaler().fit_transform(test.drop(['PassengerId'], axis=1)) ).round() ).astype(int)
 output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )<set_options>
d = {}; done = []
cols = df.columns.values
for c in cols: d[c]=[]
for i in range(len(cols)) :
if i not in done:
for j in range(i+1, len(cols)) :
if all(df[cols[i]] == df[cols[j]]):
done.append(j)
 d[cols[i]].append(cols[j])
 dub_cols = []
for k in d.keys() :
if len(d[k])> 0:
dub_cols += d[k]
print('Dublicates:','
', dub_cols )
%matplotlib inline
print("Setup Complete")
 warnings.filterwarnings('ignore' )<load_from_csv>
df.drop(['X279','X76','X37','X134','X147','X222','X244','X326'] , axis=1 , inplace=True )
df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv',na_values='nan')
 df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv',na_values='nan')
 print("Train size:",df_train.shape)
 print("Test size:",df_test.shape)
 train_ID =df_train['Id']
test_ID =df_test['Id']
df_train.drop("Id", axis = 1, inplace = True)
 df_test.drop("Id", axis = 1, inplace = True)
 print("Size of train data after dropping Id: {}".format(df_train.shape))
print("Size of test data after dropping Id: {}".format(df_test.shape)) 
<drop_column>
test.drop(['X279','X76','X37','X134','X147','X222','X244','X326'] , axis=1 , inplace=True)
 df_num.drop(['X279','X76','X37','X134','X147','X222','X244','X326'] , axis=1 , inplace=True )
train = df_train.drop(df_train[(df_train['GrLivArea']>4000)&(df_train['SalePrice']<300000)].index )<sort_values>
categorical=[]
for i in df.columns:
if df[i].dtype=='object':
le = preprocessing.LabelEncoder()
le.fit(list(df[i].values)+ list(test[i].values))
print("Categories in the encoded order from 1 to the size of "+i+" are : ")
 print(le.classes_)
 print("--------------------------------------------------------------------------")
 df[i] = le.transform(list(df[i].values))
test[i] = le.transform(list(test[i].values))
categorical.append(i )
p_df=[]
for col in df_train_num.columns:
stat, p = shapiro(df_train[col])
 p_df.append(p)
 p_df_=pd.DataFrame(p_df,df_train_num.columns ).sort_values(by=0,ascending=True)
 p_df_=p_df_.loc[p_df_[0]>0.05]
p_df_<prepare_x_and_y>
train_y = df["y"].values
train_X = df.drop(['y'], axis=1)
 def xgb_r2_score(preds, final):
labels = dtrain.get_label()
return 'r2', r2_score(labels, preds)
 xgb_params = {
'n_trees': 520,
'eta': 0.0045,
'max_depth': 4,
'subsample': 0.98,
'objective': 'reg:linear',
'eval_metric': 'rmse',
'base_score': np.mean(train_y),
'silent': 1
}
final = xgb.DMatrix(train_X, train_y, feature_names=train_X.columns.values)
 model = xgb.train(dict(xgb_params), final, num_boost_round=200, feval=xgb_r2_score, maximize=True)
 fig, ax = plt.subplots(figsize=(10,10))
xgb.plot_importance(model, max_num_features=40, height=0.8, ax=ax, color = 'coral')
 print("Feature Importance by XGBoost")
 plt.show()
model = RandomForestRegressor(n_estimators=200, max_depth=10, min_samples_leaf=4, max_features=0.2, n_jobs=-1, random_state=0)
 model.fit(train_X, train_y)
 feat_names = train_X.columns.values
importances = model.feature_importances_
std = np.std([tree.feature_importances_ for tree in model.estimators_], axis=0)
 indices = np.argsort(importances)[::-1][:40]
plt.subplots(figsize=(10,10))
plt.title("Feature importances by RandomForestRegressor")
 plt.ylabel("Features")
 plt.barh(range(len(indices)) , importances[indices], color="green", align="center")
 plt.yticks(range(len(indices)) , feat_names[indices], rotation='horizontal')
 plt.ylim([-1, len(indices)])
 plt.show()
ntrain = train.shape[0]
ntest = df_test.shape[0]
all_data = pd.concat(( train,df_test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape)) 
<create_dataframe>
df['X314_plus_X315'] = df.apply(lambda row: row.X314 + row.X315, axis=1)
 test['X314_plus_X315'] = test.apply(lambda row: row.X314 + row.X315, axis=1 )
all_data_na =(all_data.isnull().sum() / len(all_data)) * 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} )<categorify>
print("Correalation between X314_plus_X315 and y is : ",df.y.corr(df['X314_plus_X315']))
print("Which makes it pretty much high !! Awesome !!" )
all_data_rep=all_data.copy()
all_data_rep["PoolQC"] = all_data_rep["PoolQC"].fillna(0)
 all_data_rep["MiscFeature"] = all_data_rep["MiscFeature"].fillna(0)
 all_data_rep["Fence"] = all_data_rep["Fence"].fillna(0)
 all_data_rep["FireplaceQu"] = all_data_rep["FireplaceQu"].fillna(0)
 all_data_rep["LotFrontage"] = all_data_rep.groupby('Neighborhood')["LotFrontage"].transform(
 lambda x:x.fillna(x.median()))
grd_col=["GarageQual","GarageCond","GarageType","GarageFinish","GarageArea","GarageCars","GarageYrBlt"]
for col in grd_col:
all_data_rep[col] = all_data_rep[col].fillna(0)
 grd_col=["BsmtExposure","BsmtCond","BsmtQual","BsmtFinType1","BsmtFinType2","BsmtUnfSF","BsmtFinSF1","BsmtFinSF2"]
for col in grd_col:
all_data_rep[col] = all_data_rep[col].fillna(0)
 all_data_rep["TotalBsmtSF"] = all_data_rep["TotalBsmtSF"].fillna(0)
 all_data_rep["MasVnrArea"] = all_data_rep["MasVnrArea"].fillna(0)
 all_data_rep["MasVnrType"] = all_data_rep["MasVnrType"].fillna(0)
 all_data_rep['MSZoning']=all_data_rep['MSZoning'].fillna(all_data_rep['MSZoning'].mode() [0])
 all_data_rep["BsmtFullBath"] = all_data_rep["BsmtFullBath"].fillna(0)
 all_data_rep["BsmtHalfBath"] = all_data_rep["BsmtHalfBath"].fillna(0)
 all_data_rep["Functional"] =all_data_rep["Functional"].fillna("Typ")
 all_data_rep["Alley"] = all_data_rep["Alley"].fillna(0)
 mode_col = ['Electrical','KitchenQual', 'Exterior1st', 'Exterior2nd', 'SaleType']
for col in mode_col:
all_data_rep[col] = all_data_rep[col].fillna(all_data_rep[col].mode() [0])
 all_data_rep["MoSold"] = all_data_rep["MoSold"].fillna(0 )<count_missing_values>
all_data_rep.isnull().sum().sort_values(ascending=False)[:3]<feature_engineering>
df['X118_plus_X314_plus_X315'] = df.apply(lambda row: row.X118 + row.X314 + row.X315, axis=1)
 test['X118_plus_X314_plus_X315'] = test.apply(lambda row: row.X118 + row.X314 + row.X315, axis=1 )
all_data_rep['TotalSF'] = all_data_rep['TotalBsmtSF'] + all_data_rep['1stFlrSF'] + all_data_rep['2ndFlrSF']<prepare_output>
df["X10_plus_X54"] = df.apply(lambda row: row.X10 + row.X54, axis=1)
 test["X10_plus_X54"] = test.apply(lambda row: row.X10 + row.X54, axis=1)
 print("Correalation between X10_plus_X54 and y is : ",df.y.corr(df['X10_plus_X54']))
all_data_rep_enc_1=all_data_rep.copy()<data_type_conversions>
df["X10_plus_X29"] = df.apply(lambda row: row.X10 + row.X29, axis=1)
 test["X10_plus_X29"] = test.apply(lambda row: row.X10 + row.X29, axis=1)
 print("Correalation between X10_plus_X29 and y is : ",df.y.corr(df['X10_plus_X29']))
all_data_rep_enc_1['MSSubClass']=all_data_rep_enc_1['MSSubClass'].astype(str)
 all_data_rep_enc_1['YrSold']=all_data_rep_enc_1['YrSold'].astype(str)
 all_data_rep_enc_1['MoSold']=all_data_rep_enc_1['MoSold'].astype(str)
<categorify>
train_X['X314_plus_X315']=df['X314_plus_X315']
train_X['X118_plus_X314_plus_X315']=df['X118_plus_X314_plus_X315']
train_X["X10_plus_X54"] = df["X10_plus_X54"]
train_X["X10_plus_X29"] = df["X10_plus_X29"]
qual_map = {0: 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
qual_col=['FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond','ExterQual',
'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual']
for col in qual_col:
all_data_rep_enc_1[col] = all_data_rep_enc_1[col].map(qual_map)
 bsm_exp_map={0: 0, 'No': 1, 'Mn': 2, 'Av': 3, 'Gd': 4}
all_data_rep_enc_1['BsmtExposure'] = all_data_rep_enc_1['BsmtExposure'].map(bsm_exp_map)
<sort_values>
booler = np.ones(400)
 for i in same_features:
if booler[int(i[1][1:])]==1:
booler[int(i[1][1:])]=0
df_num.drop(i[1],axis=1,inplace=True)
 df.drop(i[1],axis=1,inplace=True)
 test.drop(i[1],axis=1,inplace=True)
 train_X.drop(i[1],axis=1,inplace=True)
 elif booler[int(i[0][1:])]==1:
booler[int(i[0][1:])]=0
df_num.drop(i[0],axis=1,inplace=True)
 df.drop(i[0],axis=1,inplace=True)
 test.drop(i[0],axis=1,inplace=True)
 train_X.drop(i[0],axis=1,inplace=True )
all_data_rep_enc_1.isnull().sum().sort_values(ascending=False)[:3]<feature_engineering>
train["SalePrice"] = np.log1p(train["SalePrice"])
 check_skewness('SalePrice' )<prepare_x_and_y>
y_train=train["SalePrice"]<filter>
x_train, x_valid, y_train, y_valid = train_test_split(train_X, train_y, test_size=0.2, random_state=420)
 d_train = xgb.DMatrix(x_train, label=y_train)
 d_valid = xgb.DMatrix(x_valid, label=y_valid)
 d_test = xgb.DMatrix(test)
 xgb_params = {
'n_trees': 500,
'eta': 0.0050,
'max_depth': 3,
'subsample': 0.95,
'objective': 'reg:linear',
'eval_metric': 'rmse',
'base_score': np.mean(train_y),
'silent': 1
}
def xgb_r2_score(preds, dtrain):
labels = dtrain.get_label()
return 'r2', r2_score(labels, preds)
 watchlist = [(d_train, 'train'),(d_valid, 'valid')]
clf = xgb.train(xgb_params, d_train, 1050 , watchlist, early_stopping_rounds=70, feval=xgb_r2_score, maximize=True, verbose_eval=10 )
all_data_rep_enc_1.dtypes[all_data_rep_enc_1.dtypes=='object'].index<feature_engineering>
d_train = xgb.DMatrix(train_X, label=train_y)
 d_test = xgb.DMatrix(test)
 xgb_params = {
'n_trees': 500,
'eta': 0.0050,
'max_depth': 3,
'subsample': 0.95,
'objective': 'reg:linear',
'eval_metric': 'rmse',
'base_score': np.mean(train_y),
'silent': 1
}
def xgb_r2_score(preds, dtrain):
labels = dtrain.get_label()
return 'r2', r2_score(labels, preds)
 watchlist = [(d_train, 'train')]
clf = xgb.train(xgb_params, d_train, 1050 , watchlist, early_stopping_rounds=70, feval=xgb_r2_score, maximize=True, verbose_eval=10 )
skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.5
for feat in skewed_features:
all_data_rep_enc_1[feat] = boxcox1p(all_data_rep_enc_1[feat], lam )<drop_column>
Answer = clf.predict(d_test)
 sub = pd.DataFrame()
sub['ID'] = test.ID
sub['y'] = Answer
sub.to_csv('mercedes_benz_The_best_or_Nothing.csv', index=False )